Guides building performant FastAPI APIs with Pydantic v2, async/await, OpenAPI, dependency injection, and project structure best practices.
Ships in claude-skills-collection. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
pip install "fastapi[standard]" sqlalchemy[asyncio] alembic pydantic-settings httpx pytest pytest-asyncio
Structure recommandée :
app/
main.py # lifespan, include_router, middleware
config.py # Settings via pydantic-settings
dependencies.py # Depends() partagés (db, auth, pagination)
routers/
users.py
items.py
schemas/
user.py # UserCreate, UserUpdate, UserResponse
models/
user.py # SQLAlchemy ORM
services/
user_service.py
repositories/
user_repo.py
tests/
conftest.py # fixtures DB, client async
test_users.py
# app/config.py
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
database_url: str
secret_key: str
debug: bool = False
cors_origins: list[str] = []
model_config = {"env_file": ".env"}
settings = Settings()
# app/schemas/user.py
from pydantic import BaseModel, EmailStr, Field
from datetime import datetime
class UserCreate(BaseModel):
email: EmailStr
password: str = Field(min_length=8)
username: str = Field(max_length=50)
class UserUpdate(BaseModel):
username: str | None = Field(None, max_length=50)
class UserResponse(BaseModel):
id: int
email: EmailStr
username: str
created_at: datetime
model_config = {"from_attributes": True}
Règle : jamais réutiliser UserCreate comme response_model — le schéma de réponse ne doit pas contenir le mot de passe.
# app/main.py
from contextlib import asynccontextmanager
from fastapi import FastAPI
from app.routers import users, items
@asynccontextmanager
async def lifespan(app: FastAPI):
# startup
yield
# shutdown
def create_app() -> FastAPI:
app = FastAPI(
title="Mon API",
lifespan=lifespan,
docs_url="/docs" if settings.debug else None,
)
app.include_router(users.router, prefix="/users", tags=["users"])
app.include_router(items.router, prefix="/items", tags=["items"])
return app
app = create_app()
# app/dependencies.py
from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from sqlalchemy.ext.asyncio import AsyncSession
from app.db import get_session
from app.services.auth import decode_token
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/auth/token")
async def get_current_user(
token: str = Depends(oauth2_scheme),
db: AsyncSession = Depends(get_session),
):
payload = decode_token(token)
if not payload:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
return await user_repo.get_by_id(db, payload["sub"])
# Pagination simple
def pagination(skip: int = 0, limit: int = Query(20, le=100)):
return {"skip": skip, "limit": limit}
# app/routers/users.py
from fastapi import APIRouter, Depends, status
from app.schemas.user import UserCreate, UserResponse
from app.services.user_service import UserService
from app.dependencies import get_current_user, pagination
router = APIRouter()
@router.get("/", response_model=list[UserResponse])
async def list_users(
page: dict = Depends(pagination),
service: UserService = Depends(),
):
return await service.list(**page)
@router.post("/", response_model=UserResponse, status_code=status.HTTP_201_CREATED)
async def create_user(payload: UserCreate, service: UserService = Depends()):
return await service.create(payload)
@router.get("/me", response_model=UserResponse)
async def get_me(current_user=Depends(get_current_user)):
return current_user
| Cas | Décorateur |
|---|---|
| Requête BDD async (asyncpg, asyncmy) | async def |
| Appel HTTP externe (httpx) | async def |
| Calcul CPU pur, bibliothèque sync | def (thread pool auto) |
| Lecture fichier blocking | def ou run_in_executor |
Règle critique : ne jamais appeler une librairie synchrone (ex: requests, psycopg2 standard) dans un async def — ça bloque le event loop pour tous les workers.
# app/db.py
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
engine = create_async_engine(settings.database_url, pool_size=10, max_overflow=20)
AsyncSessionLocal = async_sessionmaker(engine, expire_on_commit=False)
async def get_session():
async with AsyncSessionLocal() as session:
yield session
from fastapi import Request
from fastapi.responses import JSONResponse
class AppError(Exception):
def __init__(self, code: str, status: int = 400):
self.code = code
self.status = status
@app.exception_handler(AppError)
async def app_error_handler(request: Request, exc: AppError):
return JSONResponse(status_code=exc.status, content={"error": exc.code})
Utiliser HTTPException pour les erreurs HTTP standard, AppError pour les erreurs métier avec codes lisibles.
# tests/conftest.py
import pytest_asyncio
from httpx import AsyncClient, ASGITransport
from app.main import app
@pytest_asyncio.fixture
async def client():
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
yield ac
# tests/test_users.py
import pytest
@pytest.mark.asyncio
async def test_create_user(client):
r = await client.post("/users/", json={"email": "[email protected]", "password": "secret123", "username": "alice"})
assert r.status_code == 201
assert r.json()["email"] == "[email protected]"
# Développement
fastapi dev app/main.py
# Production (Gunicorn + Uvicorn workers)
gunicorn app.main:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
# Dockerfile multi-stage
FROM python:3.12-slim AS builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
FROM python:3.12-slim
WORKDIR /app
COPY --from=builder /usr/local/lib/python3.12 /usr/local/lib/python3.12
COPY . .
CMD ["gunicorn", "app.main:app", "-w", "4", "-k", "uvicorn.workers.UvicornWorker", "--bind", "0.0.0.0:8000"]
async def avec bibliothèques sync — requests, psycopg2, time.sleep() dans un async def bloquent l'event loop. Remplacer par httpx, asyncpg, asyncio.sleep.UserCreate en réponse expose le mot de passe haché. Toujours séparer Create / Response.expire_on_commit=True (défaut SQLAlchemy) — provoque des DetachedInstanceError en async. Toujours expire_on_commit=False avec async_sessionmaker.@app.on_event("startup") deprecated — utiliser lifespan depuis FastAPI 0.93+.response_model — sans response_model, FastAPI sérialise tout l'objet ORM (champs internes, relations lazy = erreur ou fuite de données).model.dict() (Pydantic v1) — en v2, utiliser model.model_dump(). dict() est déprécié.app.dependency_overrides[get_session] = override_session pour injecter une BDD de test.str | None au lieu de Optional[str]).fastapi[standard] remplace fastapi + uvicorn[standard] depuis FastAPI 0.111+.slowapi (basé sur limits) ou middleware custom — intégrer dès le début.opentelemetry-instrumentation-fastapi pour le tracing distribué./v1/users) ou utiliser APIVersion middleware plutôt que dupliquer les fichiers.BackgroundTasks pour les tâches légères (<1s) ; déléguer à Celery/ARQ pour les tâches longues./health et /ready pour Kubernetes liveness/readiness probes.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
/claude-skills-collection:dev-fastapi-guidefrom claude-skills-collection
npx claudepluginhub vantaflow/claude-skills-collection --plugin claude-skills-collection348 skills in this plugin
/agent-a2a-protocol-guideGuide du protocole Agent-to-Agent (A2A) de Google pour l'interopérabilité entre agents IA — découverte, communication et collaboration inter-agents, avec exemples de code, critères de décision et pièges à éviter. Se déclenche avec "A2A", "agent-to-agent", "protocole A2A", "Google A2A", "interopérabilité agents". Also triggers on "agent-to-agent protocol", "agent interoperability", "connect two agents"./agent-agent-observabilityInstrumentation technique d'un agent IA — tracing distribué des appels LLM et outils, spans, métriques custom, corrélation de logs et dashboards de supervision. Pour les alertes et garde-fous de production, voir agent-monitoring-setup. Se déclenche avec "observabilité agent", "tracing agent", "LangSmith", "Langfuse", "OpenTelemetry agent", "spans LLM", "corréler les logs agent". Also triggers on "trace my agent", "LLM tracing", "agent spans"./agent-api-caller-subagentSous-agent spécialisé dans les appels API REST/GraphQL avec retry, auth et transformation de données. Se déclenche avec "sous-agent API", "API caller agent", "agent qui appelle une API", "REST agent", "HTTP agent", "API integration subagent", "external API agent". Also triggers on "subagent that calls an API", "API calling agent", "agent HTTP requests".Other plugins' skills for the same job