DEV Community

Cover image for How I Built a Dynamic Profile API with Live Cat Facts
Tobiloba
Tobiloba

Posted on

How I Built a Dynamic Profile API with Live Cat Facts

When I saw the HNG Backend Stage 0 challenge build a /me endpoint that returns my profile plus a live cat fact,I thought: “Simple, right?”
Turns out, even “simple” APIs teach you big lessons about error handling, module systems, and the chaos of third-party dependencies. Here’s how I built it, what went wrong, and how I fixed it all in TypeScript + Express, deployed from my local machine.

🎯 Why This Task Matters

This isn’t just a JSON blob. It’s a microcosm of real backend work:

✅ Dynamic data: Fresh timestamp on every request
🌐 External API integration: Real-time cat facts from catfact.ninja
🛡️ Fault tolerance: Graceful fallbacks when APIs fail
🚪 Instant public access: No cloud deployment needed thanks to ngrok!

The Core Logic: /me Handler

The magic happens in under 30 lines. Key features:

  • Live timestamp (ISO 8601 UTC)
  • Fresh cat fact per request (with 5s timeout)
  • Fallback fact if the cat API flakes out

The code

🚧 The Module System Trap (ESM vs CommonJS)

My biggest headache? This runtime crash:

The error i faced

Cause: I used ESM-style named imports with a CommonJS package (express):

What caused it

Fix: Use type-only aliases (zero runtime cost):

The solution

Lesson: TypeScript compiles ≠ Node.js runs. Always test with tsx or compiled JS!

🌐 Deployment? Meet ngrok.

Since cloud platforms like Vercel were off-limits, I went local first:

  1. Ran my Express server: npm run dev → http://localhost:3000
  2. Fired up ngrok: ngrok http 3000
  3. Got an instant public URL: https://f8ffe71c697b.ngrok-free.app

✅ No Docker
✅ No CI/CD
✅ No waiting for builds

Just pure, tunnelled, localhost magic.

Ngrok terminal

the url in the browser

🔑 Key Takeaways

  1. Local can be public ngrok turns localhost into a shareable URL in seconds—perfect for demos, testing, and challenges like this.
  2. Third-party APIs will fail Always code for failure. A fallback fact kept my endpoint alive during catfact.ninja outages.
  3. ESM + CommonJS = handle with care Use type aliases for Express types in ESM projects. Avoid runtime destructuring.
  4. Small tasks, big insights This “simple” endpoint taught me about timeouts, env vars, module systems, and resilient design.

📦 Try It Yourself!

👋 Final Thought

You don’t need a server farm to build something useful. With TypeScript, Express, and ngrok, your laptop becomes a global API endpoint.

And if nothing else,now you know that cats spend 70% of their lives sleeping. Go take a nap. You’ve earned it.

Built for Backend Wizards . Stage 0: complete.

Top comments (0)