Automatic OpenAPI docs generation (Swagger/Redoc), typing with Pydantic, async support out of the box, and auto-documentation based on types.
Pydantic is a data validation library based on type annotations. FastAPI uses it to automatically validate requests/responses and generate schemas.
APIRouter is a module for grouping routes (by API version, by module). FastAPI() is the main application class with all middleware and mounting.
Dependencies are declared as function parameters. FastAPI automatically calls them and passes the result into the route. It supports caching (@cache), state, and DB sessions.
async def — for I/O operations (DB, Redis, external APIs). def — for CPU-bound tasks or when there's no await inside.
BackgroundTasks — lightweight tasks within an HTTP request (notifications). Celery — heavy background tasks with queues and workers.
StreamingResponse lets you send data in chunks (SSE, files, generators). Useful for large files or real-time data.
CORSMiddleware with origins, methods, and headers settings. Allows everything by default — unsafe for production.
Use slowapi (backed by Redis) or slowapi_limiter. Apply the @limiter.limit('5/minute') decorator on routes.
The @app.middleware('http') decorator gives access to Request/Response via call_next(request).
Use OAuth2PasswordBearer from fastapi.security. Dependencies check the token in the Authorization header.
BaseModel is the base class for models. Field() sets additional parameters (default, description, examples, validators).
Model configuration: from_attributes=True (Pydantic v2), json_encoders, validation_alias, and others.
A Pydantic model for the response. FastAPI automatically serializes it to JSON and generates a schema in OpenAPI.
Create an engine and SessionLocal. Dependencies for DB sessions: Depends(get_db). Alembic for migrations.
A hybrid of SQLAlchemy + Pydantic by the creator of FastAPI. Models work as both Pydantic and SQLAlchemy at the same time.
Use joinedload(), selectinload() in SQLAlchemy, or prefetch in SQLModel. Use select().offset().limit() instead of the ORM for reads.
Uvicorn is an ASGI server. Gunicorn manages processes, and Uvicorn workers provide multiprocessing (--workers 4).
Add prometheus-fastapi-instrumentator or prometheus-client. A /metrics route with Counter/Gauge/Histogram.
src/ (app/, core/, crud/, schemas/, models/), tests/, docker-compose.yml, .env, requirements/, Dockerfile.
Endpoints /health, /ready that check DB, Redis, and external services. Return 200 only if everything is OK.
WebSocket and WebSocketDisconnect. A ConnectionManager to manage active connections.
FastAPI-Ariadne or Strawberry for GraphQL. REST is simpler for CRUD, GraphQL is better for complex queries with filtering.
from fastapi.testclient import TestClient. client = TestClient(app). Full testing with DB/Redis mocks.