FastAPI Tutorial with Examples.
FastAPI tutorial for production: async PostgreSQL with connection pools, measured sync vs async throughput, and Docker plus Kubernetes deployment.
Beginner
Start here — no prior experience needed
FastAPI Introduction: Your First API with Uvicorn
Install FastAPI, write a first app, run it with Uvicorn and call it with curl: real JSON responses, 404 and 405 errors, /docs, and --reload.
FastAPI HTTP Methods and Status Codes
GET, POST, PUT, PATCH and DELETE on an in-memory FastAPI store, with real curl output for 200, 201, 204, 404, 405, 422 and an unhandled 500.
FastAPI Path and Query Parameters
How FastAPI converts path and query parameters from type hints: defaults, optional values, bool parsing, Enum, Query and Path limits, and real 422 bodies.
FastAPI Request Bodies and Response Models
Read JSON request bodies into Pydantic models, see the real 422 errors, and use response_model to stop a password field leaking out of a FastAPI API.
FastAPI Headers and Cookies
Read request headers with Header(), set response headers, and set, read and delete cookies in FastAPI, with real curl -i output and a curl cookie jar.
FastAPI Error Handling: HTTPException and Custom Handlers
Raise HTTPException with details and headers, map your own exceptions to responses, reshape FastAPI's 422, catch crashes, and test errors with TestClient.
Intermediate
For developers with core concepts down
FastAPI Pydantic Validation: Field Constraints and Nested Models
Required vs optional fields, Field constraints, nested models, extra='forbid' and strict mode in FastAPI, each shown with the real 422 body it produces.
FastAPI Validators and Serialization with Pydantic
Custom checks with field_validator and model_validator, then control the JSON out with model_dump, camelCase aliases, serializers and computed fields.
FastAPI CRUD with SQLModel and SQLite
Store FastAPI data in SQLite with SQLModel: a yield session dependency, separate create/public/update models, CRUD over curl and a 404 for missing rows.
FastAPI Database Migrations with Alembic and SQLModel
Alembic init, revision --autogenerate, upgrade, downgrade and current output for a SQLModel app, plus the autogenerate pitfalls that break or lose data.
FastAPI Authentication: Password Hashing, OAuth2 and JWT
Hash passwords with pwdlib and Argon2, log in through FastAPI's OAuth2 password flow, issue JWTs, and see wrong, expired and tampered tokens get 401s.
FastAPI Roles, CORS and Settings from Environment Variables
Guard FastAPI routes with role dependencies, run real CORS preflights that pass and fail, and load config from env vars and .env with pydantic-settings.
FastAPI Dependency Injection: Depends, yield and Overrides
Share logic with Depends, chain sub-dependencies, watch per-request caching, write yield dependencies that re-raise, and swap any of them with overrides.
Testing FastAPI with pytest, TestClient and httpx
Test FastAPI with pytest: TestClient, fixtures that swap in a test database via dependency_overrides, async tests over ASGITransport, and real failures.
FastAPI with Async PostgreSQL: Pools, Transactions, and Real Errors
Connect FastAPI to PostgreSQL with an asyncpg pool: startup lifespan, 422 validation, 409s from constraints, rollback-safe orders, and pool queuing.
Sync vs Async Endpoints in FastAPI, Measured
Benchmark def vs async def FastAPI routes with ApacheBench: the 40-thread pool, a blocking call that stalls every route, CPU-bound work, and --workers.
Advanced
Production-grade patterns for experienced engineers
FastAPI Background Tasks and Middleware
FastAPI BackgroundTasks and middleware, measured: the response beats the task, a failing task, request-ID headers, middleware order, and pure ASGI.
FastAPI File Uploads, Streaming and WebSockets
FastAPI files, streaming and WebSockets, run for real: UploadFile with a 415 for spoofed types, FileResponse headers, timed chunks, and a WebSocket echo.
Deploying FastAPI with Docker and Kubernetes: Images, Signals, and Probes
Ship FastAPI on Docker and Kubernetes: a 171 MB multi-stage image, clean SIGTERM shutdown, and liveness, readiness and startup probes on a live minikube.
Also part of FastAPI
Taught in other topics. Each link opens the lesson in its home topic.
DDL: Creating and Changing Tables
Learn SQL DDL on real PostgreSQL output: CREATE TABLE constraints, ALTER TABLE pitfalls, DROP with dependencies, TRUNCATE, schemas, generated columns.
SELECT Queries
Master SQL SELECT on real PostgreSQL output: columns, aliases, DISTINCT, WHERE, the NULL comparison trap, ORDER BY, LIMIT/OFFSET, and computed columns.
SQL JOINs
Learn SQL JOINs on real PostgreSQL output: INNER, LEFT, RIGHT, FULL, CROSS and self-joins, anti-joins, and the WHERE that breaks a LEFT JOIN.
Pytest Fixtures
Replace repeated setup with fixtures, control how often they run with scope, and watch teardown happen after a test fails.
Introduction to Docker
Run your first Docker containers and see what they are: ordinary Linux processes sharing the host kernel, started from an image in well under a second.
Multi-Stage Builds
Use multi-stage Dockerfiles to produce small, secure production images by separating build and runtime environments.