Course map

Topic 036 / Core

API contract-first design: OpenAPI, AsyncAPI, contract validation, deprecation

Chapter 3.5 · API Contract Mechanics

What and why

Without a written contract, "what does the API actually do" lives only in the implementation — or in a developer's head — leaving a versioning policy (→ 35) nothing concrete to apply to. Contract-first design means writing and reviewing the API's formal contract before the implementation: OpenAPI describes synchronous HTTP APIs (endpoints, request/response shapes, status codes); AsyncAPI describes event and message contracts (→ 113) the same way. The contract is validated in CI against the real implementation, and deprecation is tracked as part of the contract's lifecycle, not as an afterthought. Builds on: → 35.

Diagram

write OpenAPI / AsyncAPI document
        │
        ▼
review (other teams read the contract, not the code)
        │
        ▼
implement ──► CI validates real responses against the document
        │
        ▼
ship; deprecated fields/endpoints carry a removal date in the contract itself

How it works

  1. Write the OpenAPI (or AsyncAPI) document describing every endpoint or message, its schema, and its status codes — before writing the handler code.
  2. Reviewers and other teams read the contract, not the implementation, to agree on the shape.
  3. CI validates the running implementation's actual responses against the document on every change, catching drift automatically instead of relying on someone noticing.
  4. A deprecated field or endpoint is marked as such in the contract with a removal date, visible to every consumer without reading a changelog.
  5. Client code can often be generated directly from the contract, removing hand-written guesswork about the shape of a request or response.

Practical example

(illustrative). An OpenAPI document declares that POST /orders returns 201 with a specific JSON schema on success and 400 with an error schema on a bad request. A CI contract test calls the real endpoint and fails the build the moment the actual response no longer matches the declared schema — catching the break before a client does.

When to use it

  • Use when: more than one team or more than one client consumes the API — the contract becomes a single source of truth both sides can build and test against independently.
  • Avoid when: the overhead isn't worth it for a tiny single-team service deployed together with its only caller — code-first is simpler there.
  • Failure scenarios: a hand-maintained contract that's never validated in CI drifts silently from the real implementation until a client discovers the gap the hard way; no deprecation marker on a field means its removal catches someone by surprise.
  • Simpler alternative: code-first with a generated OpenAPI spec from code annotations — acceptable for one team, weak the moment a second team or an external partner depends on the API.

Practice

(process kind — fill the template; 30 min, no lab services). Write the OpenAPI contract for Project 2 (URL shortener) before any code: endpoints, status codes, an error schema, and one field marked deprecated with a removal note. Done when the document alone — no code — is enough for another person to write a working client against it.

Key takeaway

The contract, not the implementation, is the thing teams agree on and test against — write it first, validate it continuously, and deprecate inside it. Done when you can: hand someone an OpenAPI document and have them build a correct client from it alone. Used next in → 37, 38 (the pagination and idempotency designs those topics define belong inside this same contract); → 113 (AsyncAPI applies the same discipline to event contracts).

References

OpenAPI Specification — see references.md. AsyncAPI Specification — see references.md.

Source directory

Search the guide