api-contract-design-best-practices
Skill0 starsChanged 4 months agoMITView on GitHub
Framework-agnostic standard for designing HTTP/REST API contracts. Load BEFORE adding or changing any HTTP endpoint, request/response schema, error payload, OpenAPI/Swagger document, or outbound webhook — in any language or framework (FastAPI, Express, Spring, Rails, Go, .NET, gateway config). Covers resource modeling, method and status-code semantics, RFC 9457 problem+json errors, idempotency keys, cursor pagination, filtering and sorting, versioning and breaking-change rules, ETags and conditional requests, rate-limit signaling, contract-level authorization, webhooks, OpenAPI 3.1+, and CI contract gates.
In this repository7 other files it gives its agents.
The file
Show its rules and text
We split this skill into the rules it gives an agent
16 rules. About 0 are common, 4 are distinctive, and 12 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈4
Distinctive · estimate
This author's own choices. Worth a look.
12
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 9
When this conflicts with an in-repo convention, follow the repo and say so.
- Very specific: line 15
No verb in any path segment (POST /orders, not POST /orders/createNow).
- Very specific: line 27
Write bodies carry absolute values, never {"increment": 1}.
- Very specific: line 34
Never 200 with an error body, and never a {"success": false} envelope.
- Very specific: line 64
Never put stack traces, SQL, internal hostnames, upstream vendor error text, internal identifiers, or PII in an error body.
- Very specific: line 65
Keep a registry of type URIs and codes.
- Distinctive: line 87
Default to cursor pagination.
- Very specific: line 89
Default page size is documented and finite (20 to 50), never "all".
- Very specific: line 93
Document token expiry (a few days is typical).
- Distinctive: line 126
Document every limit as part of the contract: rate limits per endpoint, principal, and tenant.
- Very specific: line 130
Default to not versioning: extend compatibly.
- Very specific: line 132
Never infer a version. Under header or query versioning, a missing or unknown version is 400 with a specific problem type.
- Very specific: line 164
Every single-resource GET returns an ETag and honors If-None-Match: a match is 304, repeating the ETag and Cache-Control the 200 would …
- Very specific: line 207
Every operation declares an operationId, at least one tag, a security requirement (explicit security: [] when deliberately public), its …
- Very specific: line 219
Before writing or reviewing the CI contract job, .spectral.yaml, or the live contract tests, read references/contract-gates.md.
- Distinctive: line 225
Never invent business semantics: if status transitions, currency rounding, retry windows, quota tiers, or which fields are required are …
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
API Contract Design Best Practices
Target: HTTP JSON APIs specified in OpenAPI 3.1 or later. When this conflicts with an in-repo convention, follow the repo and say so.
1. Resources and URLs
- Collections are plural nouns, items addressed by id:
/shipment-orders/{shipment_order_id}. - Path segments are
kebab-casematching^[a-z][a-z0-9-]*$and case-sensitive: return404on a case mismatch, never normalize. - No verb in any path segment (
POST /orders, notPOST /orders/createNow). The query string filters and pages; it never identifies or acts (?action=cancel). - Nest only for genuine containment, at most 2 levels (
/orders/{order_id}/line-items); every sub-resource is independently addressable (GET /orders/{order_id}/line-items/{line_item_id}). - Model resources and fields on the caller's job, not your tables:
/order-header,/user-role-map, or a response mirroring every column leaks the schema; ship only fields a caller needs. - A non-CRUD action is a colon custom method on the resource,
POST /orders/{order_id}:cancel, never a fake noun like/orders/{order_id}/cancellation. Use one only when get/list/create/update/delete cannot express the operation. - Ids are opaque strings in the contract even when integers underneath, with one format per concept API-wide. Never expose a sequential id where enumeration is a business risk.
2. Methods
GETandHEADnever mutate state: no view counter, nolast_seen_attouch, no lazily created row.- A repeated
DELETEof an already-deleted resource returns204(or200), never404. PUTis full replacement: reject a partial body, never merge it.PATCHrequiresapplication/merge-patch+json(default) orapplication/json-patch+json; reject bareapplication/jsonwith415. Use JSON Patch when explicitnullis a meaningful stored value (merge patch readsnullas delete and replaces arrays whole), for element-level array edits, or fortestpreconditions, and apply it atomically: any failing op means no mutation.- Write bodies carry absolute values, never
{"increment": 1}. Where a relative body is unavoidable, requireIdempotency-Keyin addition toIf-Matchand document the exception on that operation.
3. Status Codes
201MUST carryLocationand return the created representation.401MUST sendWWW-Authenticate;405MUST sendAllow;503sendsRetry-Afterwhen known.400is malformed syntax or an invalid query parameter (unknown, unparseable, or a disallowed value),422is a well-formed body with invalid values (all body field validation); never collapse409,412, or422into400.- Never
200with an error body, and never a{"success": false}envelope. - Long-running work returns
202with the status-monitor URL in anOperation-Locationheader and amonitorbody member, never inLocation(clients read that as the created resource). The monitor carriesid, astatusfromNOT_STARTED,RUNNING,SUCCEEDED,FAILED,CANCELED, and on failure anerrorin the problem+json shape; retain it at least 24 hours.
4. Errors
Serve every 4xx/5xx as application/problem+json per RFC 9457.
HTTP/1.1 422 Unprocessable Content
Content-Type: application/problem+json
{
"type": "https://api.example.com/problems/validation-failed",
"title": "Request body failed validation",
"status": 422,
"detail": "2 fields were rejected.",
"instance": "/orders/9f2c1b",
"code": "VALIDATION_FAILED",
"request_id": "01J8Z3K9QW4",
"errors": [
{ "pointer": "/items/0/quantity", "code": "OUT_OF_RANGE", "message": "must be between 1 and 500" },
{ "pointer": "/currency", "code": "UNSUPPORTED_VALUE", "message": "must be one of INR, USD, EUR" }
]
}typeis a stable URI per problem class that clients branch on, preferably dereferenceable to its documentation; omit it (about:blank) only when the status code alone carries all the meaning.titleis constant pertype, varying only by localization; occurrence text goes indetail.statusequals the status line.instanceidentifies the occurrence; the target resource URI is acceptable only when paired with a correlation-id extension such asrequest_id.- Extension names are 3+ characters matching
^[A-Za-z][A-Za-z0-9_]*$, and all machine-readable payload lives in extensions: always a stableUPPER_SNAKE_CASEcode, plus anerrorsarray of{pointer, code, message}for field validation. - Never put stack traces, SQL, internal hostnames, upstream vendor error text, internal identifiers, or PII in an error body; log them and return the
request_id. - Keep a registry of
typeURIs and codes. - One error envelope per API. If the organization already standardizes another (e.g. Azure's
errorobject), use it everywhere; never mix two.
5. Idempotency Keys
Accept Idempotency-Key on every POST that creates, charges, ships, or notifies, and on the relative-body PATCH exception in §2; where a qualifying POST genuinely needs none, say why. Do not require or honor it on other methods.
| Aspect | Rule |
| Key | Client-generated, high-entropy (UUIDv4 or equivalent), at most 255 characters, no PII or secrets; reject a malformed key with 400 |
| Scope | Per endpoint and per authenticated principal or tenant, never global |
| Replay | Store the first request's status code, response body, and the headers its status requires (Location on a 201); return them byte-for-byte for every later request with the key, including 4xx/5xx produced once execution began |
| Fingerprint | Hash the request body with the key. Same key with a different body is 409 with problem type .../idempotency-key-reused (422 only if the organization already uses it), never a replayed 2xx |
| Not stored | Input-validation failures, and collisions with a concurrently executing request on the same key; return a documented retryable error for both |
| Concurrency | Lock the key or use a unique constraint so simultaneous retries cannot both execute |
| Retention | Documented; 24 hours is the baseline. After pruning, a reused key executes fresh |
Azure-aligned estates use Repeatability-Request-ID / Repeatability-First-Sent instead; pick one scheme per organization.
6. Pagination
- Paginate every collection endpoint from its first release, however small the table is today.
- Default to cursor pagination. Use offset only for small, static, human-browsed sets that need "jump to page N", and document that results may shift.
- Request
page_sizeandpage_token; respond{"items": [...], "next": ...}, wherenextis the followingpage_tokenor an absolute URL carrying it. Same names and onenextform API-wide;limit/cursoris an acceptable house alternative, one per organization, linted. - Default page size is documented and finite (20 to 50), never "all". Enforce a hard maximum: coerce oversize requests down to it; reject negative or non-numeric values with
400. - The token is opaque and URL-safe and never carries or widens authorization. A base64 offset or a JSON blob naming a table or sort key is not opaque: sign it, encrypt it, or store it server-side.
- End of collection is signaled only by an absent
next(omit it, nevernull); a page may be short or empty before the end. - Total count is optional and documented as an estimate.
- Document token expiry (a few days is typical); an expired token is
400with a specific problemtype. - The cursor needs a total order: pair the sort key with a unique tiebreaker (
created_at, thenid).
7. Filtering, Sorting, and Expansion
- Allowlist filterable and sortable fields with documented operators.
- Reject unknown or unsupported query parameters with
400problem+json; never silently ignore them (?statu=openwould return the whole collection). - Filters: one parameter per field for equality (
?status=OPEN) and a closed set of suffixed operators for ranges (?created_at_gte=2026-01-01T00:00:00Z,?amount_lt=500). Adopt an expression language (OData$filter, RSQL) only where the organization already standardizes one. - Repeated parameters OR within a field, separate parameters AND across fields:
?status=OPEN&status=HELD¤cy=INRis(OPEN or HELD) and INR. Use this or comma-separated values, never both, and state it in the spec. - One sort syntax everywhere,
?sort=created_at,-id(leading-is descending), and a documented default sort. - Cap
?expand=depth and breadth; no expansion may fan out unboundedly.
8. Representation
| Concern | Rule |
| Casing | One property casing API-wide (snake_case or camelCase), stated and linted; query and path parameter names use the same casing (examples here use snake_case) |
| Enums | UPPER_SNAKE_CASE strings, never integers; document the closed set |
| Absent values | Omit the member; explicit null only when it is a distinct, documented state |
| Timestamps | RFC 3339 UTC with an explicit offset (2026-01-31T09:15:00Z), named *_at |
| Durations | Unit in the name: timeout_seconds, ttl_ms |
| Money | Integer minor units plus an ISO 4217 currency code, or a decimal string; never a float |
| Booleans | is_* / has_*; never tri-state via null |
| Links | Absolute URLs in named members (monitor, self) where they save the client a lookup; never a HATEOAS link graph clients must traverse |
| Link relations | Registered relations (next, deprecation) are bare tokens; a relation you invent is a lowercase absolute URI you control |
| Response bodies | Always an object, never a bare scalar or array |
9. Authorization and Limits
- TLS only; credentials, tokens, and signatures never go in the query string or path.
- Authorize the specific object, not just the route: scope every query by tenant and owner derived from the authenticated principal, never from a request parameter; IDOR is the default bug.
- Return the same response for "absent" and "not yours":
404, not403, where confirming existence leaks. Document it so clients do not read404as "safe to create". - Authorize properties: bind request bodies to explicit per-operation, per-role request schemas, never to the persistence model or the response schema, so no caller can set
role,owner_id, orbalance. Mark server-owned fieldsreadOnlyand secretswriteOnly. - Document every limit as part of the contract: rate limits per endpoint, principal, and tenant; maximum body size and array lengths.
10. Versioning and Compatibility
- Default to not versioning: extend compatibly. When you must, use one mechanism API-wide, by default a single major path segment (
/v1/...) bumped almost never. Never version per endpoint (/v1/ordersbeside/v3/orders) and never mix mechanisms. - Media-type versioning via
Acceptand a required datedapi-version=YYYY-MM-DDparameter are legitimate alternatives: follow whichever the organization already standardizes, and prefer the dated parameter for frequent dated behavior changes shipped to a large external client base that must stay pinned. - Never infer a version. Under header or query versioning, a missing or unknown version is
400with a specific problemtype. Under path versioning an unversioned URL is404; add no catch-all route.
Breaking, and forbidden within a major version:
- Removing or renaming a field, endpoint, or enum member
- Adding a required request field
- Narrowing a type (
stringtointeger, wider to narrower numeric) - Tightening validation (new
maxLength, stricter regex, new required combination) - Making an always-populated response field sometimes absent or
null - Changing a default value, or whether defaults are serialized
- Changing an id or resource-name format, looser or stricter
- Changing the
type,code, or status for the same error condition - Changing observable behavior or semantics for an unchanged request, including repurposing a field
- Adding pagination to an existing collection
Non-breaking only once the tolerant-reader contract is published: an optional request field with a safe default; a new response field; a new endpoint, method, or optional query parameter; a new error type for a genuinely new condition under an existing status. A new member of a response enum is borderline: announce it.
Publish the tolerant-reader contract explicitly: clients MUST ignore unknown response fields, enum members, and problem+json extension members, and MUST NOT depend on property order or on a field's absence.
Deprecation signals:
Deprecation: @1688169599
Sunset: Sun, 31 Dec 2028 23:59:59 GMT
Link: <https://api.example.com/deprecation-policy>; rel="deprecation"Deprecation(RFC 9745) is a Structured Field date (@plus Unix seconds), past or future, and changes no behavior.Sunset(RFC 8594) is an HTTP-date no earlier than theDeprecationdate.- Instrument a deprecated operation per caller before setting its sunset; after sunset return
410 Gone, not404.
11. Conditional Requests
- Every single-resource
GETreturns anETagand honorsIf-None-Match: a match is304, repeating theETagandCache-Controlthe200would carry. ETags are strong by default; use weak (W/) only for deliberate semantic equivalence. - Require
If-Matchon everyPATCH, and onPUTandDELETEfor any resource with concurrent writers; a mismatch is412. A required-but-missingIf-Matchis428by default (412if clients already special-case it; document which), never200. - Evaluate preconditions in order:
If-Match,If-Unmodified-Since,If-None-Match,If-Modified-Since. - Derive the ETag deterministically from stored state (a version column, or a hash of a canonically ordered serialization), never from a request-time timestamp, random id, object address, or hash over an unordered map.
- Set
Cache-Controldeliberately; anything user-scoped isprivate.
12. Rate Limits
- A throttled request gets
429withRetry-After, always.Retry-Afternever points earlier than the end of the effective window, and takes precedence overRateLimitwhen both are present. - Advertise quota with the IETF
RateLimit-PolicyandRateLimitfields:
RateLimit-Policy: "burst";q=100;w=60,"daily";q=1000;w=86400
RateLimit: "burst";r=50;t=30RateLimit-Policyitems carryq(quota, required) plus optionalqu(quota unit),w(window seconds),pk(partition key);RateLimititems carryr(remaining, required) plus optionalt(effective window in seconds, not a guaranteed reset),pk.- These fields come from an Internet-Draft (
draft-ietf-httpapi-ratelimit-headers), not an RFC: call them "draft" in the docs and re-check names and parameters against the current draft. X-RateLimit-Limit/-Remaining/-Resetmay stay for compatibility, documented as legacy aliases, never as a standard.
13. Webhooks (outbound events)
| Concern | Rule |
| Transport | HTTPS only, TLS 1.2+; reject plaintext receiver URLs at registration |
| Signature | HMAC-SHA256 over timestamp + "." + raw_body, in a dedicated header carrying the timestamp and a scheme identifier |
| Verification | Constant-time comparison over the unparsed raw bytes; ignore unknown or superseded schemes so a downgrade cannot pick a weaker one |
| Replay window | Reject timestamps outside a documented tolerance: 5 minutes by default, never 0 |
| Secret rotation | Overlap window with both secrets valid and one signature per active secret; document it (24 hours by default) |
| Delivery | At-least-once and unordered, stated in the docs: a deleted event can arrive before the created |
| Consumer contract | Verify the signature, durably persist or enqueue the raw event, then return 2xx, all before any business logic. Deduplicate on event id (fallback: object id plus event type) |
| Retries | Exponential backoff over a documented window (hours to a few days); a 3xx counts as a failure |
| Envelope | Stable id, type, created, an explicit payload version, and a reference to the affected resource. An emitted event stays pinned to the version in effect at creation and is never rewritten |
| Subscription | Subscribers select event types; never force a firehose |
| Receiver routes | Exempt from CSRF token middleware. IP allowlisting is a second control, never a substitute for signature verification |
14. OpenAPI
- Design first: write the OpenAPI change before the handler and ship both in the same change; a public-surface change without a spec change is incomplete. In a code-first framework (FastAPI), stub the schemas and route, commit the exported spec, and review that diff before the implementation; CI fails when the committed export is stale.
- One self-contained document per API with
info.title,info.version,contact, and an owning-team and audience marker. Inventory every endpoint and every live version, old majors included. - From 3.1 (3.2.0 is current) the Schema Object is JSON Schema 2020-12: write
type: ["string", "null"](nullablewas removed, not deprecated), use theexamplesarray instead of schema-levelexample, and setjsonSchemaDialectif you deviate from the default. - Describe outbound webhooks in the top-level
webhooksfield with the same schema rigor as operations. - Reuse through
components(schemas, parameters, responses,securitySchemes); never duplicate a schema inline. - Every operation declares an
operationId, at least one tag, asecurityrequirement (explicitsecurity: []when deliberately public), its safe/idempotent/cacheable properties, every response code it can emit, and every required response header.
15. Gates
spectral lint openapi.yaml --ruleset .spectral.yaml --fail-severity=warn
# --fail-on ERR is required: without it oasdiff reports breaking changes and exits 0
oasdiff breaking --fail-on ERR released/openapi.yaml openapi.yaml
# informational: changelog exits non-zero only when a spec fails to load, which must not read as a breaking change
oasdiff changelog released/openapi.yaml openapi.yaml || true- Before writing or reviewing the CI contract job,
.spectral.yaml, or the live contract tests, read references/contract-gates.md. - Confirm flag names against the installed tool versions.
16. Agent Rules
- Classify every contract change as breaking or non-breaking against §10 and say which. If it is breaking, stop and propose the compatible alternative or the version-and-deprecation path; never ship it silently.
- Never invent business semantics: if status transitions, currency rounding, retry windows, quota tiers, or which fields are required are ambiguous, ask.
- Flag security-relevant surfaces you touch in the summary: auth, tenancy scoping, id handling, outbound URLs, webhook verification, quotas.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

