This is the FastAPI textbook. Read in order for a full education, or jump to a chapter.
It pairs with the SQLAlchemy 2.0 textbook , the Postgres-focused SQLAlchemy textbook , the Pydantic v2 textbook , and the Alembic textbook .
This is the FastAPI textbook. Read in order for a full education, or jump to a chapter.
It pairs with the SQLAlchemy 2.0 textbook , the Postgres-focused SQLAlchemy textbook , the Pydantic v2 textbook , and the Alembic textbook .
FastAPI architecture: ASGI, Starlette, Pydantic, dependency injection, and the design choices that make it tick.
Path operations, parameter declaration (path / query / body / header / cookie / form / file), routers, prefixes, dependencies on routers, and request internals.
Request validation with Pydantic v2: BaseModel, Field, validators, custom types, error responses, nested models, discriminated unions, and the FastAPI integration.
Output side of FastAPI: response_model, response shaping, custom encoders, status codes, response classes (JSON, HTML, Streaming, File).
Dependency injection in FastAPI: Depends, scopes, sub-deps, class-based deps, lifecycle (startup/shutdown), and the patterns from production.
Auth and authz in FastAPI: passwords, JWT, OAuth2 / OIDC, sessions, passkeys, RBAC, scopes, and security best practices.
Concurrency in FastAPI: async vs sync handlers, the threadpool, AnyIO, blocking traps, structured concurrency, and how to scale.
Real-time FastAPI: WebSockets, broadcast / pub-sub, SSE for one-way streams, StreamingResponse, NDJSON, and disconnect handling.
Background tasks in FastAPI: BackgroundTasks for fire-and-forget, ARQ / Celery / Dramatiq for reliable jobs, scheduling, and long-running operations.
Testing FastAPI: TestClient and AsyncClient, dependency overrides, real DB testing with testcontainers, factory fixtures, and CI integration.