API Architecture and Integration Design · API Styles and Contracts
OpenAPI and Contract-First Development
Design the contract first, then generate docs, mocks, clients and tests.
An OpenAPI document describes endpoints, schemas, auth and errors. In contract-first development, teams review the spec before coding; then generate documentation, mock servers for frontend work, typed clients/SDKs, and validate requests/responses in tests. Lint specs (Spectral) for style rules and detect breaking changes in CI (oasdiff).
openapi: 3.1.0
info: { title: Orders API, version: 1.4.0 }
paths:
/v1/orders/{id}:
get:
operationId: getOrder
parameters: [{ name: id, in: path, required: true, schema: { type: string } }]
responses:
"200": { content: { application/json: { schema: { $ref: "#/components/schemas/Order" } } } }
"404": { content: { application/problem+json: { schema: { $ref: "#/components/schemas/Problem" } } } }
components:
schemas:
Order:
type: object
required: [id, status, total]
properties:
id: { type: string }
status: { type: string, enum: [pending, paid, shipped, cancelled] }
total: { $ref: "#/components/schemas/Money" }
Common pitfalls
- Specs written after the fact and drifting from behavior.
- No breaking-change detection in CI.
- Undocumented error responses.
Key points
- Review contracts before building
- Generate mocks, clients and docs
- Lint and diff specs in CI
Further reading: OpenAPI Specification
Practice questions
What are the benefits of contract-first API design?
Answer
Early feedback from consumers, parallel frontend/backend work with mocks, consistent style via linting, and automated breaking-change detection.
How can CI detect breaking API changes?
Answer
Compare the new OpenAPI spec with the published one using a diff tool (e.g., oasdiff) and fail on removals, new required fields or type changes.
Open in the app to mark lessons complete, take notes and track your progress.