Python FastAPI
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. The reference material consulted while preparing these pages is listed in the section bibliography. |
This section is written against FastAPI 0.142.2 (released 2026-09-30), Pydantic 2.13 and Starlette 1.7, checked on 2026-10-02. FastAPI supports Python 3.10 through 3.14. FastAPI is still a 0.x project, so every page notes the version in which a feature appeared and the guide recommends pinning the version you deploy.
What FastAPI is
FastAPI is a Python web framework for building HTTP APIs. It is deliberately thin: Starlette provides the ASGI toolkit (routing, middleware, requests and responses, WebSockets, background tasks, the test client), Pydantic provides validation, serialization and JSON Schema, and FastAPI adds what ties them together: parameters declared with ordinary Python type hints, a dependency injection system, and automatic generation of an OpenAPI 3.1 description with interactive documentation (Swagger UI and ReDoc).
The practical result is that one declaration does several jobs at once. A function signature such as
def read_book(book_id: int) converts the path value to an integer, rejects bad input with a structured 422
error, documents the parameter in /docs, and gives your editor full autocompletion.
This section is for developers who:
-
know Python (or are coming from Flask, Django, Spring Boot, ASP.NET Core or Express) and want to build and run production-grade JSON APIs
-
want one coherent path from a first endpoint to testing, security, data access, deployment and observability
-
need the current idioms (
Annotated, Pydantic v2, lifespan, thefastapiCLI, PyJWT and pwdlib) rather than the patterns found in older tutorials
The Bookshelf API scenario
Every example builds one small original service, the Bookshelf API, so the pages fit together instead of being isolated snippets:
-
books,authorsandreviewsresources with full CRUD -
users with OAuth2 password login, JWT access tokens and scopes
-
cover-image uploads
-
a live "new review" feed over Server-Sent Events
-
a WebSocket chat per book
-
a Jinja and single-page-application front end
-
PostgreSQL through SQLModel in production and SQLite in tests
The final project layout (app/main.py, app/routers/, app/models/, app/services/, app/db.py,
app/settings.py and tests/) is assembled on
Project Structure & Layered Architecture.
Reading path
The pages are ordered so that each one builds on the previous ones. Read them in this order the first time, then use them as a reference:
-
Foundations: what FastAPI is made of, a first running app, the Python typing and async concepts it relies on.
-
Request data: path and query parameters, bodies and Pydantic models, headers, cookies, forms and uploads.
-
Responses and errors: response models, status codes, error handling, custom and streamed responses.
-
Dependencies: dependency injection and dependencies with
yield. -
Security: authentication fundamentals, OAuth2 with JWT, scopes, CORS and proxy headers.
-
Data and application structure: SQL with SQLModel, other data stores, layered project layout, settings and lifespan.
-
OpenAPI: the generated contract and how to shape it.
-
Real-time and web: WebSockets, static files, templates, front ends, sub-applications.
-
Testing:
TestClient, dependency overrides, property-based and load testing. -
Production: servers and workers, containers, performance and observability.
-
Beyond the basics: serving data-science and AI workloads, and what changed between versions.
The cheat sheet at FastAPI Cheat Sheet condenses the whole section onto one page for quick lookup.
What’s covered
Where related material lives elsewhere on this site
The FastAPI pages link to existing material instead of repeating it, and cover only what FastAPI adds.
| Topic | Where it lives | How the FastAPI section relates |
|---|---|---|
The Python language, typing, asyncio, virtual environments, pytest |
Python Reference, including Type Hints, Concurrency & Async, Virtual Environments & Packaging and Testing |
FastAPI pages link here for general Python and cover only what FastAPI adds. |
GraphQL with Python |
Python: Getting Started, Strawberry with FastAPI and Ariadne: Schema-First GraphQL |
The sub-applications page links here and does not repeat GraphQL. |
OAuth 2.0, OpenID Connect, JWT theory |
The security pages cover only FastAPI’s utilities ( |
|
Docker and containers |
The Docker page shows only the FastAPI-specific Dockerfile and Compose files. |
|
Cloud deployment |
The deployment page points to the managed container services described there. |
|
Database engines |
SQL Reference, MongoDB Reference, Redis Reference, Elasticsearch Reference and Vector Databases & RAG |
The data pages cover only how to use each from FastAPI (sessions and clients as dependencies, lifespan-managed clients). |
Architecture |
Architecture, Architectural Patterns and Starting a New Project: Which Path and Which Technologies |
The project-structure page links the patterns (hexagonal, repository, domain-driven design). |
REST APIs in other stacks |
REST APIs with Spring MVC and WebFlux, Quarkus Reference and Minimal APIs |
The introduction’s framework comparison presents these as the same idea in other stacks. |
Messaging |
The background-tasks page points here for work that outgrows |
|
Kubernetes |
The Docker & Kubernetes page shows only the FastAPI-specific Deployment, probes and graceful shutdown. |
|
AI frameworks and models |
The data-science and AI serving page stays introductory and points here for depth. |
Related topics that are planned but not yet part of this site (a programming-languages comparison and API gateways) are mentioned in prose only, where relevant.
Bibliography
Every source used to prepare this section is listed here, each linked to its official site. Individual pages
list the subset they derive from under their own == References heading.
Book
-
Lubanovic, B. FastAPI: Modern Python Web Development. O’Reilly Media, 2023. ISBN 978-1-098-13550-8. O’Reilly publisher page; companion code: madscheme/fastapi on GitHub