DEV Community

Cover image for Profile API with Cat Facts
Ameh Mathias Ejeh
Ameh Mathias Ejeh

Posted on

Profile API with Cat Facts

A simple RESTful API built with Node.js/Express that returns profile information along with dynamic cat facts.

Features

  • Profile endpoint at /me
  • Dynamic cat facts from external API
  • UTC timestamps in ISO 8601 format
  • Graceful error handling with fallback
  • Configurable timeout (5 seconds)
  • CORS enabled
  • Environment variable configuration
  • Request logging

Prerequisites

  • Node.js 18.x or higher
  • npm

Project Setup

├── app.js # Main application file ├── package.json # Project dependencies and scripts ├── .env.example # Environment variables template ├── .env # environment variables (git-ignored) └── README.md # Documentation 
Enter fullscreen mode Exit fullscreen mode

Installation

  • Clone or create the project directory
  • Install dependencies
npm install 
Enter fullscreen mode Exit fullscreen mode
  • Configure environment variables
cp.env.example .env 
Enter fullscreen mode Exit fullscreen mode
  • Edit .env file with your information
 PORT=8000 USER_EMAIL=yourname@example.com USER_NAME=Your Full Name USER_STACK=Node.js/Express 
Enter fullscreen mode Exit fullscreen mode

Usage

Start the server

Production mode:

npm start 
Enter fullscreen mode Exit fullscreen mode

Development mode

npm run dev 
Enter fullscreen mode Exit fullscreen mode

The server will start on http://localhost:8000

API Endpoints

GET /me
Returns profile information with a dynamic cat fact.
Response (Success - 200):

Response (Error - 503):

{ "status": "error", "user": { "email": "your.email@example.com", "name": "Your Full Name", "stack": "Node.js/Express" }, "timestamp": "2025-10-15T14:30:45.123Z", "fact": "Cat fact temporarily unavailable. Did you know cats spend 70% of their lives sleeping?", "error": "Unable to fetch cat fact from external API" } 
Enter fullscreen mode Exit fullscreen mode

GET /health
Health check endpoint.
Response (200):

Testing

Test the endpoint using postman:

// Test profile endpoint http://localhost:8000/me // Test health check http://localhost:8000/health 
Enter fullscreen mode Exit fullscreen mode

Error Handling

The API handles the following error scenarios:

  • Network errors: Returns 503 with fallback message
  • Timeout (5s): Returns 503 with fallback message
  • External API failure: Returns 503 with fallback message
  • Invalid routes: Returns 404 with error message
  • Server errors: Returns 500 with error message

Link

Top comments (0)