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 .β¦