What does this skill do?

The FastAPI Production Patterns Skill provides a comprehensive set of best practices for building production-ready FastAPI APIs. It covers scalable project structure with layer separation, Pydantic v2 schemas with validators, typed dependency injection using Annotated, defensive JWT authentication, transactional services, and asynchronous end-to-end testing with httpx and an isolated database.

New Project from Scratch
Start a FastAPI project with a solid structure, separation of concerns, and environment-specific configuration right from the start.
Refactor an existing API
Extracts business logic from the routers into a transactional service layer and adopts typed dependencies.
Reusable JWT Authentication
Implements JWT authentication with dependency injection, 401/403 differentiation, and aliases as ActiveUserDep.
Asynchronous E2E Testing
Set up pytest fixtures using httpx.AsyncClient, ASGITransport, and an in-memory database isolated for each test.

Usage examples

🚀 Initial Structure
Create the FastAPI project structure for a user management API with separate routers, models, schemas, and services.
🔐 JWT Authentication
Implement JWT authentication in my FastAPI API using typed dependency injection and handling 401 and 403 errors.
🧪 Asynchronous tests
Set up asynchronous end-to-end testing with httpx and pytest for my API, using an isolated in-memory database.
🔧 Router Refactoring
Refactor this router so that it delegates all business logic to a transactional service and uses typed dependencies with Annotated.

Features

Strict layer separation Slim routers without business logic, transactional services, and Pydantic schemas independent of ORM models.
Typed Dependency Injection Using Annotated[..., Depends(...)] with reusable aliases such as DbDep and ActiveUserDep throughout the codebase.
PyDantic v2 and SQLAlchemy async Schemas using `model_validator` and `from_attributes`, asynchronous database operations without blocking calls.
Defensive JWT Authentication Clear error handling that distinguishes between 401 (not authenticated) and 403 (not authorized), with no information leaks.
Isolated E2E Testing Pytest fixtures using httpx.AsyncClient and ASGITransport, overriding `get_db` to use an in-memory database for each test.

Frequently asked questions

You need fastapi, uvicorn, sqlalchemy[asyncio], pydantic-settings, python-jose, and passlib[bcrypt]. For testing: httpx and pytest with asynchronous support.
Yes. The skill is a set of patterns and references that Claude uses to generate code. It does not require any external MCP or additional paid API.
Yes. The skill is designed for both new projects and for refactoring existing APIs that lack layer separation or have business logic in the routers.
Pydantic v2. Schemas use `model_validator` and the `from_attributes` configuration to map ORM models, which are specific to version 2.
FastAPI Production Patterns — Scalable Backend with Claude AI

¿Prefieres escuchar el contenido? Genera la narración de audio con un clic.