Difficulty: Intermediate
What are the common API versioning strategies, and how do you decide when a version bump is needed?
Versioning is a promise-management tool. Once external clients depend on your API, changing it is like changing a building's floor plan while people are living in it. Versions let you make big changes without evicting anyone. The best answer starts with the reminder that the cheapest version bump is the one you never need, by evolving compatibly.
First define breaking versus non-breaking. Non-breaking: adding a new optional field to a response, adding an optional request parameter, adding a new endpoint, adding a new enum value if clients were told to tolerate unknowns. Breaking: removing or renaming a field, changing a field type or meaning, making an optional parameter required, changing error codes or status codes relied upon, tightening validation, and changing default behaviour. Only breaking changes justify a new major version.
Strategy one is URI versioning: /v1/orders, /v2/orders. It is extremely visible, easy to route at the gateway, easy to cache and easy to test in a browser. Purists object that the URI identifies a resource, not a version, but it is the most common approach in practice, used by Twitter, Razorpay and many others.
Strategy two is header versioning. Either a custom header such as X-API-Version: 2, or media type versioning through content negotiation: Accept: application/vnd.example.v2+json. URLs stay clean and the resource keeps a single identity, but it is less discoverable, harder to test with a browser and awkward for some caches unless you send Vary on the header.
Strategy three is query parameter versioning, /orders?version=2. Simple, but easy to omit and tends to pollute cache keys. Rarely recommended.
Strategy four is date-based versioning, made well known by Stripe. Each account is pinned to the API version of its first request, such as 2024-06-20, and can opt into newer versions through a header. Internally, Stripe keeps a chain of small transformation modules that convert the latest response into older shapes. This gives fine-grained evolution with no giant v1 versus v2 forks, but needs serious engineering discipline.
Operational concerns matter more than the mechanism. Keep the number of live versions small, ideally two. Publish a deprecation policy with dates, send Deprecation and Sunset headers, and monitor which clients still call old versions so you can contact them. Share as much code as possible: do transformation at the edge of the service, mapping v1 and v2 representations onto one core domain model, rather than forking the codebase.
For internal or mobile APIs, remember that mobile apps cannot be force-upgraded, so the old contract has to live as long as old app versions exist. That argues for more conservative compatibility and sometimes a minimum supported version with a forced update screen.
My recommendation in an interview: default to URI major versions for simplicity, evolve additively within a version, and describe how you would deprecate.
GET /v1/orders HTTP/1.1
HTTP/1.1 200 OK
Deprecation: true
Sunset: Wed, 31 Mar 2027 23:59:59 GMT
Link: <https://docs.example.com/migrate-v2>; rel="deprecation"
Content-Type: application/json
Old versions tell clients they are deprecated, when they stop working, and where the migration guide lives.
const v1 = require('./routes/v1');
const v2 = require('./routes/v2');
app.use('/v1', v1);
app.use('/v2', v2);
// Both routers call the same service layer;
// only request/response mappers differ.
Version differences stay in thin mapping layers while the domain logic is shared.
versioning, URI versioning, header versioning, breaking changes