API Design
Rule
Rules for HTTP APIs, tRPC routers, and server actions.
Design Principles
- MUSTConsistent error format across all endpoints (see Error Shape below).
- SHOULDVersion via URL prefix (
/v1/) only when introducing breaking changes. - SHOULDPrefer cursor-based pagination over offset-based.
HTTP Conventions
- MUSTUse appropriate status codes —
201for creation,204for deletion,409for conflicts,422for semantic errors. - MUST
401for missing/invalid auth,403for insufficient permissions. Never conflate them.
Error Shape
All API errors MUST return:
{
"error": {
"code": "auth/token-expired",
"message": "Your session has expired. Please sign in again.",
"details": {}
}
}
- MUST
codeis machine-readable, namespaced (domain/error-name). - MUST
messageis human-readable, safe to display. - SHOULD
detailsprovides structured context (field errors, limits, etc.).
tRPC
- MUSTUse
@trpc/tanstack-react-querywithqueryOptions/mutationOptions. - MUSTZod for all input validation.
- MUSTUse
queryKey()for cache invalidation — never manual string arrays. - SHOULDOrganize routers by domain (
user,posts,billing). - SHOULDUse
superjsonas transformer for Date/Map/Set serialization. - SHOULDPrefer optimistic updates for mutations that modify displayed data.
- See integrations.md for adapter error handling rules.
OpenAPI
- MUSTGenerate OpenAPI specs from code — never hand-write them.
- SHOULDUse
zod-openapifor Zod-first HTTP APIs,trpc-openapifor tRPC REST exposure. - SHOULDServe the spec at
/api/openapi.jsonfor tooling and agent consumption. - SHOULDDocument rate limits, auth requirements, and pagination in the spec.
CLI Surface
Every project with an API SHOULD ship a CLI that exposes it. See cli.md.