How it works · web framework for Python APIs
How FastAPI works
You write a function with type hints. FastAPI reads the hints to check the request, convert the values and write the docs.
Think of it like this. A form that builds itself from a job description: the same description checks what people fill in.
- 🧱 The app The object you create. It is a Starlette app with FastAPI’s routing on top. fastapi/applications.py
- 🧭 Routing Each @app.get wraps your function in a route that knows how to feed it. fastapi/routing.py
- 🔎 Dependencies Reads your function’s parameters and works out where each value comes from. fastapi/dependencies/utils.py
- 🏷️ Parameters Query, Path, Body, Header, Depends: ways to say where a value lives. fastapi/params.py
- 📚 The docs Builds the OpenAPI description of your API, and the page at /docs. fastapi/openapi/utils.py
Follow one thing through: GET /items/7?q=tea reaches read_item(item_id: int, q: str)
- When the app starts, your function’s signature is read once: item_id is in the path, q is in the query.fastapi/dependencies/utils.py
- A request arrives through the server, and the router finds the route whose path matches.fastapi/routing.py
- Each value is pulled from where it lives, and "7" is checked and turned into the number 7.fastapi/dependencies/utils.py
- If a value is missing or the wrong type, a clear error goes back and your function is never called.
- Your function runs. A plain def runs in a worker thread, so it can’t block everyone else.fastapi/routing.py
- What you return is turned into JSON and sent.fastapi/encoders.py
The clever bit: One source of truth
In most frameworks you write the handler, then the validation, then the documentation, and they drift apart. FastAPI reads all three from your function’s signature: item_id: int means a path value, checked to be a whole number, shown as an integer in the docs. Change the type hint and the checking and the docs change with it.
Words you’ll meet
- type hint
- A note on a parameter saying what kind of value it should be, like
item_id: int. - dependency
- Something your function needs (a database, the current user) that FastAPI fetches for it.
- Pydantic
- The library that checks and converts the values.
- ASGI
- The agreed way an async Python server hands requests to an app.
Good questions to ask next
- When should I write async def?
- How does Depends work?
- Where does the /docs page come from?
Open FastAPI in Bento and ask them there. It reads the project in your browser, draws its parts as a map you can press, and opens the real code behind each one.
Take FastAPI apart yourself.
Free and open source. Nothing to install, no account.
More projects, taken apart
Written by hand from the code as it was on 2026-12-03 (commit 8e36534). FastAPI belongs to its authors; this page is an independent explanation. Spot a mistake? Tell us.