How-To Guide

How to Implement API Versioning Strategies

Compare URI, header, and media-type versioning, design a deprecation policy, and evolve an API without breaking existing clients.

On this page

  1. Introduction
  2. Compare the versioning schemes
  3. Define what breaks compatibility
  4. Announce and enforce deprecation
  5. Route and isolate versions
  6. Test every supported version
  7. Common pitfalls
  8. Conclusion

Introduction

An API is a contract with clients you may not control. The moment a field is renamed or a response shape changes, every consumer that depended on the old form can break. Versioning is how you keep that contract while still evolving. The hard part is not choosing a syntax; it is deciding what counts as a breaking change, how long an old version lives, and how to tell clients when a change is coming.

This tutorial compares the three common versioning schemes, defines a compatibility policy that keeps most changes backward compatible, and shows how to announce and enforce deprecation. The examples use Node.js, but the strategy is independent of the framework and the language.

Compare the versioning schemes

URI path versioning puts the version in the address, for example /v1/orders. It is the most visible and the easiest to route, cache, and test, because a version is a distinct URL. Media-type versioning passes the version inside Accept or a custom Content-Type such as application/vnd.example.v2+json, keeping URLs stable and letting clients negotiate, but few teams read media types fluently and tooling support is uneven. A custom header such as X-API-Version sits between the two: URLs stay clean, but the version is invisible in a browser and easy for a client to omit accidentally.

The right choice depends on the audience. Public APIs favor visibility and cacheability, which points to path versioning. Internal APIs favor stable URLs and often already agree on a header. Whichever you choose, decide once and apply it uniformly; mixing schemes makes the deprecation story confusing because some clients version by path and others by header.

function resolveVersion(req) {
  const pathMatch = /^\/v(\d+)\//.exec(req.url ?? '');
  if (pathMatch) return { version: Number(pathMatch[1]), stripped: req.url.replace(/^\/v\d+/, '') };
  const headerVersion = req.headers['x-api-version'];
  if (typeof headerVersion === 'string' && /^\d+$/.test(headerVersion)) {
    return { version: Number(headerVersion), stripped: req.url ?? '/' };
  }
  return { version: 1, stripped: req.url ?? '/' };
}

Define what breaks compatibility

Most changes can be additive and safe. Adding a new optional field to a response, adding a new optional request parameter, adding a new endpoint, and adding a new enum value that clients tolerate are backward compatible. Removing or renaming a field, changing a field’s type, making an optional parameter required, changing a default, and changing the meaning of an existing value are breaking. Write this list down, because the decision is otherwise relitigated in every pull request.

A useful discipline is to design requests to tolerate unknown response fields. Clients that ignore fields they do not recognize survive additive changes without modification, which lets a single version live far longer. Publish this expectation in your client guidance and enforce it in your own SDKs. For request payloads, ignore unknown fields on the server where you can, so a newer client talking to an older server does not fail on an extra key.

Announce and enforce deprecation

When a change is genuinely breaking, introduce a new version and deprecate the old one on a schedule. Send Deprecation and Sunset headers on responses from the old version so clients and monitoring tools see the notice, and document the sunset date where clients look for it. Track usage per version: you can only safely remove a version when the traffic attributed to it reaches zero.

Sunset headers are standardized by RFC 8594 and are a reliable, machine-readable signal. Combine them with a changelog and an email or in-console notice for the humans. The goal is that no client is surprised by a removal; a version that disappears while still in use is an outage you caused on purpose.

function applyDeprecation(res, version, sunsetDate) {
  if (version < CURRENT_VERSION) {
    res.setHeader('Deprecation', 'true');
    res.setHeader('Sunset', sunsetDate.toUTCString()); // RFC 8594
    res.setHeader('Link', '</docs/migrate-v2>; rel="deprecation"');
  }
}

Route and isolate versions

Implement each version as a separate handler or module rather than branching on the version inside shared code. When versions share a handler full of if version === 2 checks, the logic rots and a change for the new version can silently affect the old one. Route the resolved version to the right handler at the edge, and let each version own its serialization. Shared internals such as the data layer stay common, but the request and response contracts are per-version.

Keep the version resolution in one place so every route behaves the same. A request that resolves inconsistently — path on some routes, header on others — produces bugs where a client appears to get a different version depending on the endpoint.

Test every supported version

Run contract tests per version so a change to shared code cannot break a contract quietly. At minimum, assert the resolved version for a path request, a header request, and a request with neither, and assert that a deprecated version still works and carries the Deprecation and Sunset headers.

curl -s -o /dev/null -D - http://localhost:3000/v1/orders | grep -i -E 'deprecation|sunset'
curl -s -o /dev/null -w '%{http_code}\n' -H 'X-API-Version: 2' http://localhost:3000/v2/orders

Common pitfalls

Mixing path and header versioning, treating every change as breaking so versions multiply, treating no change as breaking and silently breaking clients, removing a version before traffic reaches zero, and branching on version inside shared handlers are the recurring mistakes. A written compatibility policy plus per-version contract tests addresses all of them.

Conclusion

Versioning is a contract-management problem, not a URL-format problem. Choose one scheme and apply it everywhere, keep additive changes inside the current version, isolate versions in separate handlers, and deprecate with Sunset headers and usage monitoring before removal. Self-check by resolving the version from a path request, a header request, and neither, and by asserting a deprecated version still responds with its deprecation notice. Done well, clients migrate on their schedule rather than during an outage.

Sources: MDN: HTTP caching and versioning guidance, RFC 8594: The Sunset HTTP Header Field, Google Cloud API design guide.

FAQ

Where should the version live: path or header?
A path segment such as /v1/orders is visible, cacheable, and easy to route, which suits public APIs. A custom header or media type keeps URLs stable and is often preferred for internal APIs, but it is harder to explore and to debug in a browser.
How long should I keep an old version?
Long enough to give clients a migration window, typically several months for public APIs and shorter for internal ones. Announce the sunset, send Deprecation headers, and monitor real usage so you remove a version only when traffic reaches zero.

Related Examples