API versioning is one of the hardest problems to get right. Too rigid and you cannot evolve; too loose and you break clients. Here are versioning strategies that balance backward compatibility with forward momentum.
Why version APIs at all
Versioning lets you make breaking changes without coordinating deployment across every client. Without it, you are locked to the lowest common denominator or you force synchronized releases.
Breaking changes include removing fields, changing types, altering error codes, or tightening validation. Any change that could break existing client code needs a version transition period.
URI path versioning
Put the version in the URL path: /v1/users, /v2/users. This is the most explicit and easiest to route, cache, and reason about.
Clients specify exactly which version they want. Proxies and load balancers can route based on path patterns. No ambiguity.
URI versioning trades REST purity for operational clarity. That is almost always the right trade.
Use when: You want maximum explicitness and simple routing logic. Most teams should start here.
Drawback: URLs change between versions, which some REST purists dislike. In practice, this rarely matters.
Header-based versioning
Use a custom header like API-Version: 2 or the Accept header: Accept: application/vnd.company.v2+json. The URL stays constant while the response format changes.
This keeps resource identifiers stable and works well for content negotiation. Good fit when versioning is primarily about representation format rather than resource structure.
Use when: You want RESTful resource stability or when versioning is mostly about response format (JSON vs XML, field naming conventions).
Drawback: Harder to debug, cache, and route. Clients must remember to set headers; missing version defaults can cause confusion.
Query parameter versioning
Append version to requests: /users?version=2. Simple to implement and visible in logs, but mixes versioning with actual query semantics.
Use when: You need something quick for internal APIs or when building on frameworks with minimal routing flexibility.
Drawback: Muddies query semantics. Hard to enforce (easy to omit parameter). Not recommended for public APIs.
Semantic versioning for APIs
Apply semantic versioning to API versions: v1.2.3 where major version breaks compatibility, minor adds features, patch fixes bugs.
This works better in theory than practice. Most teams find major versions sufficient: v1, v2, v3. Fine-grained versions add cognitive overhead without much benefit in HTTP contexts.
Recommendation: Stick to major versions for REST APIs. Use semantic versioning for client libraries and SDKs.
Deprecation and sunset policy
Versioning is only useful if you eventually retire old versions. Define a deprecation policy before you need it:
- Deprecation notice: 6 months before removal, mark version as deprecated in docs and add a Sunset header to responses.
- Monitor usage: Track which clients call deprecated endpoints. Reach out to stragglers.
- Grace period: Allow 3-6 months overlap where both versions work.
- Final cutoff: Return 410 Gone or redirect to new version after sunset date.
Never support more than two major versions simultaneously. Three or more becomes unsustainable.
Making changes without versioning
Not every change needs a new version. Additive changes are backward compatible:
- Adding optional request fields (with defaults)
- Adding response fields (clients ignore unknown fields)
- Adding new endpoints
- Relaxing validation rules
Design APIs to tolerate these changes. Clients should ignore unknown fields. Servers should accept extra fields and apply defaults for missing optional ones.
Robustness principle: Be conservative in what you send, liberal in what you accept.
Choosing a versioning strategy
For most teams, URI path versioning is the right default. It is simple, explicit, and operationally clear.
Use header versioning if you have strong REST purity requirements or if your API is primarily about content negotiation.
Avoid query parameter versioning for public APIs. It is too easy to omit and hard to enforce.
Version your client libraries too
If you publish SDKs, version them separately from the API. Pin each SDK version to a specific API version. This lets clients upgrade the library without changing API versions, or vice versa.
Example: sdk-2.3.0 targets api-v1, sdk-3.0.0 targets api-v2. Semantic versioning works well here.
Testing across versions
Run integration tests for all supported versions. Duplicate critical test cases across v1 and v2 during migration periods.
Use contract testing or schema validation to catch breaking changes before they ship. Tools like OpenAPI/Swagger diffs help identify breaking changes automatically.
Key takeaways
- Use URI path versioning (
/v1/resource) for most APIs — it is explicit and easy to operate. - Define a deprecation policy with sunset dates before you launch v2.
- Support no more than two major versions simultaneously.
- Make additive changes without versioning; version only for breaking changes.
- Version client SDKs separately from the API itself.
- Use automated tooling to detect breaking changes in pull requests.