Skilllibrary fastapi
Build or review FastAPI endpoints, dependencies, Pydantic models, and lifespan wiring with clear boundary contracts and testable error handling. Use this when editing FastAPI apps, routers, request or response models, dependency injection, or async API behavior. Do not use for generic Python scripts or framework-neutral API design.
git clone https://github.com/merceralex397-collab/skilllibrary
T=$(mktemp -d) && git clone --depth=1 https://github.com/merceralex397-collab/skilllibrary "$T" && mkdir -p ~/.claude/skills && cp -r "$T/09-backend-api-and-data/fastapi" ~/.claude/skills/merceralex397-collab-skilllibrary-fastapi && rm -rf "$T"
09-backend-api-and-data/fastapi/SKILL.mdPurpose
Use this skill to keep FastAPI work explicit at the HTTP boundary instead of letting framework convenience leak through the whole service.
When to use this skill
Use this skill when:
- adding or refactoring
,FastAPI
,APIRouter
,Depends
, orresponse_model
wiringlifespan - reviewing request validation, response shaping, auth dependencies, or background task usage in a FastAPI service
- debugging why a route, dependency chain, or model contract is behaving differently from the intended API surface
Do not use this skill when
- the task is a general Python script, CLI, or batch job with no FastAPI boundary
- the main work is framework-neutral contract design rather than FastAPI implementation details
- a narrower active skill already owns the problem, such as pure database modeling or queue delivery
Operating procedure
-
Inventory the app surface first. Locate the app entrypoint, router layout, dependency providers, exception handlers, and model modules before editing route code.
-
Keep transport models at the edge. Use Pydantic request and response models to define HTTP contracts. Do not pass raw ORM objects or ad hoc dicts through the handler boundary unless the repo already standardizes that pattern.
-
Make dependency flow obvious. Prefer small composable dependencies for auth, DB sessions, and request-scoped context. Avoid hidden globals or dependency functions that perform unrelated work.
-
Choose sync versus async deliberately. Use
only when the handler actually awaits async I/O. Keep CPU-heavy work or blocking libraries out of the event loop unless the repo already wraps them safely. Example:async def
for sync DB calls via SQLAlchemy;def
only when using async drivers likeasync def
orasyncpg
.httpx -
Normalize failure behavior. Decide where errors should become
, where domain errors should be translated centrally, and what shape the client should receive.HTTPException -
Verify the real boundary. Run the narrowest route or integration test that proves the request body, dependency chain, status code, and response model all match the intended contract.
Decision rules
- Prefer
and explicit status codes over relying on implicit serialization.response_model - Keep DB session creation, auth lookup, and request context dependencies separate so failures are easier to reason about.
- Use
or equivalent startup and shutdown wiring for shared resources; avoid scattered initialization side effects.lifespan - If a route starts accumulating branching business logic, push that logic down into a service layer and keep the handler focused on transport concerns.
Output requirements
Service SurfaceRouter and Dependency PlanModel and Error ShapeValidation
Scripts
: scan a FastAPI codebase for route decorators, methods, paths, and sync versus async handlers.scripts/route_inventory.py
References
Read these only when relevant:
references/router-and-dependency-patterns.mdreferences/validation-and-response-shapes.mdreferences/async-lifespan-and-testing.md
Related skills
pythonapi-contractsauth-patterns
Anti-patterns
- Returning ORM models directly as responses, leaking internal schema to clients.
- Using
for CPU-bound or blocking I/O operations that starve the event loop.async def - Giant dependency chains that hide business logic behind layers of
.Depends - Catching exceptions in handlers instead of using centralized exception handlers.
- Missing
on routes, causing unvalidated response shapes.response_model
Failure handling
- If the repo does not separate handlers, services, and persistence yet, describe the existing pattern before forcing a refactor.
- If request and response shapes are unclear, stop and inventory the live contract before rewriting models.
- If the real bug is framework-neutral API design, hand off to
instead of stretching this skill.api-contracts