Scaffold a new FastAPI route in <app>: pydantic v2 request/response models, envelope mapping, regenerated specs/openapi.yaml entry, route registration in src/app/features/<slug>/module.py, and Deps wiring, as one change across the slice.
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Is that skill safe to install?
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
Are you the author of Add Handler?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/facta-dev-add-handler-c64591be)
---
name: add-handler
description: "Scaffold a new FastAPI route in <app>: pydantic v2 request/response models, envelope mapping, regenerated specs/openapi.yaml entry, route registration in src/app/features/<slug>/module.py, and Deps wiring, as one change across the slice."
division: backend
---
# <app> Add Handler
A reusable scaffolding workflow for adding an HTTP capability to the `<app>` API. An agent (e.g. the API designer role) invokes this skill to produce the handler, the regenerated OpenAPI contract, the route registration, and the Deps wiring together — so a route never lands without its contract and its wiring. The output is a design sketch plus a registration checklist.
## When to Activate
- A new endpoint is requested on the `<app>` API (a new verb on an existing path, or a new path).
- An existing handler needs a new request model, a new response model, or a new error code.
- The committed OpenAPI contract must be regenerated to cover a route that already exists in code (drift fix).
- A reviewer asks for the handler + contract + wiring bundle before approving an API change.
Do not activate for non-HTTP changes (domain logic, persistence, migrations) — those belong to other skills.
## Core Principles
- The handler is an adapter, not a service. Validate at the edge with a pydantic v2 model, call the slice's use case (`src/app/features/<slug>/usecase.py`), map to the envelope, return. No SQL, no domain logic in the handler.
- The contract is generated and committed. `specs/openapi.yaml` is produced from the app by `make generate` (`scripts/export_openapi.py` dumps `app.openapi()`), and reviewed as a diff via `make gen-check`. A route that lands without a regenerated, committed contract is a defect.
- The composition root stays thin. A new `Deps` field is a use-case service / small consumer-defined protocol wired once in `src/app/bin/api.py`; logic lives in the use cases.
- Registration is centralized per slice. `src/app/features/<slug>/module.py` `register(app, service)` is the one place a slice's routes are mounted under `/api/v1`.
- Errors are typed, not stringy. Every domain error the use case can raise maps to a stable `Code` (`app.shared.apperr`) with a deliberate status code.
## Rules
- Anti-hallucination: do not invent import paths, use-case methods, or error codes that are not in the tree. Every wiring step cites a file that exists.
- Every stat needs a source. If you cite FastAPI's duplicate-route behavior or a status code semantics, cite the FastAPI docs and RFC 9110. No unsourced numbers.
- Do not emit the final design as a fenced code block when raw output is expected — the sketch is plain text with labelled sections (handler / contract / wiring / checklist).
- Verify the cited use-case method exists in `src/app/features/<slug>/usecase.py` before wiring it; a handler that calls a method the use case does not expose is a hallucination.
- Every route must carry a matching committed `specs/openapi.yaml` entry; `make gen-check` must stay green.
- Routes are registered in the slice's `module.py`, never inline in `src/app/bin/api.py` or in another slice's file.
- Protected handlers depend on `IdentityDep` (`app.platform.authx`); admin handlers add `Depends(require_role(Role.ADMIN))`; public handlers are explicitly marked and rare.
## Research References
Provenance table — fill the link column from the primary source before relying on a claim. No claim may appear in a finding without a row here.
| claim | source | year | link |
| ---------------------------------------------------------------------------- | ---------------------------------- | ---- | ------------------------ |
| FastAPI raises when two routes share a method and path | FastAPI documentation, Routing | 2024 | <TODO: primary doc link> |
| specs/openapi.yaml is generated from the app's own app.openapi() | scripts/export_openapi.py (local) | — | local file |
| HTTP status code semantics (400/401/403/404/409/422/500) | RFC 9110, status codes | 2022 | <TODO: primary doc link> |
| pydantic v2 validates the request body at the trust boundary | pydantic v2 documentation | 2024 | <TODO: primary doc link> |
## Output Format
A design sketch plus a registration checklist, anchored to the skeleton file layout. Plain text — not a fenced block:
```
DESIGN POST /api/v1/widgets (src/app/features/widgets/http.py)
handler:
@router.post("/widgets", status_code=status.HTTP_201_CREATED)
async def create_widget(body: WidgetRequest, identity: IdentityDep) -> WidgetResponse:
# validate at the edge (pydantic v2), call the use case, return the model
...
contract:
specs/openapi.yaml — regenerated by `make generate` after the router
changes; the POST /api/v1/widgets path appears automatically.
Run `make gen-check` before landing.
wiring:
module.py -> register(app, service) mounts POST /api/v1/widgets
deps -> Deps.widgets (WidgetService) (already wired; no new field)
CHECKLIST
[ ] handler is a thin adapter — no business logic; calls the use case
[ ] pydantic v2 request/response models declared in http.py
[ ] make generate run and specs/openapi.yaml committed; make gen-check clean
[ ] route registered in module.py register() (not inline in the bin)
[ ] envelope {"error":{"code","message"}} covers the failure modes; no leaky 500s
[ ] Deps unchanged OR new field is a use case / port wired in src/app/bin/api.py
```
Summary line: `add-handler: N handler(s) designed, M registration step(s) — verdict: APPROVE | REVISE`.