8. API contracts: API Contract QA¶
A pull request renames a field or tightens a request body. Its own tests pass. Three other services break in production.
Spec-diff tools can tell you a change is "breaking", but not who it breaks, so teams either ignore the warnings or block every change. k11 API Contract QA answers the real question: which consumers does this change break, and why?
What it covers¶
Formats, detected from the file: OpenAPI 3.x, Swagger 2.0, GraphQL (SDL or introspection JSON), gRPC/protobuf (imports followed), AsyncAPI 2.x/3.x and SOAP/WSDL 1.1. Every format goes through the same rules, consumer impact and report.
| Format | Operations are named like |
|---|---|
| OpenAPI, Swagger | GET /orders/{id} |
| GraphQL | QUERY order, MUTATION createOrder |
| gRPC | RPC shop.v1.OrderService/GetOrder |
| AsyncAPI | EMITS order.created, RECEIVES order.cancel |
| SOAP | SOAP OrderPort/GetOrder |
The rules, the same for every format:
| Change | Breaking? |
|---|---|
| Endpoint removed | yes (critical) |
| Response field removed, type changed, became nullable, no longer guaranteed | yes |
| New required request field or parameter; field became required | yes |
| Request enum value removed; field newly restricted to an enum | yes |
| New authentication requirement | yes (critical) |
| New endpoint, optional field or parameter; response enum value added | no (enum additions flagged for exhaustive switches) |
| protobuf: field number changed, message replaced, streaming changed | yes |
| protobuf: field renamed with the same number | yes (medium: wire-safe, breaks JSON and generated code) |
The rules are deterministic: no model is involved, and the same change gets the same verdict every run.
Tell it who your consumers are¶
The impact analysis is only as good as .qa-mcp/consumers.yaml:
consumers:
- name: billing-svc
repo: acme/billing-svc
owner: "@team-billing"
criticality: critical # critical | high | medium | low
uses:
- endpoint: GET /orders/{id}
reads: [id, total, currency] # leave out to mean "any field"
- name: checkout-web
owner: "@team-web"
criticality: high
uses:
- endpoint: POST /orders
sends: [customerId, items]
reads: [id]
Field lists make the verdict precise. A removed field only counts against consumers that read it; a newly required field only against consumers that don't send it. Without field lists, every breaking change on an endpoint counts against everyone who calls it.
You don't have to write this from memory. discover_consumer_usage scans a consumer's source code (Python, Java, TypeScript,
Go…) for calls to your API, with file and line, and drafts its entry. For GraphQL it parses the consumer's actual queries, so it
knows exactly which fields they read.
Fields of list responses
For an endpoint that returns a list, such as GET /orders, write the item fields plainly: reads: [id, createdAt].
Since version 0.1.1, these are matched to changes in the list's items (reported as [].createdAt). Earlier versions missed them.
The verdict¶
| Verdict | Means |
|---|---|
| breaking | at least one known consumer breaks |
| breaking, no known consumer | breaking changes, but nobody listed uses what changed. Check the consumer list is complete |
| compatible | nothing breaks |
And it decides when a person should decide before merge: when two or more consumers break, or a critical or high one does.
A real pull request comment from the tool's demo, where Order Service v2 changes total from a number to a string, drops
customer.email, adds a required channel and removes DELETE /orders/{id}:
API contract check: 🔴 Breaking for known consumers
11 changes, 6 breaking; 4 known consumers, 3 affected. Needs a person before merge: 3 consumers break (threshold 2); high-criticality consumers break.
Consumer Criticality Breaking change Why it affects them billing-svc critical GET /orders/{id}: 'total' changed type from number to stringreads 'total' checkout-web high POST /orders: new required request field 'channel'doesn't send the now-required 'channel' notification-svc high GET /orders/{id}: 'customer.email' was removedreads 'customer.email'
A fourth consumer, analytics-svc, calls GET /orders too, but reads only id and status, so it isn't flagged.
In CI: contract-ci¶
It compares the spec in the pull request with the same file on origin/main (check out with fetch-depth: 0), and writes
.qa-mcp/contract-report/contract-report.md (a PR comment) and contract-report.json.
| Option | Default | |
|---|---|---|
--spec PATH |
(required) | the contract in this branch |
--base REF |
origin/main |
the git ref to compare with |
--base-spec SPEC |
compare with this file or <ref>:<path> instead |
|
--consumers PATH |
.qa-mcp/consumers.yaml |
|
--fail-on LEVEL |
breaking |
breaking, needs-human, any-breaking, never |
--report-dir PATH |
.qa-mcp/contract-report |
When the release gate decides, run it with --fail-on never and let the policy judge.
Beyond the diff¶
| Tool | Use it to |
|---|---|
check_deployed_contract |
compare what a running service publishes (GraphQL introspection, ?wsdl, /v3/api-docs) with your spec file: matches, drift or incompatible |
check_live_conformance |
call a running REST endpoint and check status, content type and the body against the schema |
generate_api_tests |
generate REST contract tests (status and JSON schema) for pytest + httpx or REST Assured |
The commands: /check-api-change and /api-contract-tests.
Evidence for the gate¶
contract-report.json is the contract evidence kind.
| Rule | Outcome | When |
|---|---|---|
contract.breaks_critical_consumer |
block | a breaking change affects a consumer whose criticality is in the policy's critical_consumers (default: critical, high) |
contract.breaks_consumer |
needs a person | it affects another known consumer |
contract.escalation |
needs a person | the contract check itself said a person should decide |
contract.breaking_unused |
condition | breaking changes no known consumer uses: confirm the consumer list is complete |
Changes to openapi*.yaml, swagger*, *.graphql, *.proto, asyncapi*.yaml and *.wsdl make contract evidence required
for that pull request (Chapter 14).