DEV Community

Cover image for Building a Dynamic Profile API with Express.js and TypeScript — HNG Backend Stage Zero
Ugochukwu Nebolisa
Ugochukwu Nebolisa

Posted on

Building a Dynamic Profile API with Express.js and TypeScript — HNG Backend Stage Zero

Introduction

As part of the HNG Backend Stage 0 challenge, I built a RESTful API using Express.js and TypeScript that returns my profile information together with a random cat fact fetched dynamically from the Cat Facts API.

This project helped me refine key backend concepts like API consumption, structured JSON responses, and handling asynchronous requests gracefully.


Project Overview

The main goal was to create a GET /me endpoint that outputs the following structure:

{
  "status": "success",
  "user": {
    "email": "your.email@example.com",
    "name": "Your Full Name",
    "stack": "Node.js/Express"
  },
  "timestamp": "2025-10-16T12:34:56.789Z",
  "fact": "A random cat fact from Cat Facts API"
}
Enter fullscreen mode Exit fullscreen mode

This JSON response includes my details, a dynamically generated timestamp (in UTC ISO 8601 format), and a random cat fact fetched from an external API.


Tech Stack

Here’s what I used to implement the project:

  • Runtime: Node.js
  • Language: TypeScript
  • Framework: Express.js
  • HTTP Client: Axios
  • Additional Libraries: CORS, Dotenv

Implementation

Here’s the simplified version of the /me endpoint:

import express, { Request, Response } from 'express';
import axios from 'axios';
import dotenv from 'dotenv';

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

app.use((req, res, next) => {
console.log([${new Date().toISOString()}] ${req.method} ${req.url});
next();
});

app.get('/me', async (req: Request, res: Response) => {
const timestamp = new Date().toISOString();

let catFact = '';
try {
const { data } = await axios.get('https://catfact.ninja/fact', { timeout: 5000 });
catFact = data.fact;
} catch {
catFact = 'Could not fetch a cat fact at this moment.';
}

res.status(200).json({
status: 'success',
user: {
email: process.env.USER_EMAIL!,
name: process.env.USER_NAME!,
stack: process.env.USER_STACK!,
},
timestamp,
fact: catFact,
});
});

app.listen(PORT, () => {
console.log(Server running on port ${PORT});
});
Enter fullscreen mode Exit fullscreen mode

Testing the API

You can run and test the API locally by executing:

npm run dev
Enter fullscreen mode Exit fullscreen mode

Then, use curl or Postman:

curl http://localhost:3000/me
Enter fullscreen mode Exit fullscreen mode

Expected output:

PostMan Output

Deployment

I deployed using PXXL App, a free hosting platform. You can check out the live link here Api Url


What I Learned

This task helped me strengthen:

  • Error handling: ensuring graceful fallbacks on API failure
  • Dynamic data generation: timestamps and per-request responses
  • Environmental configurations: protecting sensitive data
  • Code readability: through TypeScript interfaces and module separation

Conclusion

The Stage 0 backend task was a straightforward yet meaningful challenge. It blends creativity with structured engineering principles.

I’m excited for the next HNG stage and the opportunity to work with even more complex integrations.


Connect with Me

That's it brethren.Thanks for reading! If you’re also working on HNG tasks or even any project drop your comment below, let’s connect and grow together


Top comments (0)