API versioning
medium 22 minPrograms you cannot update depend on your API, so some changes would break them.
Video lesson · Chapter 1 of 6
A price takes a new shape
API versioning in 75 seconds
1:15Reel coming soon
Theory
Read or listen first. About 2 min.
What it is
A published API is used by programs you do not own: apps on phones, scripts at partners, services of other teams. They were written against the answers they saw. API versioning is how you change the API while those programs keep working.
A breaking change forces existing callers to change. A compatible change does not. Versioning is needed only for the first kind, so the first skill is telling them apart.
A version is a label for one form of the contract. The caller says which form it expects, and the server answers in that form.
Why it exists
You cannot update everyone at once. A phone app from two years ago is still installed. A partner's script has no maintainer. If the price field turns from a number into an object, those programs fail, and their users blame you.
Never changing is no answer either: products grow. Versions give you room to improve, test with a few users and move people at their own pace. The price is that several forms must be built, tested and documented at the same time, which is why each version needs an end date.
A short history
2011
Stripe launches. It later writes that it has stayed compatible with every version of its API since then, using versions named by date.
June 2012
RFC 6648 advises against the X- prefix for new headers, so a custom version header should not start with X-.
August 2017
Stripe describes how it keeps old versions alive: small version-change modules transform today's response back, step by step.
May 2019
RFC 8594 defines the Sunset header: the date after which a resource is expected to stop responding.
November 2022
GitHub introduces calendar-based versions for its REST API, chosen with a header.
March 2025
RFC 9745 standardises the Deprecation header.
Words to know
- Breaking change
- A change that makes correct existing callers fail.
- Backward compatible
- Old callers keep working with the new server.
- Tolerant reader
- A client that ignores fields it does not know.
- Deprecated
- Still works, should no longer be used.
- Sunset
- The date when it is expected to stop working.
- Adapter
- Code that turns the current form into an older one.
- Pinning
- Remembering the version a caller started with.
How it works
Step through the idea, or press Play.
Is it breaking?. Adding is usually safe; removing, renaming and retyping are not
Understand it
Start here
Plugs and sockets
Imagine the sockets in your wall. Every lamp in the building was bought to fit them. One night somebody replaces them with a better design.
The lamp never went dark again, because the change was announced and both sockets lived side by side.
Why
Why version an API?
An example: three versions at once
| Version | State | Share of traffic |
|---|---|---|
| v1 | deprecated | 5% |
| v2 | stable | 80% |
| v3 | beta | 15% |
Benefits
- Add features without breaking clients
- Move people gradually
- Try a new version with beta users
- A client can go back to the old version
Costs
- Several versions to maintain
- More documentation
- More tests
- Deciding when the old ones end
The test
Breaking or not?
Usually safe
- Adding an optional request field
- Adding a response field, if clients ignore unknown ones
- Adding a new endpoint
- Making a required request field optional
- Raising a rate limit
Breaking
- Removing a field or an endpoint
- Renaming a field
- Changing a field's type or meaning
- Making an optional field required
- Changing error codes
- Stricter validation or smaller limits
The treacherous middle
| Change | Looks | But |
|---|---|---|
| A new value in an enum | safe | A client that handles every known value may crash on an unknown one |
| A response field may now be null | small | Code that calls it as text breaks |
| Another default sort order | invisible | Clients relied on newest first |
| A bug fix | good | Somebody depends on the bug |
| Much slower answers | no contract change | Client timeouts begin to fire |
v1 response v2 response: breaking{ { "name": "iPhone 15", "name": "iPhone 15", "price": 999 "price": { "amount": 999, "currency": "USD" }} }Do I need a version?
How
Four places for a version
| Strategy | Example | Good | Bad |
|---|---|---|---|
| URL path | /api/v1/products | Visible, easy to test, works in a browser, separate cache keys | URLs multiply; one thing has several addresses |
| Custom header | Shop-Api-Version: 2 | Clean URLs, the address stays the same | Hidden, harder to test; caches need Vary |
| Accept header | Accept: application/vnd.shop.v2+json | Standard content negotiation | Complex for clients; caches need Vary |
| Query parameter | /api/products?version=2 | Easy to switch | Easily forgotten; looks like a filter |
What well-known APIs do
Stripe
The path says /v1 and hardly moves. Date-named versions come in the Stripe-Version header. Without it, the account's default version is used.
GitHub REST
A date in the X-GitHub-Api-Version header. A version is supported for 24 months after the next one; later calls get 410 Gone.
Shopify
A dated version every quarter, supported for at least 12 months.
Twilio
A date in the path: /2010-04-01/.
Recommendation: for most teams the path is the best start: explicit, easy to route, test and read in logs. Move to dates and headers when you release breaking changes often and want each one to be small.
GET https://api.shopeasy.example/v1/productsGET https://api.shopeasy.example/v2/productsInside
One core, thin adapters
Old versions translate, they do not duplicate
The same product in three versions
| v1 | v2 | v3 |
|---|---|---|
id: 123 | id: 123 | id: 123 |
name: "iPhone" | name: "iPhone" | title: "iPhone" |
price: 999 | price: {amount: 999, currency: "USD"} | pricing: {amount: 999, currency: "USD", discount: null} |
| no stock | stock: 50 | inventory: {stock: 50, reserved: 5} |
A v1 request
Adapters
- One core, one set of rules
- Old versions are small
- Easy to delete
Copied code per version
- Bugs fixed three times, or once by mistake
- Rules drift apart
- Nobody dares to delete it
Numbers
What a version costs
Contract tests
Is 5% small?
Goodbye
The deprecation lifecycle
Four phases
- 1
Active
The recommended version. Receives features and fixes.
- 2
Deprecated
Works, gets only fixes. Docs, changelog and headers point to the successor and a migration guide.
- 3
Sunset warning
The date is public. Contact the owners of the remaining keys. Short planned outages (brownouts) wake up those who do not read mail.
- 4
Removed
Answer 410 Gone with a link to the guide, not a bare 404.
HTTP/1.1 200 OKDeprecation: @1796083200Sunset: Tue, 01 Jun 2027 00:00:00 GMTLink: <https://api.shopeasy.example/v3/products>; rel="successor-version", <https://docs.shopeasy.example/migrate-v3>; rel="deprecation"{ "data": [], "_deprecation": { "message": "v1 is deprecated, migrate to v3", "sunset_date": "2027-06-01", "migration_guide": "https://docs.shopeasy.example/migrate-v3" } }How long?
| Callers | Common practice |
|---|---|
| Your own web app | Days: you deploy it yourself |
| Your own mobile apps | Until old app versions are rare, or force an update |
| Partners and the public | 6 to 12 months at least; some give 24 |
| Banking, health, government | Follow the contract |
Release
Rolling out a new version
From beta to default
- Compare in shadow. Run both versions on real reads and compare the answers.
- Detect breaking changes in the build. Compare the API description with the published one.
- Record version and client per request. Without that you can never remove anything.
- Default for callers who name no version: pin them to the version they started with. Never move them silently to the latest.
Alternatives
Living without versions
GraphQL and gRPC usually evolve without version numbers: add fields, mark old ones as deprecated, remove them when nobody uses them. A REST API can work like that too, if you control the clients. See REST, GraphQL and gRPC.
Summary
The whole topic in one minute.
Change by adding: new optional fields, new endpoints. Break only for a good reason, and then under a new version that the caller chooses, by path, by header, by media type or by date.
Behind the edge there should be one core. Old versions are adapters that translate. Retire them in stages: announce, mark, warn with dates, measure who is left, then remove. The date alone is not enough. Look at the traffic before you pull the plug.
- Versioning improves an API without breaking existing clients.
- Adding is usually safe; removing, renaming, retyping and requiring are not.
- Path versioning is the clearest start; headers and dates suit frequent small changes.
- One core, thin adapters: fix once, delete easily.
- Deprecation says from when, Sunset says until when.
- Look at the traffic per client before you remove a version.