Agent skill · software engineering · affaan-m
fastapi-patterns
FastAPI patterns for async APIs, dependency injection, Pydantic request and response models, OpenAPI docs, tests, security, and production readiness.
Why this skill is useful
Provides specific patterns and examples for structuring FastAPI applications that the AI wouldn't reliably generate on its own.
What it needs
About 4k tokens when loaded. Last updated 2026-08-06. 238,342 stars on the source repository.
What this skill does
FastAPI Patterns Production-oriented patterns for FastAPI services. When to Use Building or reviewing a FastAPI app. Splitting routers, schemas, dependencies, and database access. Writing async endpoints that call a database or external service. Adding authentication, authorization, OpenAPI docs, tests, or deployment settings. Checking a FastAPI PR for copy-pasteable examples and production risks. How It Works Treat the FastAPI app as a thin HTTP layer over explicit dependencies and service code: main.py owns app construction, middleware, exception handlers, and router registration. schemas/ owns Pydantic request and response models. dependencies.py owns database, auth, pagination, and request-scoped dependencies. services/ or crud/ owns business and persistence operations. tests/ overrides dependencies instead of opening production resources. Prefer small routers and explicit responsemodel declarations. Keep raw ORM objects, secrets, and framework globals out of response schemas. Project Layout Application Factory Use a factory so tests and workers can build the app with controlled settings. Do not use alloworigins=[""] with allowcredentials=True; browsers reject that combination and Starlette disallows it for credentialed requests. Pydantic Schemas Keep request, update, and response models separate. Response models must never include password hashes, access tokens, refresh tokens, or internal authorization state. Dependencies Use dependency injection for request-scoped resources. Avoid creating sessions, clients, or credentials inline inside route handlers. Async Endpoints Keep route handlers async when they perform I/O, and use async libraries inside them. Use httpx.AsyncClient for external HTTP calls from async handlers. Do not call requests in an async route. Error Handling Centralize domain exceptions and keep response shapes stable. OpenAPI Customization Assign the custom OpenAPI callable to app.openapi; do not just call the function once. …
How to use it
Reference it in AdaL, Claude Code, Cursor or any coding agent — nothing to install:
@skills affaan-m/fastapi-patterns