| name | fastapi-endpoint |
| description | Generate a complete FastAPI CRUD endpoint with Pydantic v2 schemas, async SQLAlchemy queries, dependency injection, response models, and error handling. |
Generate FastAPI Endpoint
When to Use
Use this skill when creating a new API endpoint or resource with full CRUD operations.
Instructions
-
Read the project to understand conventions: database setup (sync/async SQLAlchemy), existing schemas, router structure, auth pattern.
-
Generate these components:
Pydantic schemas (in schemas/):
Create model (request body for POST)
Update model (request body for PATCH, all fields optional)
Response model (with model_config = ConfigDict(from_attributes=True))
- Use
Annotated[type, Field(...)] for constraints (not constr/conint)
- Use
@field_validator (not @validator)
- Explicit field list (never auto-generate from ORM)
Router (in routers/):
- Use
APIRouter with prefix and tags
response_model on every endpoint
status_code=201 on POST
- Async endpoints with
await for all DB operations
selectinload/joinedload for eager loading relationships
- HTTPException for error cases
- Dependency injection with
Annotated[type, Depends()]
Include router in main app
-
All database access must use the async session dependency pattern.
-
All endpoints must have response_model set.
-
Use @field_validator and @model_validator (Pydantic v2), never @validator or @root_validator.