← back to books & articles
FastAPIPythonBackend

My FastAPI Learning Journey

January 1, 2024•6 min read

Django taught me how a web application is organised. FastAPI is what I reach for now, and the reasons turned out to be less about speed than I expected.

What Django gave me

I don't want to be unfair to the framework I learned on. Django handed me:

  • an ORM and migrations that just worked
  • an admin panel I got for free
  • authentication I didn't have to design
  • a strong opinion about where every file goes, which is exactly what a beginner needs

For a server-rendered product with forms and an admin, I'd still pick it.

Why I moved

My project stopped being a website with pages and became a JSON API with a separate frontend. That changes what you want from a framework.

Types that do the work

In FastAPI, the function signature *is* the contract:

class SignupRequest(BaseModel):
    email: EmailStr
    username: str
    password: str

@router.post("/signup", status_code=201)
async def signup(body: SignupRequest) -> TokenResponse:
    ...

Parsing, validation, coercion, and the 422 response with a precise error path - all of that comes from the type annotation. I deleted a genuinely embarrassing amount of hand-written "is this field present and a string" code.

Docs I didn't have to write

Every endpoint appears at /docs as an interactive form. I can click a button and hit my own API. When I'm wiring up the frontend, that page is my source of truth instead of a stale README.

async without ceremony

async def is the default posture, not an add-on. For an API that spends most of its life waiting on the database and third-party services, that matters.

Dependency injection as a habit

@router.get("/me")
async def me(user: User = Depends(get_current_user)) -> UserResponse:
    return user

"This endpoint needs a logged-in user" becomes one parameter. Auth stops being a decorator I copy-paste and becomes a dependency I declare - and swap for a fake one in tests.

What I had to give up

Honest accounting - the switch cost me things:

  • No admin panel. If you rely on Django admin to poke at data, you'll miss it immediately.
  • No batteries-included auth. I hashed my own passwords and issued my own JWTs. Educational, and also a foot-gun if you're careless.
  • You choose your own structure. FastAPI has no opinion about your folder layout, which is freedom right up until the project grows.
  • Async is a discipline. One blocking call inside an async endpoint stalls the event loop, and the symptom - "everything got slow" - points nowhere useful.

What I'd tell myself at the start

  1. Let Pydantic own the boundary. Validate at the edge, and let the inside of your app assume clean data.
  2. Separate schemas from models. The database row and the API response are different shapes, and conflating them leaks fields you didn't mean to expose.
  3. Push everything reusable into a dependency. Current user, DB session, settings.
  4. Read the errors FastAPI gives you. A 422 tells you the exact field and why. That's a gift, not a rejection.
  5. Keep async pure. If a library is synchronous, either find the async one or push the call off the event loop.

Where I landed

Django taught me structure. FastAPI taught me that a type annotation can be documentation, validation, and an editor autocomplete all at once.

Different tools, and the switch was worth it - for *this* kind of project.