Instructions
When to Use
- Use for REST-ish Route Handlers, status codes, errors, versioning.
- Prefer
forms-and-validationfor Server Action forms. - Prefer
webhook-receiversfor inbound provider callbacks.
Shape HTTP APIs implemented as Next.js Route Handlers (or edge/node runtime).
Decision: Handler vs Server Action vs webhook
| Need | Prefer |
|---|---|
| Browser form POST same-origin | forms-and-validation / Server Action |
| Public/partner HTTP JSON API | Route Handler (api-handbook) |
| Provider callback (Stripe/GitHub) | webhook-receivers |
| Long-lived stream | realtime-handbook |
- Model resources and sub-resources; prefer nouns in paths; version in path or header - pick one and document.
- Status codes: 200/201/204/400/401/403/404/409/422/429/500 - state when each applies for this feature.
- Errors: one JSON shape (
error.code,error.message, optionaldetails); never leak stack traces in production. - Idempotency:
Idempotency-Keyfor creates; GET/PUT safe retries; document replay behavior. - Validation: zod at the boundary before side effects; typed handler args.
- AuthZ: enforce in the handler (pair
auth-handbook); never trust client-only role flags. - Rate limiting: middleware or gateway - do not invent product names.
- CORS: explicit origins; never
*with credentials; prefer same-origin BFF for cookie sessions. - Security headers: align with
security-headersfor browser-hit APIs; JSON APIs still usenosniff. - Observability: attach
requestId; log failures without PII (observability-handbook).
Outcomes
- Verb + path table, error contract, idempotency bullets, authz note.
Output Rules
Tables first; then optional example handler signature pseudocode only.
Scope and boundaries
- In scope: Route Handler design, contracts, headers.
- Out of scope: GraphQL schema design, gRPC, legacy PHP APIs.
Safety
- Read-only; no real API keys in examples.
Troubleshooting
- 405 on route: wrong HTTP method export or conflicting dynamic segment.
- Body parse errors: validate
Content-Typethen zod; avoid double-reading the stream. - Duplicate creates: missing idempotency store on POST.
- 401 loops: cookie session vs bearer mismatch - document the expected auth mode.
Related skills
forms-and-validation- Server Actions + zod formswebhook-receivers- inbound provider callbacksauth-handbook- protecting handlersobservability-handbook- request IDs and error logsrealtime-handbook- streams instead of REST
GitHub: https://github.com/bh611627/skillcodex/tree/main/skills/api-handbook/SKILL.md
npm: https://www.npmjs.com/package/@skillcodex/skills