DEV Community

Cover image for Building a RESTful API with Real-Time Data β€” My HNG Internship Project πŸš€
Muazu Idris Y.
Muazu Idris Y.

Posted on

Building a RESTful API with Real-Time Data β€” My HNG Internship Project πŸš€

During the HNG Internship (Stage 0), I built a dynamic RESTful API that integrates with an external service, handles errors gracefully, and returns real-time data using Node.js and Express.

πŸ”— Live Demo: https://hng-be-s0-production.up.railway.app/me

πŸ’» Source Code: https://github.com/Imuaz/HNG-BE-S0


🧩 The Challenge

The goal was to create an endpoint that:

  • Returns my profile (email, name, tech stack)
  • Fetches a random cat fact from an external API
  • Generates a dynamic UTC timestamp
  • Handles failures gracefully
  • Follows a specific JSON format

Example response:

{
  "status": "success",
  "user": {
    "email": "your.email@example.com",
    "name": "Your Full Name",
    "stack": "Node.js/Express"
  },
  "timestamp": "2025-10-17T14:23:45.678Z",
  "fact": "Cats sleep 70% of their lives."
}
Enter fullscreen mode Exit fullscreen mode

Sounds simple, right? But there's more beneath the surface!

βš™οΈ Tech Stack

  • Node.js + Express.js
  • Axios – for API calls
  • dotenv – for environment variables
  • CORS middleware
  • Railway – for deployment
  • Git & GitHub – for version control

🧠 Implementation Highlights

Project Structure

src/
 β”œβ”€β”€ routes/profile.js
 └── services/catFactService.js
server.js
Enter fullscreen mode Exit fullscreen mode
  • server.js β†’ Initializes Express, handles middleware & routes
  • catFactService.js β†’ Fetches random cat facts with timeout protection
  • profile.js β†’ Returns user info, timestamp, and a cat fact

Key Features

  • βœ… Modular architecture
  • βœ… Comprehensive error handling
  • βœ… Timeout to prevent hanging requests
  • βœ… Dynamic timestamps (always up to date)

πŸš€ Deployment

  1. Create a .env file with the following variables:
USER_EMAIL=your.email@example.com
USER_NAME=Your Name
USER_STACK=Node.js/Express
Enter fullscreen mode Exit fullscreen mode
  1. Push your code to GitHub
  2. Deploy on Railway
  3. Add environment variables in the Railway dashboard

5. Visit the /me endpoint to test the live API

🧩 Challenges & Lessons

  1. External API failures

    β†’ Solved using Axios timeout + fallback handling

  2. Dynamic timestamps

    β†’ Generated fresh ISO 8601 timestamps per request

  3. Environment setup

    β†’ Used .env for sensitive config, excluded via .gitignore

  4. Deployment

    β†’ Switched to Railway after Render/Vercel restrictions


🧠 Key Takeaways

  • Always handle API failures gracefully
  • Keep your code modular and testable
  • .env files are your friend for managing config
  • Learn to deploy β€” DevOps is part of the job!
  • Testing endpoints early saves hours later

🎯 Conclusion

This project taught me how to design, test, and deploy a real-world API.

If you’re starting with backend development or participating in the HNG Internship, building a project like this is an amazing learning experience.


πŸ‘‰ Check It Out


🌐 Connect With Me

Top comments (0)