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/):Createmodel (request body for POST)Updatemodel (request body for PATCH, all fields optional)Responsemodel (withmodel_config = ConfigDict(from_attributes=True))- Use
Annotated[type, Field(...)]for constraints (notconstr/conint) - Use
@field_validator(not@validator) - Explicit field list (never auto-generate from ORM)
Router (in
routers/):- Use
APIRouterwithprefixandtags response_modelon every endpointstatus_code=201on POST- Async endpoints with
awaitfor all DB operations selectinload/joinedloadfor 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_modelset.Use
@field_validatorand@model_validator(Pydantic v2), never@validatoror@root_validator.
Source: RoninForge/roninforge-fastapi — distributed by TomeVault.