How to Implement API Versioning with URL Path
Version an API by embedding the version in the URL path, with backward-compatible defaults, deprecation headers, and sunset tracking.
On this page
Introduction
API versioning lets you evolve the API without breaking existing clients. The URL path approach embeds the version in the path, like /v1/users and /v2/users, so each version is a distinct set of routes. This is the most visible and cache-friendly approach: clients see the version in the URL, and caches key on the full path. This guide implements path-based versioning with deprecation headers and sunset tracking.
The problem
Without versioning, a breaking change breaks every client. With versioning, you can ship v2 with the new shape while v1 keeps working. The challenge is managing the lifecycle: knowing when to deprecate, how to communicate the migration, and when to sunset. A good versioning strategy makes the version explicit, the deprecation visible, and the sunset data-driven.
Design decisions
Each version is mounted under a prefix: app.use(‘/v1’, v1Routes) and app.use(‘/v2’, v2Routes). The routes share core logic but use version-specific adapters for response shapes. When v2 ships, v1 gets Deprecation and Sunset headers pointing to the migration guide. The server logs the version and endpoint for every request, so a dashboard shows v1 usage over time. When v1 usage drops below a threshold, the routes are removed and a 410 Gone response replaces them.
app.use('/v1', v1Router);
app.use('/v2', v2Router);
// Deprecation middleware on v1
v1Router.use((req, res, next) => {
res.set('Deprecation', 'true');
res.set('Sunset', '2026-12-31');
res.set('Link', '</v2/users>; rel="successor-version"');
next();
});
Implementation
The v1 and v2 routers share the same handlers but transform the response. A v1 adapter wraps the core handler and maps the v2 response to the v1 shape, stripping new fields or renaming old ones. The deprecation middleware runs on v1 and sets the headers. The usage log records the version, endpoint, and status code; a scheduled job aggregates the data and alerts when v1 usage drops below the sunset threshold. On sunset, the v1 router is replaced with a 410 handler that links to the migration guide.
// v1 adapter: strip v2-only fields
function v1Adapter(handler) {
return async (req, res) => {
const result = await handler(req);
const { newField, ...v1Shape } = result;
res.json(v1Shape);
};
}
Testing the boundary
Test that /v1/users returns the v1 shape and /v2/users returns the v2 shape. Test that v1 responses include Deprecation and Sunset headers. Test that a request to a sunset version returns 410 with a Link to the migration guide. Test that the usage log records the correct version for each request. Test that v1 and v2 can coexist without interference.
Common pitfalls
Avoid creating a new version for non-breaking changes; add fields or endpoints to the current version instead. Avoid supporting too many versions; each version is maintenance cost. Avoid sunsetting without notice; announce the sunset date in headers and docs well in advance. Avoid versioning by query parameter or header; it breaks caching and is less discoverable. Avoid copying v1 to v2 without refactoring; v2 should fix design mistakes, not repeat them.
Operational considerations
Map each URL path prefix to a handler version in a routing table that is explicit and versioned itself, so adding a version does not silently change an existing route. Keep the v1 handler frozen once v2 ships: bug fixes go to all versions, but new features go only to the current version. A frozen v1 that only receives security patches is cheaper to maintain than a v1 that keeps accreting features, and it gives clients a stable target while they migrate. Track the request volume per version so you can decide when v1 is small enough to sunset.
Sunsetting is a process, not a flag. Start by adding a Deprecation header to v1 responses with a link to the migration guide, then add a Sunset header with the planned removal date once you have a date. Log every v1 request with the client identifier, so you can reach out to high-volume v1 callers before the deadline. On the sunset date, return 410 Gone with a body that points to the migration guide, not a generic 404. A 410 is a stronger signal than a 404 and tells automated clients the version is intentionally gone.
Backward-compatible changes do not require a new version: adding a field to a response, adding an optional parameter, or loosening a validation rule are all safe under v1. Breaking changes require a new version: removing a field, changing a field type, tightening a validation, or changing the semantics of an existing field. When in doubt, ship the change as a new version, because rolling back a breaking change in the same version is impossible once clients have adapted to it. Keep a changelog per version that classifies every change as compatible or breaking, so the decision history is visible.
Document each version with its own OpenAPI specification, not a single spec with versioned paths. A per-version spec lets clients generate a client for exactly the version they target, without filtering. Publish the specs at stable URLs so tooling can fetch them. When a version is sunset, keep the spec available with a deprecation notice, because clients debugging a sunset integration still need the reference. Remove the spec only when the version is fully removed from the router.
Conclusion
URL path versioning makes the API version explicit, cache-friendly, and easy to route. Mount each version under a prefix, share core logic with version-specific adapters, and add deprecation headers when a version is superseded. Track usage to decide when to sunset, and replace sunset routes with a 410 that links to the migration guide. The result is an API that evolves without breaking clients.
Sources: API versioning, RFC 8594 Sunset.
FAQ
- When should I create a new version?
- Only for breaking changes. Non-breaking changes like adding fields or endpoints do not need a new version; clients that ignore unknown fields are forward-compatible.
- How long should I support an old version?
- Until usage drops below a threshold or a announced sunset date; give clients at least six months to migrate.
- Should I use URL path or header versioning?
- URL path is the most visible and cache-friendly; header versioning is less discoverable and harder to cache.
Report a problem with this articleOpens a pre-filled issue on GitHub.