Versioning
URI, header and media-type versioning, what a breaking change is, and the deprecation process that keeps clients from being surprised.
Versioning is the subject people reach for first and need least. Most APIs that carry a /v2 got there by making a change they did not have to make, and the version then has to be maintained forever by a team that wanted to rename one field.
So this lesson is in two halves: what actually breaks a client, and — only once that is clear — how to version when you genuinely must.
What breaks a client, and what does not
A client breaks when something it relied on changes. That is narrower than it sounds, and the two lists are worth knowing precisely.
Compatible — safe to ship without a version:
- Adding a new field to a response. A client that ignores unknown fields is unaffected, and any sensible parser does.
- Adding a new optional request field.
- Adding a whole new endpoint.
- Adding a new value to an enum-like field — if clients were told to expect that.
- Relaxing a validation rule: accepting something you used to reject.
Breaking — needs a version, or a migration plan:
- Removing or renaming a field, in either direction.
- Changing a field's type —
"1999"to1999breaks every strictly typed client. - Making an optional request field required.
- Tightening validation: rejecting something you used to accept.
- Changing a status code for the same condition.
- Changing the meaning of a field while keeping its name and type. This is the worst one, because nothing detects it — not a schema diff, not a contract test — and the client keeps working while doing the wrong thing.
That last item deserves its own sentence. Changing total from including tax to excluding tax is invisible to every automated check you have and wrong in every client that reads it.
The change you cannot make
Some changes have no compatible form. Splitting name into firstName and lastName cannot be done by addition alone, because the old field has to go eventually, and "eventually" is where the work is.
The pattern that works is the one from the migrations lesson, applied to a contract instead of a schema: expand, migrate, contract.
- Expand. Add
firstNameandlastNamebesidename. Populate all three. No client is affected. - Migrate. Document
nameas deprecated, announce it, and — this is the step people skip — measure who is still sending or reading it. Log usage per client; you cannot remove what you cannot see. - Contract. When the number reaches zero, or the remaining callers have agreed a date, remove it.
That takes months, and the months are the point: they are what a version number would have cost you anyway, without the second codebase.
Three ways to say which version
When you do need one, there are three places to put it, and the argument between them is smaller than the internet suggests.
In the URI — /v1/orders, /v2/orders:
- Visible in a log, a browser and a curl command; trivially routable at a gateway.
- Purists object that the resource has not changed, only its representation, so it does not belong in the identifier.
- The common choice, for the practical reasons above.
In a header — X-API-Version: 2:
- Keeps the URL stable.
- Invisible. A developer pasting a URL gets an unversioned default and different behaviour from their colleague, and nothing in the request makes that obvious.
In the media type — Accept: application/vnd.example.order.v2+json:
- Formally the most correct: content negotiation is exactly the mechanism for "same resource, different representation".
- Almost nobody does it, tooling handles it poorly, and it makes a browser useless for exploration.
Pick URI versioning unless you have a specific reason not to, version the whole API rather than per endpoint — per-endpoint versions produce a matrix nobody can reason about — and use a major number only. v1.2 implies a compatible change, and compatible changes do not need a version.
Announcing a removal
A deprecation nobody is told about is an outage with a longer fuse. The mechanics are standardised and cost almost nothing:
Deprecation: Sun, 01 Nov 2026 00:00:00 GMT
Sunset: Sun, 01 Feb 2027 00:00:00 GMT
Link: <https://api.example.com/docs/migrating-to-v2>; rel="deprecation"Deprecation says when it became deprecated; Sunset (RFC 8594) says when it stops working; the Link says where to read about it. A client's own monitoring can pick these up, which is the entire reason to send them as headers rather than only writing a blog post.
Alongside that: log which clients use the deprecated path, and tell them individually. "We emailed the list" is not a migration plan when one integration is a customer's billing system.
Running two versions
If you ship a v2, decide early how much of the code is duplicated, because the honest answer is "as little as possible":
- One service, two sets of DTOs and controllers, one domain underneath. The versions differ in their contract, not in what the system does. This is almost always right.
- Two deployments is a last resort — double the operational surface, and every bug fix needs porting.
And set an end date when you launch, not when you get tired. An API version with no announced sunset is one that will still be running in five years, because nothing forces the conversation.
Under the hood: how a server actually dispatches on each kind of version
The three approaches this lesson compares are not equally supported by a typical framework's routing, and the gap is the practical reason URI versioning ends up being the default rather than merely the popular one.
URI versioning needs no special mechanism at all. /v2/orders is, to Spring's RequestMappingHandlerMapping, an ordinary path pattern — matched by the same trie/pattern lookup as every other route. There is nothing "version-aware" in the dispatcher; /v1/orders and /v2/orders are simply two unrelated URL patterns that happen to be handled by different controller methods. This is exactly why it is trivially routable at a gateway too: a reverse proxy that has never heard of your versioning scheme can still route /v1/* and /v2/* to different backends on path alone.
Header versioning needs the dispatcher to consider more than the path. Spring's @RequestMapping supports a headers attribute (headers = "X-API-Version=2") that participates in the match via a HeadersRequestCondition — so it is supported, but two controller methods now share the same path pattern and are disambiguated only by inspecting a header, which is real additional matching work on every request and a real source of "which method actually handled this" confusion when debugging, compared to a URI's dispatch being visible in the path alone.
Media-type versioning is real HTTP content negotiation, and that is both its formal correctness and its practical cost. Accept: application/vnd.example.order.v2+json is matched through the same produces mechanism (ProducesRequestCondition) that ordinary content negotiation uses (RFC 9110 §12 defines the Accept header and the negotiation it enables) — but the vendor-specific media-type suffix (vnd.example.order.v2+json) is not something a default JSON message converter parses out of the box; the version portion of the media type is opaque to Jackson, and the mapping from that string to "which DTO/schema to bind against" has to be something you build. Formally most correct, and the tooling gap this lesson already flags is exactly this — the mechanism exists in the spec, but not, by default, in the framework's automatic handling of it the way path and header matching already do.
Try it yourself
Work out what a missing Vary header actually does to two clients
An API versions by header (X-API-Version: 1 or 2) and sits behind a CDN that caches GET responses by URL for sixty seconds, with no Vary configured. Client A, running v1, requests GET /orders/42 and gets a v1-shaped response, which the CDN caches under the key GET /orders/42. Eight seconds later, Client B, running v2, requests the same URL with X-API-Version: 2. What does Client B actually receive, and whose fault is the resulting bug?
Why this is a caching-layer bug wearing a versioning costume
Client B receives the cached v1-shaped response — the CDN never re-asked the origin, because as far as its cache key is concerned GET /orders/42 is GET /orders/42 regardless of what header rode along with it, and a fresh cached entry exists for exactly that key. Client B's code, written against the v2 contract, now parses a v1 body and either throws on a missing field or silently misreads one that changed meaning. It is not a bug in the versioning scheme or in either client — it is a cache that was never told the response varies by a header the API's own dispatcher considers, which is precisely why Vary has to be set by whoever owns the versioning decision, not assumed as a caching-infrastructure default.
Misconceptions
- "Media-type versioning needs no extra machinery because content negotiation is built into HTTP." The mechanism (
Accept,producesmatching) is built in; parsing a vendor-specific suffix likevnd.example.order.v2+jsoninto "which version's schema applies" is not something a default JSON converter does — that mapping is something you write, which is a real part of why "almost nobody does it" despite being formally correct. - "Header-based versioning is automatically cache-safe because it doesn't touch the URL." The opposite is closer to true by default: a cache that keys on URL alone treats every version as the same cached entry unless
Varyis explicitly set on the header carrying the version, which is extra configuration URI versioning never needs.
Sources
- RFC 9110, HTTP Semantics — §12 content negotiation (
Accept) and §12.5.5 theVaryheader. - RFC 8594, The Sunset HTTP Header Field.
- Roy T. Fielding, Architectural Styles and the Design of Network-based Software Architectures (2000) — the REST dissertation this lesson's "the resource hasn't changed, only its representation" objection to URI versioning comes from.
- Spring Framework reference documentation — request mapping by headers and by
produces/consumes.