project · maintained · open source
P/D82

fastcbv

Class-based views for FastAPI. HTTP verbs become methods on a class, and the dependencies an endpoint group has in common are declared once as class attributes instead of repeated in every function signature.

maintained
python
open source · mit
since 2025

FastAPI declares an endpoint as a function, and everything the endpoint needs is declared in its signature. That is the right shape for one route. It stops being the right shape once several routes share the same authentication, the same database handle, and the same path parameter, because the shared part has nowhere to live and gets restated in every function that needs it.

A class is where that shared part already belongs. fastcbv binds a view class to a route, dispatches each HTTP method to the method of the same name, and reads class attributes annotated with Depends as dependencies of every method on the class. What was repeated per function is written once, and the endpoints that differ are the only thing left in the body.

example.py
a view, its dependency, and one verb
1 2 3 4 5 6
@router.view("/items/{item_id}")
class ItemView(BaseView):
db: Annotated[Database, Depends(get_db)]
async def get(self, item_id: int) -> dict:
return await self.db.get_item(item_id)
the pieces

Path, query, and body parameters are declared in the method signature and resolve exactly as FastAPI resolves them on a function. A __prepare__ hook runs before the dispatched method for setup the whole class shares, the request is available as self.request, and views inherit, so a base class can carry the dependencies and the hook that a family of routes has in common.