← ClaudeAtlas

fastapi-service-patternslisted

Production FastAPI conventions — app factory, lifespan hooks, custom request routing, dependency injection, middleware, ResponseData envelope. Use when building a FastAPI service or adding routes/middleware.
ajyadav013/claude-kit · ★ 12 · API & Backend · score 72
Install: claude install-skill ajyadav013/claude-kit
Standardize FastAPI application structure, routing, middleware, and dependency injection following production service patterns. ## When to use - Scaffolding a new FastAPI service or microservice - Adding structured exception handling and logging to routes - Implementing CORS, RBAC, or custom middleware - Setting up database and Redis connections with proper lifecycle management - Configuring lifespan startup/shutdown hooks - Creating a custom route handler for request/response envelope patterns - Implementing dependency injection for database sessions or auth context - Building paginated list endpoints with consistent response format - Adding multi-tenancy support with row-level security or schema isolation - Migrating from deprecated `@app.on_event` to lifespan context manager ## Core conventions 1. **App factory pattern in `app/application.py`**: define `get_app() -> FastAPI` that creates the app, configures middleware (CORS, telemetry, security headers), includes routers, and returns a ready-to-serve instance. Use `FastAPI(debug=..., lifespan=lifespan, default_response_class=ORJSONResponse)`. Optional: `redirect_slashes=False` to prevent auto-redirects. 2. **Lifespan context manager in `app/lifetime.py`**: use `@asynccontextmanager async def lifespan(app: FastAPI)` to initialize resources (ConnectionManager, Redis, Kafka, Temporal, telemetry) on startup and close them on shutdown. The older `@app.on_event("startup"/"shutdown")` style is deprecated; prefer lifespan. 3