API Contracts
Purpose
Make the API contract explicit and enforceable: one source of truth (OpenAPI spec or GraphQL schema), types shared or generated from it, and a policy for what may change without breaking clients.
When to Use
- When backend and any client (web/mobile/third party) must agree on shapes.
- When adding/changing endpoints — contract first, then implementation.
- Not for internal service-to-service shapes with a single owner (still useful, lower ceremony).
Inputs
- Endpoint/schema design (
rest-api-design / graphql-api-design).
- Client list and how they consume types (codegen, shared package, manual).
Discovery Questions
- Where does the contract live, and what generates from it (types, clients, docs)?
- Which clients are deployed independently and can lag the backend?
- Is the contract written first, or inferred from code — and is that enforced?
Responsibilities
- Establish the source of truth: OpenAPI document or GraphQL SDL, versioned in the repo.
- Plan type flow: generate client types/servers stubs from the contract, or generate the contract from typed server code — one direction, enforced in CI.
- Define the breaking-change policy: additive (new optional fields, new endpoints) is safe; removing/renaming/retyping/making-required breaks — requires a version bump or deprecation window.
- Keep validation schemas (
backend-validation) and the contract from drifting — ideally derived from the same definitions.
- Document error shapes and auth requirements in the contract, not just prose.
Required Workflow
- Choose contract format + storage location.
- Choose the generation direction and wire it into CI (drift fails the build).
- Record the breaking-change policy and deprecation process.
- For each new/changed endpoint: update contract → review → implement → verify against contract.
Decision Rules
- One source of truth; hand-maintained parallel type definitions are drift waiting to happen.
- Mobile clients lag: deprecation windows must cover store-release cycles (
../../mobile/ pack, if present).
- Contract review is part of code review for any endpoint change (
../../code-review).
Rules
- No endpoint ships that isn't in the contract.
- Breaking changes require explicit approval and a migration note for clients.
- Generated artifacts are never hand-edited.
Anti-Patterns
- Types copy-pasted between backend and frontend repos.
- Contract updated after implementation "when there's time."
- Silent breaking changes ("just renamed a field").
- OpenAPI docs that describe an older API than the deployed one.
Validation Checklist
Definition of Done
A versioned contract source of truth with enforced type flow, a recorded breaking-change policy, and a contract-first workflow for endpoint changes.
Related Skills
rest-api-design, graphql-api-design, backend-validation, backend-error-handling, ../../code-review, ../../documentation.
Related Knowledge
../../../knowledge/ (client release cadences, integration owners).
Related References
../../../references/backend/api/ (contract conventions, when populated).
Context Loading Guidance
- Requires: endpoint design, client list.
- Does not require: handler implementations, database schema.
- May load:
backend-validation (schema sharing).
- Stop when: contract source, type flow, and change policy are recorded.
Token Efficiency Guidance
Reference the contract file instead of pasting it; review diffs of the contract, not the whole document.
1---2name: api-contracts3description: Use to define and maintain the API contract between backend and clients — OpenAPI/GraphQL schema as source of truth, shared/generated types, breaking-change policy, and contract-first workflow for new endpoints.4---56# API Contracts78## Purpose910Make the API contract explicit and enforceable: one source of truth (OpenAPI spec or GraphQL schema), types shared or generated from it, and a policy for what may change without breaking clients.1112## When to Use1314- When backend and any client (web/mobile/third party) must agree on shapes.15- When adding/changing endpoints — contract first, then implementation.16- **Not** for internal service-to-service shapes with a single owner (still useful, lower ceremony).1718## Inputs1920- Endpoint/schema design (`rest-api-design` / `graphql-api-design`).21- Client list and how they consume types (codegen, shared package, manual).2223## Discovery Questions2425- Where does the contract live, and what generates from it (types, clients, docs)?26- Which clients are deployed independently and can lag the backend?27- Is the contract written first, or inferred from code — and is that enforced?2829## Responsibilities3031- Establish the **source of truth**: OpenAPI document or GraphQL SDL, versioned in the repo.32- Plan **type flow**: generate client types/servers stubs from the contract, or generate the contract from typed server code — one direction, enforced in CI.33- Define the **breaking-change policy**: additive (new optional fields, new endpoints) is safe; removing/renaming/retyping/making-required breaks — requires a version bump or deprecation window.34- Keep validation schemas (`backend-validation`) and the contract from drifting — ideally derived from the same definitions.35- Document error shapes and auth requirements in the contract, not just prose.3637## Required Workflow38391. Choose contract format + storage location.402. Choose the generation direction and wire it into CI (drift fails the build).413. Record the breaking-change policy and deprecation process.424. For each new/changed endpoint: update contract → review → implement → verify against contract.4344## Decision Rules4546- One source of truth; hand-maintained parallel type definitions are drift waiting to happen.47- Mobile clients lag: deprecation windows must cover store-release cycles (`../../mobile/` pack, if present).48- Contract review is part of code review for any endpoint change (`../../code-review`).4950## Rules5152- No endpoint ships that isn't in the contract.53- Breaking changes require explicit approval and a migration note for clients.54- Generated artifacts are never hand-edited.5556## Anti-Patterns5758- Types copy-pasted between backend and frontend repos.59- Contract updated after implementation "when there's time."60- Silent breaking changes ("just renamed a field").61- OpenAPI docs that describe an older API than the deployed one.6263## Validation Checklist6465- [ ] Source of truth chosen and versioned.66- [ ] Generation direction wired; drift detected in CI.67- [ ] Breaking-change policy recorded.68- [ ] Error shapes + auth requirements in the contract.69- [ ] Deprecation window covers slowest client.7071## Definition of Done7273A versioned contract source of truth with enforced type flow, a recorded breaking-change policy, and a contract-first workflow for endpoint changes.7475## Related Skills7677`rest-api-design`, `graphql-api-design`, `backend-validation`, `backend-error-handling`, `../../code-review`, `../../documentation`.7879## Related Knowledge8081`../../../knowledge/` (client release cadences, integration owners).8283## Related References8485`../../../references/backend/api/` (contract conventions, when populated).8687## Context Loading Guidance8889- **Requires:** endpoint design, client list.90- **Does not require:** handler implementations, database schema.91- **May load:** `backend-validation` (schema sharing).92- **Stop when:** contract source, type flow, and change policy are recorded.9394## Token Efficiency Guidance9596Reference the contract file instead of pasting it; review diffs of the contract, not the whole document.