DEV Community

Francesca Valli
Francesca Valli

Posted on

Incredibile: era davanti agli occhi e nessuno lo ha notato per mesi.

Day 1 of my internship. The task seemed simple: "Build an API endpoint that returns your profile and a cat fact."
How hard could it be? Spoiler: I learned more from this "simple" task than from watching 10 hours of tutorials.

The Challenge
Build a /me endpoint that:

Returns my profile info
Fetches random cat facts from an external API
Updates timestamp on every request
Doesn't crash when things go wrong

My Stack: Python + FastAPI
The "Aha!" Moments

External APIs Are Unreliable (And That's Normal) My first version looked like this: python@app.get("/me") async def get_profile(): response = await client.get("https://catfact.ninja/fact") cat_fact = response.json()["fact"] return {"fact": cat_fact, ...} What could go wrong?
API is down → My app crashes
API is slow → User waits forever
API returns weird data → App explodes

The fix: Timeouts + error handling + fallbacks
pythonasync def fetch_cat_fact():
try:
async with httpx.AsyncClient(timeout=5.0) as client:
response = await client.get(CAT_FACT_API_URL)
return response.json()["fact"]
except:
return "Cat facts temporarily unavailable"

Lesson learned: Always plan for failure. Production apps need to be resilient, not just functional.

Environment Variables Aren't Just for Secrets I hardcoded my email in the code at first. Then I realized:
What if I want to change it?
What if someone else uses my code?
What about deployment?

Created .env for my actual data:
bashUSER_EMAIL=john@example.com
USER_NAME=John Doe
Created .env.example for the template:
bashUSER_EMAIL=your.email@example.com
USER_NAME=Your Name

Lesson learned: Separate configuration from code. Future you will thank present you.

FastAPI's Auto-Docs Saved Me Hours
Hit localhost:8000/docs and boom.....interactive API documentation, automatically generated. I could test everything right in the browser without writing a single line of test code.

Lesson learned: Choose tools that give you superpowers for free.

The Final Result
Live API: [https://profile-api-production-f700.up.railway.app/me]
json{
"status": "success",
"user": {
"email": "john@example.com",
"name": "John Doe",
"stack": "Python/FastAPI"
},
"timestamp": "2025-10-17T14:30:45.123456+00:00",
"fact": "Cats sleep 70% of their lives."
}
Every request fetches a new cat fact. Every timestamp is fresh. And when the cat API goes down? My API keeps working.

Top comments (0)