Skip to content

Lesson 6 of 9 · 25m

Consumer-Driven Contracts Without Letting Consumers Design the API

Capture real consumer expectations, run provider verification, and use consumer feedback as evidence while preserving a coherent provider-owned contract.

Consumer-driven contracts sound empowering until you watch five teams demand five incompatible response shapes. The mature interpretation is narrower: consumers record what they actually depend on, and providers continuously prove they still satisfy those dependencies. The provider still owns the coherent API design.

Consumer contracts answer a different question

OpenAPI says what the provider offers. Consumer contracts say what a specific consumer uses. That difference matters when the provider offers 80 fields and the mobile app depends on 7:

Mobile checkout consumes:
GET /orders/{id}
fields: id, status, totals.amountMinor, totals.currency, items[].quantity
errors: 404 means invisible; 409 means state conflict

If the provider renames totals.amountMinor, schema validation may still pass while the mobile checkout breaks. Consumer verification catches the dependency directly.

Pact-style testing in practice

Pact is the classic implementation of this model. A consumer test records expected interactions, and the provider verifies them later:

await provider.addInteraction({
  state: "order 7f3 exists",
  uponReceiving: "a request for order 7f3",
  withRequest: {method: "GET", path: "/orders/7f3"},
  willRespondWith: {
    status: 200,
    headers: {"Content-Type": "application/json"},
    body: {
      id: like("7f3"),
      status: regex("confirmed", "^(confirmed|paid)$")
    }
  }
});

The provider test then replays those interactions against real provider states. The details vary by language, but the important discipline is stable provider states and verification in CI.

Do not overfit every incidental field

Consumers should assert what they need, not everything the provider happens to return. Exact-match contracts make additive evolution painful and create false breakage. Prefer selective matching and explicit tolerance for additional fields.

Consumer expectations reveal design gaps

If every consumer ignores half the response, ask whether the API is overexposed. If consumers rely on undocumented behavior, either document and support it or wean them off deliberately. If consumers need client-side orchestration across six calls for one workflow, perhaps the API lacks a meaningful aggregate operation.

Ownership and release coordination still matter

Contracts do not eliminate communication; they make communication concrete. Before breaking a high-use field, query which consumer contracts depend on it. Coordinate migration windows for mobile applications, embedded devices, partner integrations, and customers with pinned SDKs. Web clients deploy quickly; installed clients do not.

Field notes

  • Consumer-driven verification is strongest for internal consumers. Public APIs need compatibility policy because unknown consumers cannot publish contracts to you.
  • Pact files are not documentation replacements. They are executable dependency evidence.
  • Provider states should describe domain state, not test implementation details.
  • Do not let consumer convenience eliminate invariants. A consumer can ask for a mutation; it cannot redefine what the mutation means.

Was this lesson helpful?

Sign in to rate