Topic 2 of 5 · 4 min read
Versioning and backward-compatible changes
Goal: Classify a list of proposed API changes as breaking or non-breaking and plan a deprecation for the breaking ones.
A change is breaking if a client that worked yesterday, without being edited, can fail today; review every generated API change by asking that one question. An assistant asked to "clean up the response" will happily rename, remove and retype fields. It sees the code. It does not see the mobile app released 14 months ago that still calls it.
Check yourself · 1 of 3
Which change to a JSON response is usually non-breaking?
Why it matters
You control the server. You do not control the clients: old app builds, partner integrations, scripts and other services deploy on their own schedule. A breaking change shows up as a crash in someone else's product.
Breaking or not
- Usually safe (additive): a new optional response field, a new endpoint, a new optional request parameter, a new enum value only if clients are told to expect unknown values.
- Breaking: removing or renaming a field, changing a type (
"id": 17to"id": "17"), making an optional request field required, tightening validation, changing a default, changing a status code or error code, changing the meaning of an existing value.
Clients that break on unknown fields are a bug on their side, but a rule like "be liberal in what you accept" only helps if you document it.
How to handle a breaking change
- Prefer an additive path. Add
full_nameand keepname. Add the new endpoint and keep the old one. - Version when you must. URL versions (
/v2/orders) are easy to see, route and cache. Header versions (Accept: application/vnd.shop.v2+json) keep URLs clean but are easy to forget. Pick the project's existing choice. - Deprecate in the open. Announce the date, add a
DeprecationorSunsetresponse header (RFC 8594 definesSunset), log which clients still call the old version, and remove it only when usage is near zero or the date passes. - Test against the old contract. Keep the previous OpenAPI file and run a diff in CI that flags removed fields.
Before and after
{
- "name": "Ada Lovelace",
+ "first_name": "Ada",
+ "last_name": "Lovelace",
"id": 17
}
This removes name. Reviewed: keep name, add the two new fields, mark name deprecated in the spec, and set a sunset date for v2.
What goes wrong
- Silent type changes. An int becoming a string passes the server tests and breaks typed clients.
- "Internal" endpoints. Nothing is internal once someone has called it.
- Removing on the announced date without checking usage. A date is a plan, not proof that clients moved.
Examples
A type change in a tidy-up
- "id": 17,
+ "id": "17",
The model made ids strings "for consistency with the other service". Typed clients that parse an integer now fail. Treat it as breaking: keep the integer, or ship the change in v2 with a sunset date for v1.
Required becomes optional, or the reverse
required: [email]
+ required: [email, phone]
Existing callers do not send phone, so every one of them now gets a validation error. Making a request field required is breaking. Keep it optional and default it on the server.
Key points
- A change is breaking when an unmodified, previously working client can fail, so ask that question of every diff.
- Adding optional fields and endpoints is usually safe, while removing, renaming, retyping and tightening are breaking.
- Prefer an additive change and keep the old field until you can show clients have moved.
- Choose URL or header versioning once, follow the project's choice, and signal deprecation with dates and a Sunset header.
- Compare the new OpenAPI file with the old one in CI so removed or retyped fields are caught automatically.
Common mistakes
- Judging a change by whether the server tests still pass.
- Deleting the old field in the same release that adds the new one.
- Bumping the version number without a way to see who still uses the old version.
- Forgetting that error codes and status codes are part of the contract too.
Check yourself · 2 of 3
An assistant makes the phone request field required on POST /signups. Existing clients do not send it. How do you classify this change?
Check yourself · 3 of 3
You must remove an old endpoint. What is the best plan?
Practice items, not counted towards your mastery.
Start here
- Google API Improvement Proposals (AIP) (opens in a new tab)DocsFree
A large, consistent rulebook for resource naming, standard methods, pagination, errors and compatibility.
- RFC 9457: Problem Details for HTTP APIs (opens in a new tab)StandardFree
The standard JSON error shape with type, title, status, detail and instance members.
- Idempotent requests (Stripe API reference) (opens in a new tab)DocsFree
A widely copied idempotency-key design: key reuse, parameter mismatch and key expiry.
More for this topic
- RFC 8594: The Sunset HTTP Header Field (opens in a new tab)StandardFree
A header that tells clients the date an endpoint will stop working.