Scaffold a typed FastAPI router instantly
FastAPI’s power comes from Pydantic models that drive validation, serialization, and the automatic OpenAPI schema. Writing those models plus the matching CRUD endpoints by hand is repetitive. This builder generates a complete APIRouter — request and response models, five REST endpoints, a dependency stub, and docstrings — so you only fill in the database logic.
How it works
You enter a singular resource name and a list of fields written as name type pairs (for example email str or age int). The tool builds two Pydantic models:
- a Create model containing exactly the fields you listed, used for request bodies, and
- a Read model that inherits the Create fields and adds an
idso responses carry the primary key.
It then emits five endpoints following REST and FastAPI conventions:
GET / -> list[ResourceRead]
GET /{id} -> ResourceRead
POST / -> ResourceRead, status_code=201
PUT /{id} -> ResourceRead
DELETE /{id} -> status_code=204
Each endpoint declares its response_model, accepts a db = Depends(get_db) dependency, and includes an OpenAPI summary via the route decorator plus a docstring. Path parameters are typed (item_id: int), and the create/update routes take the typed Pydantic body so validation happens automatically.
Tips and notes
- Register the router with
app.include_router(router, prefix="/users", tags=["users"]). Thetagsargument groups the endpoints in the Swagger UI. - Keep the
Depends(get_db)stub even after wiring a real session factory — it is how FastAPI scopes resources per request and makes them testable. - For partial updates, add a third
Updatemodel where every field isOptionaland use it on thePUT(or a newPATCH) route. - The
204 No Contentdelete route returns nothing on purpose; do not set aresponse_modelthere.