Imagine your city decides overnight that all electric sockets will use a new pin shape. The next morning, every fridge, phone charger and TV in town stops working. Nobody would accept that. In real life, new sockets are introduced gradually, and old ones keep working for years.
APIs face the same problem. Once mobile apps, websites and partner companies depend on your API, a careless change can break them all at once, and you can’t force everyone to update their app on the same day. API versioning is how you change an API without pulling the plug on anyone.
What is API versioning?
API versioning means giving your API a version label (like v1, v2) so that when you make an incompatible change, old clients can keep using the old version while new clients move to the new one.
The key idea is backward compatibility: a change is backward compatible if existing clients keep working without any changes on their side. You only need a new version when you can’t stay compatible.
Breaking vs non-breaking changes
This is the most useful thing to learn about versioning: most changes don’t need a new version.
| Usually safe (non-breaking) | Breaking (needs a new version) |
|---|---|
| Adding a new endpoint | Removing or renaming an endpoint |
| Adding a new optional field to a response | Removing or renaming a field in a response |
| Adding a new optional input parameter | Making an optional input required |
| Making validation looser | Making validation stricter |
| Adding a new value that clients can ignore | Changing a field’s type (number → text) or meaning |
| Making things faster | Changing error codes clients rely on |
Two habits make safe changes much easier:
- Clients should ignore fields they don’t know. Then adding fields never breaks them. This is sometimes called being “tolerant in what you accept”.
- Never change what an existing field means. If
priceused to be in rupees, don’t quietly switch it to paise. AddpriceInPaiseinstead.
The common ways to version an API
1. In the URL path: GET /v2/orders/42
The most common style, used by many public APIs. It’s obvious, easy to test in a browser, easy to route in an API gateway, and caches treat each version as a different address. The downside: the “same” order now has two URLs.
2. In a header: GET /orders/42 with Api-Version: 2
Keeps URLs clean and stable. Some well-known APIs use a date as the version (for example, a header like 2024-06-01), so each client is pinned to the API as it was on a given date. Harder to try out by just pasting a link.
3. In the media type: Accept: application/vnd.shop.v2+json
The client asks for a specific format of the resource. Precise and favoured by some REST purists, but less common and less familiar to most developers.
4. In a query parameter: GET /orders/42?version=2
Simple, but easy to forget, and mixes versioning with filters. Less popular for public APIs.
There’s no single right answer. Pick one, use it consistently across the whole API, and document it.
Retiring an old version politely
Every version you keep running costs effort: more code paths, more tests, more security patches. So old versions are eventually retired. Doing it kindly looks like this:
- Announce early. Publish a deprecation notice with a clear end date, months ahead for public APIs.
- Signal it in responses. Add headers telling clients the version is deprecated and when it will stop working. (There are standard headers for this, such as
Sunset.) - Measure who still uses it. Your logs show which clients and API keys still call
v1. Contact the big ones directly. - Offer a migration guide listing every change and how to adapt.
- Switch it off gradually. Some teams do short planned “brownouts” first, briefly turning the old version off so stragglers notice, before removing it for good.
Versioning inside a company
Between your own microservices, the same rules apply, with one twist: you usually can’t update a service and all its callers at the exact same moment. So teams follow a safe sequence often called expand and contract:
- Expand: add the new field or endpoint while keeping the old one.
- Migrate: move every caller to the new one.
- Contract: remove the old one once nothing uses it.
It’s the same idea used for database changes during zero-downtime deployments.
Common misconceptions
“Every change needs a new version.” Most changes, such as adding endpoints or optional fields, are backward compatible. Save new versions for real breaking changes.
“Mobile apps update quickly.” Many users don’t update their apps for months. Old app versions will keep calling your old API long after you release a new one.
“Version numbers should change with every release.” API versions describe the contract, not your deploys. You might ship hundreds of releases on v2 without changing the contract.
Key takeaways
- API versioning lets old and new clients work side by side when you make incompatible changes.
- Prefer backward-compatible changes: add, don’t remove or rename.
- Common styles: URL path (
/v2/…), header, media type, query parameter. Pick one and be consistent. - Retire versions with early notice, deprecation headers, usage tracking and migration guides.
- Inside a company, use expand → migrate → contract.
Frequently asked questions
What is API versioning in simple words?
API versioning means labelling your API with a version, like v1 or v2, so you can make big changes in a new version while apps using the old version keep working.
What is a breaking change in an API?
A breaking change is one that makes existing clients fail, such as removing an endpoint, renaming a field, changing a field’s type or making an optional input required.
What is the best way to version a REST API?
There’s no single best way. Putting the version in the URL path, like /v1/, is the most common because it’s simple and visible. Header-based versioning keeps URLs clean. What matters most is choosing one style and using it consistently.
How long should you support an old API version?
Long enough for clients to migrate comfortably. For public APIs that’s often many months or more, with clear dates announced in advance. For internal APIs, it can be shorter because you can see and contact every caller.
What does deprecated mean for an API?
Deprecated means the version or endpoint still works for now, but is scheduled to be removed. Clients should move to the newer version before the announced end date.
Comments