FastAPI in the Real World
FastAPI alone is a 30-line tutorial. What makes it a service is everything that wraps the framework.

FastAPI alone is a 30-line tutorial. What makes it a service is everything that wraps the framework.
What the framework gives you for free:
ASGI runtime, Pydantic validation, dependency injection, auto-generated OpenAPI. That's the part everyone shows in the README.
What you actually wire yourself in a real project:
1. App factory + `lifespan` instead of a module-global `app = FastAPI()`. Lets tests inject a tailored `Settings` and tears the engine down cleanly.
2. `pydantic-settings` for config. One `Settings(BaseSettings)` reads `APP_*` env vars and `.env`. Required secrets like `APP_DB_PASSWORD` refuse to start the app if missing. No more `os.getenv` scattered across files.
3. Async SQLAlchemy 2.0 + asyncpg. `AsyncSession` lives in a per-request dependency. Engine disposed on shutdown via `lifespan`.
4. Repository pattern. Routes stay HTTP-shaped; SQL lives in `BookRepository`. Domain errors like `BookNotFoundError` cross the boundary as typed exceptions, never `IntegrityError` leaks.
5. One error envelope. A single set of handlers maps everything to `{error, message, code, path, details}`. Validation returns 422, not 400.
6. Versioned API. Routes mounted under `/v1`. Cheap insurance for the day a breaking change is necessary.
7. Structured JSON logs configured in `lifespan`. Every log line is one JSON object โ a shipper can parse it without regex.
8. Pydantic schemas as the wire contract โ `BookCreate`, `BookReplace`, `BookPatch`, `BookOut`. OpenAPI generates itself from them; no hand-rolled spec.
9. Health endpoint that does a real `SELECT 1` through the same session the app uses. Catches DSN/permission issues during deploy, not in production.
10. 100% test coverage with no DB mocks. SQLite in-memory + dependency override; 71 tests in under a second. (Coming in part 3.)
What I left out on purpose: auth (depends on deployment shape), CORS (added when there's a real cross-origin frontend), background workers (separate concern).
This is the map. The next three posts zoom in: the libraries doing the heavy lifting, the test strategy, and an honest benchmark.
If you ship FastAPI in prod, what part of the "wraps the framework" list do you treat as non-negotiable?
P.S. Code: https://github.com/bilouro/FastAPIProject
P.S. New tech post every Wednesday.
#FastAPI #Python #BackendEngineering