- Python 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| examples | ||
| guide | ||
| tests | ||
| .gitignore | ||
| CHANGELOG.md | ||
| pyproject.toml | ||
| README.md | ||
| requirements-dev.txt | ||
| requirements.txt | ||
FastAPI Guide
A practical guide and reference for building backend APIs with FastAPI. It is short on theory and long on examples, and every example is a small app you can run.
It is written for engineers who know Python but are new to FastAPI, and possibly to backend work in general.
Scope. The guide covers the API only. The backend speaks HTTP and JSON, and the frontend, whatever it is, is a separate application that calls the API. Nothing here renders HTML or serves a frontend. Databases are SQLite for local development and MariaDB in production.
Versions
Checked against:
| Package | Version |
|---|---|
| Python | 3.11+ (developed on 3.14) |
| FastAPI | 0.163.2 |
| Starlette | 1.10.0 |
| Pydantic | 2.14.3 |
| pydantic-settings | 2.16.3 |
| Uvicorn | 0.54.0 |
Setup
PowerShell:
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -r requirements-dev.txt
bash:
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements-dev.txt
Running an example
Each chapter has an app in examples/. Run it from the repository root:
fastapi dev examples/ch01_first_app/main.py
Then open http://127.0.0.1:8000/docs to call the endpoints from the browser.
Apps that are a package (chapter 06 onward) are run from their own folder:
cd examples/ch06_structure
fastapi dev app/main.py
How to read it
Read guide/ in order the first time; each chapter builds on the previous one. After that, the reference pages are for looking things up quickly.
Requests and responses are written like this:
GET /items/5?q=abc -> 200 {"item_id": 5, "q": "abc"}
Contents
Guide
| # | Chapter | What it covers |
|---|---|---|
| 00 | How a backend API works | HTTP, methods, status codes, REST URLs, how a frontend uses the API |
| 01 | First app | install, run, automatic docs, def vs async def |
| 02 | Parameters | path, query, headers, cookies, body, forms, validation |
| 03 | Pydantic models | Create/Update/Read models, validators, PATCH, camelCase, field types |
| 04 | Responses and errors | status codes, headers, cookies, exceptions and handlers, error shapes |
| 05 | Dependencies | Depends, auth checks, yield cleanup, router-wide dependencies, overrides |
| 06 | Structuring an app | layout, routers, settings, lifespan, app factory |
| 07 | Databases | SQLAlchemy 2.0, sessions, CRUD, relationships, pagination, SQLite and MariaDB |
| 08 | Migrations | Alembic: autogenerate, upgrade, downgrade, data migrations |
| 09 | Sync vs async | when to use async def, blocking code, async database access |
| 10 | Authentication | API keys, password hashing, OAuth2 + JWT, roles, LDAP |
| 11 | Hardening | CORS, trusted hosts, rate limiting, running behind a proxy, secrets |
| 12 | Files | uploads, size limits, downloads, streaming large files |
| 13 | Background work | BackgroundTasks, scheduled jobs, when to use a real queue |
| 14 | Real-time | WebSockets, Server-Sent Events, streaming responses |
| 15 | Calling other services | httpx, timeouts, retries, mapping upstream errors |
| 16 | Logging and middleware | logging setup, request IDs, middleware, health checks |
| 17 | Performance | caching, compression, workers, finding slow endpoints |
| 18 | Deployment | Dockerfile, Compose with MariaDB, workers, reverse proxy |
| 19 | API design for frontends | versioning, pagination and error conventions, OpenAPI, typed clients |
| 20 | Testing | TestClient, overrides, a test database (short) |
Reference
One-page lookups: cheat sheet, parameters, Pydantic, responses and status codes, dependencies, SQLAlchemy queries, CLI, gotchas.
Capstone
An assignment: build the backend for a frontend that already exists or will be built separately, from a written brief and API contract. A reference solution is included to compare against.
Development
Every example is covered by a test in tests/, so the code in the guide is known to work.
python -m pytest
ruff check .
ruff format --check .