kit-fastapi-api
Implement or change HTTP endpoints in a FastAPI application following FastAPI's conventions and the project's own patterns, with Pydantic request and response models, dependency-injected authenticatio
SKILL.mdregistry/skills/fastapi-api/SKILL.md
GitHubFastAPI API implementation
1. Learn the application's structure
- Find how the app is built (
FastAPI()instance or acreate_appfactory), how routers are included (APIRouter,prefix,tags), and the Pydantic major version (v2model_config,model_dump; v1Config,dict). - Note existing dependencies for the database session, the current user, and permissions, exception handlers, and settings management.
- Read an existing route and its tests, and follow them, including sync or
asyncstyle.
2. Implement the route
- Add the route to the matching
APIRouter. Separate input and output models (UserCreate,UserRead) and setresponse_modelor a return annotation so fields such as password hashes are never serialized. Output models that validate ORM objects needmodel_config = ConfigDict(from_attributes=True)(v1:orm_mode = True). - Use path, query, and body parameters with types and constraints (
Annotated[int, Path(gt=0)],Field) so FastAPI validates input and returns 422 on bad data. - Raise
HTTPException(or the project's error types) with the right status for missing resources and client mistakes; setstatus_code=201on creation and 204 with no body on deletion. - Get the session, current user, and permissions through
Depends, never globals. Protect anything not explicitly public; if no authentication exists and the endpoint exposes user data, say so and ask before shipping it unprotected. - Do not call blocking I/O (sync database drivers,
requests,time.sleep) insideasync def; usedefendpoints or async libraries. - Use the project's logger, never
print, and never log credentials or tokens.
3. Test
- Test through
fastapi.testclient.TestClientorhttpx.AsyncClientwithASGITransport, overriding dependencies withapp.dependency_overridesand clearing them after each test. - Cover the success response and its shape, validation failures (422), missing resources (404), and authorization failures where relevant.
- Run the project's tests and type checker.
4. Report
Summarize the routes added or changed, the models and dependencies used, the tests and their results, and any assumptions, such as authentication decisions, that the user should confirm.
Examples
- Use: "Add a POST /users/{id}/deactivate endpoint." "Add pagination to GET /orders."
- Skip: "Review app/routes/users.py." (code review) "Add a Flask blueprint." (other framework)