Once other people depend on your API, every change is a negotiation with code you cannot see. The best versioning strategy is to need versions rarely.
Changes that are safe
Adding is usually safe if clients ignore what they do not know:
- A new optional field in a response.
- A new endpoint.
- A new optional request parameter with a sensible default.
This works only if clients are written to tolerate unknown fields. State that rule in the documentation.
Changes that break clients
- Removing or renaming a field or endpoint.
- Changing a field's type or meaning, such as seconds becoming milliseconds.
- Making an optional parameter required.
- Tightening validation so formerly accepted input now fails.
- Changing error formats or status codes that clients handle.
Where to put the version
- In the URL:
/v2/orders. Obvious, easy to route and to cache. The most common choice. - In a header:
Accept: application/vnd.example.v2+json. Keeps URLs clean but is less visible and harder to try in a browser. - Date-based versions: a client pins the API behaviour as of a date, and the server keeps compatibility shims. Powerful, but needs discipline.
{
"id": 42,
"total": 1999,
"currency": "USD",
"total_formatted": "19.99"
}Adding total_formatted next to total is compatible. Replacing total with a string would be a new version.
Deprecation process
- Announce the change with a date, in docs and by email to registered developers.
- Mark old endpoints with a
DeprecationorSunsetheader so tools can detect them. - Measure who still calls the old version, and contact them.
- Remove only after the announced date, and only when usage is negligible.
Internal APIs
Services you control can use expand and contract: add the new field, migrate every consumer, then remove the old field. No version number needed if you coordinate.
Takeaway
Prefer additive changes, document the tolerance rule, and when a break is unavoidable give clients a long, visible and measured migration path.
Comments