Simple RESTful API endpoint that returns your profile information along with a dynamic cat fact fetched from an external API.
Introduction
As part of the HNG Internship Backend track Stage 0 Task, I was challenged to build a simple yet dynamic RESTful API that returns my profile details along with a live cat fact fetched from the Cat Facts API.
This task has helped me sharpen key backend development concepts — from API integration, response structuring, time formatting, to error handling and deployment.
Task Requirements
The goal was to create a GET /me
endpoint that returns:
{
"status": "success",
"user": {
"email": "johndoe@gmail.com",
"name": "John Doe",
"stack": "Python/FastAPI"
},
"timestamp": "2025-10-17T12:34:56.789Z",
"fact": "Cats sleep for 70% of their lives."
}
The cat fact must come dynamically from the public API
Tools & Technologies
Framework: FastAPI
Language: Python 3
Deployment Platform: Railway
My Implementation Process
- Project Setup
I created a new FastAPI project and installed dependencies:
pip install fastapi uvicorn requests python-dotenv
- Defined Schema with Pydantic.
To maintain clean and validated responses, I created a schema.py:
from datetime import datetime
from pydantic import BaseModel, EmailStr
class User(BaseModel):
"""Model for the response"""
email: EmailStr
name:str
stack: str
class Profile(BaseModel):
status: str
user: User
timestamp: datetime
fact: str
- Get cat facts
To get the cat facts from the Cat Fact API, i created the catfact.py
import requests
def cat_fact(url: str) -> str:
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
response_dict = response.json()
fact = response_dict["fact"]
except Exception:
fact = "Could not fetch cat fact at this time."
return fact
- Built the /me
Endpoint
from datetime import datetime, timezone
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.schema import Profile
from app.catfact import cat_fact
app = FastAPI()
URL = "https://catfact.ninja/fact"
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
my_info = {
"status": "Success",
"user": {
"email": "johnteclaire@gmail.com",
"name": "John Charles Otienoh",
"stack": "Python/fastAPI"
},
"timestamp": datetime.now(timezone.utc).isoformat(),
"fact": ""
}
@app.get("/me", response_model=Profile)
async def get_my_info():
"Retreive Basic information"
my_info['fact'] = cat_fact(URL)
return my_info
- Tested the API
I used cURL to test:
curl http://localhost:8000/me
The API returned a live cat fact and a real-time timestamp every request!
What I Learned
- How to consume external APIs safely using requests
- How to handle timeouts, errors, and fallbacks
- How to return structured JSON with Pydantic models
- The importance of clean documentation and response validation
- How to think about API reliability and best practices (timeouts, CORS, content-type headers, etc.)
Project Links
- GitHub Repo: https://github.com/john-otienoh/Me_API
- Live API: https://meapi-production.up.railway.app/me
- Contact: johnteclaire@gmail.com
Top comments (0)