DEV Community

Musungu (Ruth) Ambogo
Musungu (Ruth) Ambogo

Posted on

Building a Simple RESTful API Endpoint with Node.js and Express

I built a simple REST API that returns user information along with a random cat fact fetched from an external API. This is how I developed it.

The project was part of the HNG Internship Stage 0 backend task. We were not restricted to any specific tech stack, so I chose Node.js, Express.js, and Axios, three powerful tools for building and consuming APIs.

What This API Does

The endpoint /me returns a JSON response that includes:

  • My user details (email, full name, and tech stack)

  • The current UTC timestamp

  • A random cat fact fetched dynamically from the Cat Facts API

Here’s the structure of the response:

{
  "status": "success",
  "user": {
    "email": "ruthambogo.ra@gmail.com",
    "name": "Musungu Ruth Ambogo",
    "stack": "Node.js/Express"
  },
  "timestamp": "2025-10-16T12:54:25.659Z",
  "fact": "Every year, nearly four million cats are eaten in Asia."
}
Enter fullscreen mode Exit fullscreen mode

Setting Up the Project

To start, I created a new folder and initialized it with






```mkdir Dynamic-profile-api
cd Dynamic-profile-api
npm init -y
Enter fullscreen mode Exit fullscreen mode

Then I installed the necessary dependencies:

npm install express axios

Here’s what each package does:

  • Express → helps create the server and handle routes

  • Axios → makes HTTP requests to external APIs

Building the /me Endpoint

After setting up, I created an index.js file and wrote the code below:

const express = require("express");
const axios = require("axios");
const cors = require("cors");

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

app.use(cors());

const user = {
  email: "ruthambogo.ra@gmail.com",
  name: "Musungu Ruth Ambogo",
  stack: "Node.js/Express"
};

app.get("/me", async (req, res) => {
  try {
    const response = await axios.get("https://catfact.ninja/fact", { timeout: 5000 });
    const fact = response.data.fact;

    res.status(200).json({
      status: "success",
      user,
      timestamp: new Date().toISOString(),
      fact
    });
  } catch (error) {
    console.error("Error fetching cat fact:", error.message);
    res.status(500).json({
      status: "error",
      user,
      timestamp: new Date().toISOString(),
      fact: "Could not fetch a cat fact at this time. Please try again later."
    });
  }
});

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

Testing the API

I ran the server using:

node index.js

Then visited http://localhost:3000/me
in my browser.

Each time I refreshed the page, I got a new cat fact, confirming that the API was successfully fetching dynamic data.

Deployment

After building my API locally and confirming that it worked correctly, the next step was to deploy it online so others could access it.

Steps I Followed

  1. Pushed the Code to GitHub I first made sure all my project files were committed and pushed to GitHub:

git add .
git commit -m "Initial commit"
git push origin main

  1. Connected GitHub Repo to Railway:
  2. I logged into Railway using my GitHub account.
  • Clicked on "New Project" → "Deploy from GitHub Repo".

  • Selected my repository (Dynamic-profile-api).

  • Railway automatically detected that it was a Node.js app.

  1. Clicked “Deploy” Railway built and deployed my API in just a few minutes. After deployment, it generated a public URL that allowed access to my /me endpoint.

🌐 Example:

If my API was deployed at

https://dynamic-profile-api-production-522c.up.railway.app/me

I can test it by visiting the link in my browser or using a tool like Postman.

What I Learned

This project may look simple, but it taught me several essential backend development concepts, including:

  • How to build RESTful APIs using Express.js

  • How to consume external APIs with Axios

  • Handling asynchronous code using async/await

  • Error handling — providing meaningful fallback responses when an external API fails

  • Returning dynamic data (timestamp and random facts)

  • The importance of response consistency and proper JSON formatting

It also helped me appreciate how backend services communicate, and how small details like response structure, status codes, and headers make a big difference in API design.

Conclusion

Building this RESTful API was a fun and insightful experience. It strengthened my confidence in creating backend applications and handling third-party integrations.

I’m grateful to the HNG Internship for pushing me to build something practical — this task reminded me that even simple projects can teach powerful lessons.

If you’re learning backend development, I encourage you to try building your own version of this API. It’s a great starting point to understand how APIs work, how to fetch data from other services, and how to structure clean JSON responses.

Top comments (0)