API Versioning
How to change an API over time without breaking the apps and clients that already use it.
What is it?
Once an API is live, you rarely control everything that calls it. A mobile app may be on thousands of phones, some not updated for months. A partner may have built an integration against your endpoints. If you rename a field, change what a status code means, or remove something you thought was unused, those callers can break the moment you deploy.
API versioning lets you change an API while existing clients keep working as before, usually by running several versions of an endpoint side by side, at least for a while. Two common approaches:
- URL versioning:
/v1/usersvs./v2/users. - Header versioning: the same URL, with the client naming a version in a request header.
Explain like I'm 10
Think of a road under construction. You don't close the only bridge into town overnight. You build a new bridge next to the old one, run both for a while, and remove the old one only when you're sure nobody needs it.
Examples
URL versioning
// v1: returns a flat "name" field
app.get("/v1/users/:id", (req, res) => {
res.json({ id: req.params.id, name: "Ada Lovelace" });
});
// v2: splits name into first/last, without touching v1's contract at all
app.get("/v2/users/:id", (req, res) => {
res.json({ id: req.params.id, firstName: "Ada", lastName: "Lovelace" });
});Old clients keep calling /v1/users and get the same response shape as always. New clients can opt into /v2/users for the improved shape.
Header-based versioning
app.get("/users/:id", (req, res) => {
const version = req.headers["api-version"] || "1";
const user = { id: req.params.id, name: "Ada Lovelace" };
if (version === "2") {
const [firstName, lastName] = user.name.split(" ");
return res.json({ id: user.id, firstName, lastName });
}
res.json(user);
});The URL never changes. The version the client sends in a header decides the response shape.
How it works
Instead of changing an endpoint in place, you add a new version next to it. That can be a new URL prefix (/v2/...), or a version read from a header (sometimes a query parameter) that the code branches on.
Clients that send no version, or ask for the old one, keep the original behavior indefinitely (or until an announced deprecation date). New clients adopt the new version when they're ready.
Why does it exist?
An API is a contract: callers trust that its shape and behavior won't change under them. But software must evolve: fields get renamed, responses improve, concepts get restructured. Versioning lets you improve the design without being stuck with every past decision, and without breaking clients you don't control or can't force to update.
When to use it
Add a new version when you need a breaking change (removing a field, changing a field's type or meaning, changing what a status code means) to an API that has external or independently deployed consumers.
When not to use it
Purely additive changes, like a new optional field or a new endpoint, usually don't need a new version: well-behaved clients ignore fields they don't recognize. Versioning non-breaking changes just adds maintenance.
Common mistakes
Treating every change as breaking and creating new versions far more often than needed.
Adding new versions but never retiring old ones, leaving an ever-growing pile to maintain.
Changing an existing version's behavior 'just this once' instead of creating a new version, breaking clients who trusted it to stay stable.
Practice exercises
- Easy:
Explain the difference between a breaking and a non-breaking API change, with one example of each.
- Medium:
Design /v1/ and /v2/ routes for a
/products/:idendpoint where v2 adds a new required field that v1 didn't have. - Hard:
Propose a plan for retiring an old API version: how would you warn existing clients, and how would you decide when it's safe to remove it?
Interview questions
Why is API versioning necessary?
An API is a contract other systems depend on, and breaking it can silently break clients you don't control. Versioning lets the API evolve while existing consumers keep working.
What's the difference between URL versioning and header versioning?
URL versioning puts the version in the path (like /v2/users), so it's visible and cacheable. Header versioning keeps one URL and the client names a version in a request header, so URLs stay stable.
Does every change to an API require a new version?
No. Usually only breaking changes (removing or restructuring fields, changing behavior clients rely on) need one. Purely additive changes usually don't.
What generally counts as a breaking change to an API?
Removing or renaming a field, changing a field's type or meaning, changing what a status code means, adding a new required request parameter, or restructuring the URL. Anything an existing, well-behaved client can't just ignore.
Give an example of a change that looks risky but usually isn't breaking.
Adding a new optional field to a response. Well-behaved clients only read fields they know and ignore the rest, so it doesn't need a new version.
Why is URL versioning considered more visible than header versioning?
The version is in the path, so it shows up in address bars, server logs, proxy config and cache keys. Nothing is hidden in request headers.
What's a downside of URL versioning?
You maintain two or more full sets of routes for one resource (/v1/users and /v2/users), which means duplicated route logic and more to keep working as versions pile up.
What's a downside of header versioning?
It's less discoverable: you can't paste a URL into a browser to see a version. Caching is trickier too, since one URL can return different bodies depending on a header a cache may not vary on by default.
In the header-versioning example, what version does a request get if it sends no `api-version` header at all?
Version 1. The handler reads req.headers["api-version"] || "1", so a missing header falls back to the original behavior instead of failing or using the newest version.
Why does defaulting a missing version header to the original version matter for backward compatibility?
Clients built before versioning existed keep working with no changes, instead of having to opt into 'version 1' explicitly to avoid breaking.
What's an alternative to a custom header for expressing an API version, using a standard HTTP mechanism?
Content negotiation with the Accept header, e.g. Accept: application/vnd.myapi.v2+json. The version uses HTTP's existing mechanism for choosing a response format instead of a custom header.
Why doesn't API versioning map cleanly onto semantic versioning (major.minor.patch)?
Semantic versioning tracks every release, including non-breaking additions. API versions usually only change for breaking changes, so a long run of additive changes may never need a new API version.
What is a deprecation window (or sunset period)?
An announced period when an old API version still works as before but is marked for removal, giving clients time to migrate.
How might a server communicate an upcoming version retirement to its clients?
With a Sunset or Deprecation response header, a warning field in the response body, changelog and docs entries, or direct contact with known API consumers.
Why is silently changing an existing version's behavior worse than releasing a proper new version?
It breaks the contract clients trusted, with no warning and no way to opt out or prepare. A new version keeps the old behavior running and makes the new one opt-in.
Scenario: you need to change a live endpoint's field from a string to a number. What's the safe way to do it?
Don't change the field's type in place. Either make the change in a new version, or add a new field next to the old one and deprecate the old field. Either way, clients relying on the current type keep working.
Why can adding a new *required* field to a request body be breaking, even though adding fields sounds additive?
Existing clients don't send the new field, so their requests fail validation as soon as it's required. Additive changes are only safe in responses, where unknown fields are ignored; a new required request field is something old clients can't provide.
What's the practical risk of letting old API versions pile up indefinitely?
Each version adds maintenance, testing and security-patching work, plus more room for subtle bugs. The mistake isn't adding versions; it's never retiring unused ones.
Debugging: a client explicitly requesting version 1 via the header is receiving the version 2 response shape. What's a likely bug?
The version check is probably wrong: comparing against the wrong type or string, or branching so the v2 path runs before the version check happens.
Why does API versioning usually require maintaining separate documentation per version?
Docs must describe each version's actual contract. Docs for the current shape would be wrong for a client still calling an older, differently shaped version.
Trap: a team assumes a breaking change to a GET endpoint is low-risk because 'nobody really uses GET responses directly.' Why is that reasoning unsafe?
Base versioning decisions on the resource's contract, not guesses about usage. Any independently deployed consumer could be parsing that GET response's exact shape, and you can't be sure none are.
How does API versioning differ from a feature flag, given that both can change server behavior for some requests but not others?
A feature flag toggles internal behavior in code you fully control, often for gradual rollout. Versioning keeps a stable, documented contract for external or independently deployed clients that can't be made to flip a flag on your schedule.
Follow-up: once traffic to an old version has dropped to nearly zero, is it safe to delete it immediately?
Not necessarily. 'Nearly zero' isn't zero, and a caller may still quietly rely on it. Confirm with logs and metrics, announce a firm removal date, and remove it only after that grace period.
Advanced: how can an API gateway reduce the internal cost of supporting multiple externally-visible versions?
The gateway can translate older-version requests and responses into what the current internal implementation expects. The backend keeps fewer diverging code paths while still exposing several versions externally.