DEV Community

Cover image for 📝 3 FastAPI Mistakes I Made (So You Don't Have To)
EZENWA CHRISTIAN CHIMEREMEZE
EZENWA CHRISTIAN CHIMEREMEZE

Posted on

📝 3 FastAPI Mistakes I Made (So You Don't Have To)

When I built my first production backend with FastAPI, I thought I was doing everything right. The code ran locally, the endpoints returned 200 OK, and the server started up without any errors.

But when I deployed it, things broke. FastAPI is incredibly powerful, but its async nature means it's easy to shoot yourself in the foot.

Here are three mistakes I made(and how you can avoid them).

Mistake 1: Using time.sleep() instead of asyncio.sleep()

This was my biggest mistake. I was writing an endpoint that had to wait for an external API to respond, so I used time.sleep(2).

What happened? The entire server froze. FastAPI is built on asynchronous Python. When you use blocking code like time.sleep(), it blocks the event loop, meaning no other user can make a request to your API while that one request is waiting.

The Fix:
Always use asyncio.sleep() inside an async def route. Better yet, use an async HTTP client like httpx instead of the standard requests library.

import asyncio
import httpx

@app.get("/external-data")
async def get_data():
    # Use async HTTP requests
    async with httpx.AsyncClient() as client:
        response = await client.get("https://api.example.com")

    # Use asyncio.sleep, not time.sleep
    await asyncio.sleep(1) 

    return response.json()
Enter fullscreen mode Exit fullscreen mode

Mistake 2: Not using Pydantic for Output Validation

When I first started, I just returned raw dictionaries from my endpoints.

# Don't do this
@app.get("/user")
def get_user():
    return {"name": "Ezenwa", "age": 25, "password": "secret123"}
Enter fullscreen mode Exit fullscreen mode

The problem is that you might accidentally leak sensitive data (like passwords) or return the wrong data types, which breaks your frontend.

The Fix:
Create a Pydantic response_model to strictly define what the client receives. This strips out any extra data and validates the output automatically.

from pydantic import BaseModel

class UserResponse(BaseModel):
    name: str
    age: int

@app.get("/user", response_model=UserResponse)
def get_user():
    # Even if you return more data, FastAPI will only send name and age
    return {"name": "Ezenwa", "age": 25, "password": "secret123"} 
Enter fullscreen mode Exit fullscreen mode

Mistake 3: Hardcoding Secrets

It is so tempting to just write SECRET_KEY = "my_super_secret_key_123" directly in your code. But if you push that to GitHub, bots will scrape it in seconds.

The Fix:
Use a .env file and the python-dotenv library.

pip install python-dotenv
Enter fullscreen mode Exit fullscreen mode

Then load it in your app:

import os
from dotenv import load_dotenv

load_dotenv()
SECRET_KEY = os.getenv("SECRET_KEY")
Enter fullscreen mode Exit fullscreen mode

Conclusion

FastAPI is fast, but you have to respect its async rules and strict typing. If you avoid blocking the event loop and use Pydantic to validate both inputs and outputs, your API will be significantly more stable.

Have you made any of these mistakes? Let me know in the comments!

Top comments (0)