API design
Contract
Section titled “Contract”- The API contract MUST be written down before implementation, as an OpenAPI document (REST) or schema (GraphQL), and kept in the repo.
- Generated clients and server validation SHOULD come from that contract so they cannot drift.
Resources and methods
Section titled “Resources and methods”- Nouns for resources, plural, lowercase, hyphenated:
/v1/orders/{orderId}/line-items. GETreads (safe, cacheable),POSTcreates or runs an action,PUTreplaces,PATCHpartially updates,DELETEremoves.GETMUST NOT change state.
Status codes
Section titled “Status codes”200OK,201Created (withLocation),204No Content,400invalid input,401not authenticated,403not allowed,404not found,409conflict,422valid syntax but failed rules,429rate limited (withRetry-After),5xxserver faults only.
Errors
Section titled “Errors”- Error responses MUST use one format across the API. Use Problem Details (RFC 9457,
application/problem+json):type,title,status,detail,instance, plus field-levelerrorswhere useful. - Errors MUST NOT leak stack traces, SQL, internal hostnames or whether a user account exists.
- Paginate every list endpoint. Prefer cursor pagination for large or changing data; return
nextcursors. - Set a default and a maximum page size.
- Filtering and sorting through documented query parameters only.
Safety
Section titled “Safety”POSTendpoints that create payments, orders or messages MUST accept anIdempotency-Keyheader and return the original result on retry.- Validate every input against the schema; reject unknown fields on writes.
- Authenticate every non-public endpoint; authorise every object access (check the caller owns or may see that record).
- Rate-limit by user and by IP.
Versioning and change
Section titled “Versioning and change”- Version in the path (
/v1/) or a header; pick one per API. - Adding optional fields is non-breaking. Removing, renaming or changing meaning is breaking and needs a new version and a deprecation period.
- Timestamps in ISO 8601 UTC; IDs as opaque strings.
Checklist
Section titled “Checklist”- Contract written and committed
- Consistent error format
- Pagination with limits
- Idempotency on state-changing money/message endpoints
- Authn + per-object authz on every endpoint
- Rate limits