Topic 035 / Core
API versioning and backward compatibility
Chapter 3.5 · API Contract Mechanics
What and why
A REST API (→ 31) in production already has callers depending on its current shape; any later change risks breaking them unless they update in lockstep with the server. API versioning is the discipline of changing a contract over time while keeping existing callers working — preferring an additive, backward-compatible change first, and reserving an explicit version break, with a published deprecation plan, for when a change truly cannot be additive. Builds on: → 31.
Diagram
Is the change additive (new optional field or new endpoint)?
yes ──► ship it; no version bump needed
no ──► introduce a new version (URI or header)
run both versions side by side for a stated deprecation window
retire the old version only once its traffic is gone
How it works
- Prefer additive, backward-compatible changes: new optional fields, new endpoints, new optional query parameters — existing callers are unaffected.
- When a break is genuinely unavoidable (removing or renaming a field a client relies on,
changing existing semantics), introduce a new version — commonly a URI segment
(
/v2/...) or a version header. - Run both versions side by side for a defined deprecation window.
- Publish the deprecation policy and a removal date before retiring the old version.
- Monitor old-version traffic to know when it's actually safe to retire — not just when the calendar says the window has closed.
Practical example
(illustrative). v1 returns {"name": "..."}. A new requirement
needs separate first and last names: adding optional first and last fields while
name keeps being computed from them is backward-compatible — every v1 caller keeps
working unchanged. Dropping name outright, with no transition period, breaks every one
of them at once.
When to use it
- Use when: the API has callers you don't fully control — a public API, or another team's service.
- Avoid when: skipping a deprecation window because the API is "mostly internal" — "mostly" is exactly where the surprise lives.
- Failure scenarios: removing a field with no warning breaks every caller simultaneously; keeping too many old versions alive indefinitely because no one tracked who still depends on them.
- Simpler alternative: for a single-team internal API deployed together with all its callers, a version number is often unnecessary — ship additive changes and coordinate breaking ones directly with the one team affected.
Practice
(process kind — fill the template; 15–20 min, no lab services). For Project 2 (URL shortener), write a one-page versioning plan: list two plausible future changes you'd ship as additive, and one that would force a new version, with its deprecation window. Done when the plan names two additive changes, one breaking change, and the breaking change's deprecation window.
Key takeaway
Additive changes need no version; a genuine break needs a new version and a published deprecation window, not a surprise. Done when you can: classify a proposed API change as additive or breaking before writing any code for it. Used next in → 36.
References
None — this topic makes no external claims.
Source directory