Skip to content

Lesson 1 of 9 · 23m

Contract-First Thinking: The API Is the Product Boundary

Treat the OpenAPI document as the negotiated boundary between teams, not as documentation generated after the implementation has already made the decisions.

The worst API design meeting I ever sat in ended with everyone agreeing verbally. Three months later the mobile team had implemented pagination as page, the backend had shipped cursor, and the customer-facing docs showed offset. Nobody lied; the contract simply existed only in people's heads. Contract-first means you write the boundary down before code makes improvisation expensive.

The mental model

An API is not a set of endpoints. It is a promises system:

If the caller sends this shaped request,
under this authentication and workflow state,
the service will perform this effect
and return this shaped response,
with these failure meanings.

OpenAPI captures much of that promise in a machine-readable form. It does not capture everything — ownership rules, side effects, operational latency, and business invariants still need prose, tests, and judgment.

Design behavior before syntax

Start with use cases and state transitions, then write paths. A checkout API is not POST /orders because nouns are fashionable. It is a sequence of invariants: draft cart, priced cart, payable order, failed payment, confirmed order. The contract should make those states observable instead of hiding them behind ambiguous booleans.

paths:
  /orders/{orderId}:
    get:
      operationId: getOrder
      parameters:
        - name: orderId
          in: path
          required: true
          schema: {type: string, format: uuid}
      responses:
        '200':
          description: Order representation
        '404':
          description: Order not found or not visible to caller

Notice the 404 description. Security and ambiguity often meet here: returning 404 for invisible objects can reduce enumeration, but it also changes debugging semantics. That is a product decision, not a syntax detail.

Boring consistency is a feature

Pick conventions and apply them ruthlessly: operationId names, error envelope shape, pagination strategy, time formats, idempotency behavior, nullability, and ownership terminology. Consumers build mental models faster when every endpoint follows the same rules.

Write the contract where engineers can review it

Keep openapi.yaml in the same repository as the service or in a clearly owned API repository. Review it like code. The pull request should discuss breaking behavior, not just YAML indentation. If product, backend, frontend, security, and support cannot all comment on the contract, your process has excluded one of the teams that will pay for mistakes.

Field notes

  • OpenAPI is a contract language, not a requirements dump; use prose around it for lifecycle and operational behavior.
  • If a field is meaningless to consumers, do not expose it because the database already has it.
  • Decide whether absent, null, empty string, and empty array mean different things. They often do in practice.
  • A contract that nobody enforces is still documentation — and stale documentation is worse than none.

Was this lesson helpful?

Sign in to rate