Skip to content

fastapi

Use when working with FastAPI endpoints, Pydantic v2 models, dependency injection, middleware, background tasks, WebSockets, or streaming responses. Also use when testing FastAPI with httpx/TestClient, deploying with uvicorn, or debugging async route handling and OpenAPI schema generation.

ModelSource
sonnetpack: python
Full Reference

FastAPI is an async-first Python web framework built on Starlette and Pydantic v2. It auto-generates OpenAPI (Swagger) docs from type annotations. Pydantic v1 support is deprecated — use Pydantic v2 patterns throughout.

ItemValue
Installpip install fastapi uvicorn[standard]
Pydantic versionv2 — model_config = ConfigDict(from_attributes=True) (not orm_mode)
Dev serveruvicorn main:app --reload
Prod servergunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker
Docs URL/docs (Swagger) and /redoc
Background tasks limit30s max — use Celery/ARQ for longer work
I want to…File
Set up the app, CORS middleware, and lifespan hooksreference/app-setup.md
Define path operations (GET, POST, PUT, DELETE) with query/path paramsreference/path-operations.md
Write Pydantic v2 request/response models and validatorsreference/pydantic-models.md
Implement dependency injection for DB sessions and authreference/dependency-injection.md
Add streaming responses, WebSockets, background tasks, and file uploadsreference/advanced-features.md
Test FastAPI routes with httpx and pytest-asyncioreference/testing.md

Usage: Read the reference file matching your current task from the index above. Each file is self-contained with code examples and inline gotchas.


┏━ ⚡ fastapi ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Async Python API framework with Pydantic v2 and auto OpenAPI docs ┃ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛