Skip to main content

Command Palette

Search for a command to run...

Building a Dynamic Profile API Endpoint

Published
3 min read

Introduction

A few days ago, I joined the HNG internship backed track, to be precise, and, as part of the Backend Stage 0 Task, I built a Dynamic Profile API Endpoint that returns my profile information along with a random cat fact fetched from the Cat Facts API.

This project helped me strengthen my skills in creating RESTful APIs, integrating third-party APIs, handling asynchronous requests, and structuring clean JSON responses.

The main objective was to create a GET endpoint at:

that returns a JSON response with the following structure:

{
  "status": "success",
  "user": {
    "email": "my email",
    "name": "My name",
    "stack": "Node.js/Express"
  },
  "timestamp": "2025-10-19T12:34:56.789Z",
  "fact": "Cats sleep for 70% of their lives."
}

Each field had to meet the following criteria:

  • status: Always returns "success".

  • user: Includes my name, email, and backend stack.

  • timestamp: Reflects the current UTC time in ISO 8601 format.

  • fact: A random cat fact fetched dynamically from the Cat Facts API on each request.

Tools & Stack Used

  • Runtime: Node.js

  • Framework: Express.js

  • HTTP Client: Axios

  • Environment Management: dotenv

  • Logging & Middleware: morgan, cors

  • Hosting Platform: PXXL App

  • Testing (local): Postman & cURL

Implementation Process

1️⃣ Setting Up the Project

I initialized my Node.js app and installed the required dependencies:

npm init -y
npm install express axios dotenv cors morgan

2️⃣ Project Structure

stage0-profile-endpoint/
│
├── index.js
├── package.json
├── .env
├── .env.example
└── README.md

3️⃣ Core Logic (index.js)

Here’s the main part of my code:

require('dotenv').config();
const express = require('express');
const axios = require('axios');
const cors = require('cors');
const morgan = require('morgan');

const app = express();
const PORT = process.env.PORT || 3000;

app.use(cors());
app.use(morgan('dev'));

app.get('/me', async (req, res) => {
  const response = {
    status: 'success',
    user: {
      email: process.env.EMAIL,
      name: process.env.FULL_NAME,
      stack: process.env.STACK
    },
    timestamp: new Date().toISOString(),
    fact: ''
  };

  try {
    const result = await axios.get('https://catfact.ninja/fact');
    response.fact = result.data.fact;
    res.status(200).json(response);
  } catch (error) {
    console.error(error.message);
    response.fact = 'A cat fact could not be retrieved right now.';
    res.status(200).json(response);
  }
});

app.get('/health', (req, res) => {
  res.json({ status: 'ok', timestamp: new Date().toISOString() });
});

app.listen(PORT, () => console.log(`Server running on port ${PORT}`));

🌍 Deployment

For hosting, Vercel and Render were not allowed, so I deployed my backend on PXXL App.

During setup:

  • I specified my environment variables.

  • Ensured the correct port (process.env.PORT).

  • Verified that /me returns a proper JSON structure.

✅ Live URL:
👉 https://abdul-profileendpoint.pxxl.click/me


💻 Testing Locally

Before deployment, I tested the endpoint using Postman:

GET: http://localhost:3000/me

Response Example:

{
  "status": "success",
  "user": {
    "email": "abdullateefdauda01@gmail.com",
    "name": "Abdullateef Dauda",
    "stack": "Node.js/Express"
  },
  "timestamp": "2025-10-19T14:02:11.324Z",
  "fact": "Cats have five toes on their front paws, but only four toes on their back paws."
}

🧠 What I Learned

This task was simple yet very educational. I learned how to:

  • Integrate and consume third-party APIs efficiently.

  • Handle asynchronous requests using async/await.

  • Gracefully handle API failures and fallback responses.

  • Properly format JSON responses and use semantic field names.

  • Deploy Node.js apps on alternative hosting platforms (like PXXL).

  • Use dotenv and environment variables securely for deployment.

More from this blog

Orbit414

9 posts