System Design · Concept Library · Free article
API Contracts: Evolve APIs Without Breaking Clients
An API contract is what two services promise each other at the boundary, and whether a change breaks it depends on who writes a message and who reads it. Additive changes are usually safe; renames and removals need an expand, migrate, contract rollout.
An API contract describes the requests and responses two sides agree to exchange: fields, types, errors, authentication, and behavior such as retry semantics. Safe evolution depends on who writes a message and who reads it. Additive changes are usually safe only when readers tolerate unknown fields. Renames, removals, tighter validation, and changed meanings need an expand, migrate, contract rollout or an explicit version.
What is an API contract?
The set of requests and responses two services agree to exchange, including field meanings, status codes, authentication and retry behaviour, not only the JSON shape. Hatch sends the texting service { to, message }. The provider promises to validate those fields, attempt delivery, and return documented success or error responses. Neither side needs the other's implementation. They do need the same meaning at the boundary.
That agreement is the API contract. It includes more than JSON shape: field meaning, required and optional inputs, status codes, authentication, rate limits, retry behavior, and any ordering or consistency guarantees callers rely on.
A rename plus missing validation creates a false success
Hatch renames to to phone, but the provider still reads to. A correctly validated provider rejects the malformed request with a client error. The Chapter 2 incident is worse because the sample provider is deliberately lenient: it ignores the unknown field, fails to require to, and reports 200 OK even though it sent nothing.
Use the simulator to compare that false success with a handler that enforces its published contract.
The interactive version of this example is available when JavaScript loads.
What counts as a breaking API change?
Any change an existing reader cannot absorb, which is a question about pairs of versions rather than about the change on its own: the same rename is harmless when every reader ignores unknown fields and fatal when one validates strictly. Compatibility labels vary across API and data-schema tools, so start with reader and writer versions. Can the new reader understand data written by the old writer? Can the old reader understand data written by the new writer?
For an HTTP call, apply that question twice. The client writes the request and the server reads it. The server writes the response and the client reads it. A change can be safe in one direction and unsafe in the other. Schema registries often call new-reader/old-data backward compatibility and old-reader/new-data forward compatibility. State the viewpoint instead of relying on the label alone.
Classify a change with its assumptions visible
Work through a few changes and watch what the verdict actually depends on. The change itself is rarely enough to decide: the same edit is safe or fatal according to whether readers tolerate unknown fields, whether the field was required, and which side deploys first. The panel makes each of those an explicit input rather than a hidden assumption, which is the habit worth taking away from it.
The interactive version of this example is available when JavaScript loads.
| Change | Default verdict | Important condition |
|---|---|---|
| Add an optional response field | Usually compatible | Strict or closed-content readers may reject it. |
| Add a new endpoint | Compatible | Existing endpoints keep their behavior. |
| Rename or remove a field | Breaking | Old readers or writers still use the old name. |
| Make an optional request field required | Breaking | Previously valid requests are rejected. |
| Change a field type or meaning | Breaking | A schema check may miss a meaning-only change. |
| Return a new enum value | Conditional | Wire formats may accept it while exhaustive application code fails. |
How do you rename a field without breaking clients?
- Expand. Add
phonealongsideto. During the transition, accept both and use one documented precedence rule. - Migrate. Update callers and observe usage of the old field. Do not infer completion from a deploy list alone.
- Contract. Remove
toonly after supported callers have moved and the deprecation window has ended.
The same question appears in every format
| Style | Contract source | Evolution note |
|---|---|---|
| REST or JSON | OpenAPI and JSON Schema | Unknown-field behavior depends on the reader and whether the content model is open or closed. |
| gRPC | Protobuf schema | Keep field numbers stable and reserve removed numbers and names. A rename can be binary-wire compatible while breaking generated source, ProtoJSON, or TextFormat users. |
| GraphQL | GraphQL schema | Adding an output field is generally safe. A required argument or required input field is breaking. Deprecate before removal. |
| Events | Avro or Protobuf plus a schema registry | Choose backward, forward, full, and transitive checks based on which reader and writer versions must coexist. |
Schemas help when the pipeline enforces them
A schema file is a description until something rejects a change that contradicts it. The sample below is the enforcing half: the declaration, and the check that compares a proposed version against the published one and fails the build on an incompatible edit. Without that second part a schema records intent and catches nothing, which is how a rename reaches production with the document that forbids it sitting in the same repository.
The interactive version of this example is available when JavaScript loads.
Useful guardrails
- Generated clients and stubs reduce hand-written shape drift, but they do not verify runtime behavior or field meaning.
- Consumer-driven contract tests record what consumers rely on. Provider verification and deployment checks must run for those contracts to gate a release.
- Compatibility checks compare schema versions in CI or a registry before a producer publishes them.
- Runtime validation rejects malformed requests instead of returning a false success.
- Deprecation telemetry tells you whether the old surface is still used.
Primary references
- OpenAPI Specification
- Protocol Buffers: updating a message type
- GraphQL specification: field deprecation
- Confluent Schema Registry: compatibility types
- Pact: how consumer contracts and provider verification work
- Stripe API versioning
Describe the coexistence window
By GOAT Engineer. Back to Concept Library. Privacy Policy · Contact.