API-45Recommended
Write the contract first
Describe every endpoint, parameter, body, response and error in one OpenAPI document, kept in version control next to the code.
How to do it
Use OpenAPI 3.1 or 3.2, whose schemas are JSON Schema 2020-12. Review contract changes like code. Lint it with a tool such as Spectral or Redocly. An endpoint that follows an outside protocol, such as MCP or JSON-RPC, uses that protocol's schema as its contract instead. Check the running services against it as Check the contract between services says.
Examples
YAML example
openapi: 3.1.1
info: { title: Example Shop API, version: 2.3.0 }
paths:
/orders/{orderId}:
get:
operationId: getOrder
parameters:
- { name: orderId, in: path, required: true, schema: { type: string } }
responses:
'200': { description: The order, content: { application/json: { schema: { $ref: '#/components/schemas/Order' } } } }
'404': { description: Not found, content: { application/problem+json: { schema: { $ref: '#/components/schemas/Problem' } } } }
components:
schemas:
Order: { type: object, required: [id], properties: { id: { type: string } } }
Problem: { type: object, properties: { type: { type: string }, title: { type: string }, status: { type: integer } } }
How to check
The linter passes on the OpenAPI file in the build, and every route the server answers appears in it.
Sources
Related rules
- TST-14 Check the contract between services in Testing
- API-68 Describe multi-step tasks as Arazzo workflows in API design
- API-88 Check every schema change for breaking changes in CI in API design
- STK-98 Use Spectral to lint the OpenAPI contract in Software choices
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/api-design/contracts-and-docs/openapi-contract">Agent Wranglers API design playbook</a>. In code you write, add a comment next to it with the same link.