API versioning lets you change a web API without breaking the clients that already depend on it. Each version is a stable contract, so existing mobile apps, partner integrations and scripts keep working while new clients adopt the new behaviour. Without it, every breaking change becomes a coordinated, all-at-once migration.
Versioning is not needed for every change. Additive, backward-compatible changes (a new optional field, a new endpoint) usually ship in the current version. A new version is for breaking changes.
| Change | Breaking? |
|---|---|
| Adding a new optional response field | Usually no |
| Adding a new endpoint | No |
| Removing or renaming a field | Yes |
| Changing a field type (string to number) | Yes |
| Making an optional request parameter required | Yes |
| Changing status codes or error format | Yes |
| Changing authentication requirements | Yes |
Well-behaved clients ignore unknown fields (the tolerant reader principle), which is what makes additive changes safe. Document that expectation for your consumers.
The version is part of the path, for example /api/v1/orders. It is the most visible and easiest to route, cache and debug.
The client selects a version through a header, such as Accept: application/vnd.example.v2+json, or a custom header like X-API-Version: 2. URLs stay stable, but the version is less visible and harder to test in a browser.
The version is passed as ?version=2. It is simple, but it is easy to omit and complicates caching if proxies ignore the query string.
| Strategy | Pros | Cons |
|---|---|---|
| URI path | Obvious, easy to route and cache | URLs change between versions |
| Header / media type | Clean URLs, fits content negotiation | Harder to discover and test |
| Query parameter | Easy to add | Easy to forget, cache pitfalls |
Most public APIs pick URI path versioning for simplicity. Pick one strategy and apply it everywhere.
With Spring Boot you can version by path using separate controllers or mapped prefixes:
Version 2 splits name into firstName and lastName, which is a breaking change, so it lives under a new path. Both controllers can share the same service layer, with only the mapping to the response model differing. Spring Framework 7 and Spring Boot 4 also add first-class API versioning support in the web layer, so check the current reference documentation before building your own version resolver.
For header-based versioning in older setups, use the headers or produces attributes of @GetMapping:
Versioning only works if old versions eventually go away. A practical policy:
Deprecation and Sunset HTTP headers, which have standard definitions (RFC 9745 and RFC 8594).410 Gone after retirement.Keep the number of supported versions small. Supporting four versions at once multiplies testing and security work.
v1 already exists when you need v2.v1, v2); do not expose minor numbers in the URL.Yes, if more than one team or deployment unit consumes it and they cannot upgrade in lockstep. If one team deploys both sides together, versioning matters less.
Purists argue the URL should identify the resource, not the representation, so they prefer media type versioning. In practice, URL versioning is widely used and works well.
GraphQL APIs usually avoid explicit versions. They add new fields and mark old ones with the @deprecated directive instead.
Long enough for clients to migrate, commonly six to twenty-four months for public APIs. Set the period in advance and publish it.
API versioning protects clients from breaking changes and gives you freedom to improve your API. Choose one strategy, usually a path prefix like /v1, reserve new versions for breaking changes, share logic between versions, and retire old versions on a published schedule using the Deprecation and Sunset headers.