DEV Community

Humphery
Humphery

Posted on

Building a Dynamic Profile Endpoint

By Otuoniyo Ufuoma-Oghene Humphery


Introduction

As part of the HNG 2025 Backend Wizards program, I recently completed Stage 0, where the task was to build a dynamic profile endpoint. The goal was simple yet powerful: create a RESTful API endpoint that returns my profile information along with a random cat fact fetched from an external API.

This task taught me not just the technicalities of building endpoints, but also how to consume third-party APIs, handle dynamic data, and structure JSON responses professionally.


Task Overview

Requirements:

  • Create a GET endpoint at /me.
  • Return JSON with Content-Type: application/json.
  • Include a dynamic cat fact fetched from Cat Facts API.
  • JSON response format:
{
  "status": "success",
  "user": {
    "email": "<your email>",
    "name": "<your full name>",
    "stack": "<your backend stack>"
  },
  "timestamp": "<current UTC time in ISO 8601 format>",
  "fact": "<random cat fact from Cat Facts API>"
}
Enter fullscreen mode Exit fullscreen mode
  • Handle API errors gracefully.
  • Follow best practices for code structure, environment variables, and logging.

My Work Process

Here’s how I approached this task step by step:

1. Setting Up the Server

I used Node.js and Express for this project. First, I initialized the server and created a simple route handler for /me.

app.get("/me", getUsers);
Enter fullscreen mode Exit fullscreen mode

2. Fetching Dynamic Cat Facts

I implemented a fetchWithTimeout utility to safely fetch data from the Cat Facts API, handling timeouts and network errors:

const catFactJson = await fetchWithTimeout(CAT_API_URL);
const catFact = catFactJson?.fact || "Could not fetch a cat fact right now.";
Enter fullscreen mode Exit fullscreen mode

This ensures the endpoint never crashes, even if the external API is down.

3. Formatting the JSON Response

Next, I structured the JSON response to strictly match the Stage 0 requirements:

res.json({
  status: "success",
  user: {
    email: "humpheryufuoma@gmail.com",
    name: "Otuoniyo Ufuoma-Oghene Humphery",
    stack: "Node.js/Express"
  },
  timestamp: new Date().toISOString(),
  fact: catFact
});
Enter fullscreen mode Exit fullscreen mode

I made sure the timestamp was dynamic and in UTC ISO 8601 format.

4. Environment Variables

For local development, I used .env to store the Cat Facts API URL:

CAT_API_URL=https://catfact.ninja/fact
Enter fullscreen mode Exit fullscreen mode

Note: Railway automatically adds quotes to environment variables, so in production I strip them in code before using the URL.

5. Handling Errors Gracefully

I added error handling to ensure a fallback cat fact is returned if the API fails:

catch (err) {
    res.status(500).json({
        status: "error",
        message: "Failed to fetch cat fact"
    });
}
Enter fullscreen mode Exit fullscreen mode

This guarantees a smooth user experience at all times.


Lessons Learned

  • Environment variables are crucial for separating config from code, but deployment platforms like Railway may add quirks (like automatic quotes) that you need to handle.
  • Dynamic data fetching requires proper error handling — always expect the external API to fail occasionally.
  • JSON schema validation is important; even small typos in field names can break automated checks.
  • Small tasks teach discipline: Stage 0 reinforced best practices in endpoint design, logging, and clean code.

Screenshots / GIFs

Response test on postman


Outcome

The final endpoint returns JSON like this:

{
  "status": "success",
  "user": {
    "email": "humpheryufuoma@gmail.com",
    "name": "Otuoniyo Ufuoma-Oghene Humphery",
    "stack": "Node.js/Express"
  },
  "timestamp": "2025-10-18T19:50:29.028Z",
  "fact": "A cat usually has about 12 whiskers on each side of its face."
}
Enter fullscreen mode Exit fullscreen mode
  1. Meets all Stage 0 requirements.
  2. Dynamic timestamp and cat fact.
  3. Graceful error handling.
  4. Ready for deployment on Railway.

Conclusion

Stage 0 of HNG Backend Wizards was packed with learning: working with REST APIs, dynamic data, and deployment quirks.

I’m excited to carry these best practices forward into Stage 1 and beyond, building more complex backend systems that are robust, maintainable, and dynamic.


GitHub Repository

View the code on GitHub


Top comments (0)