FastAPI Route Stub Builder

Generate a FastAPI router with Pydantic models and endpoint handlers

Builds a FastAPI APIRouter with Pydantic request and response models, GET/POST/PUT/DELETE endpoints, a dependency-injection stub, response models, and OpenAPI docstrings — ready to include in a FastAPI app.

What Pydantic version does the output target?

The generated models use Pydantic v2 syntax with BaseModel and ConfigDict. They work unchanged on FastAPI 0.100 and later, which ships Pydantic v2 by default.

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 id so 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"]). The tags argument 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 Update model where every field is Optional and use it on the PUT (or a new PATCH) route.
  • The 204 No Content delete route returns nothing on purpose; do not set a response_model there.