DEV Community

John Otienoh
John Otienoh

Posted on

Me-API

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."
}

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

- 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

Enter fullscreen mode Exit fullscreen mode

- 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

Enter fullscreen mode Exit fullscreen mode

- 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

Enter fullscreen mode Exit fullscreen mode

- Tested the API
I used cURL to test:

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

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

Top comments (0)