AlgoViz

API versioning

medium 22 min

Programs 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

0:00 / 3:56

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

  1. 2011

    Stripe launches. It later writes that it has stayed compatible with every version of its API since then, using versions named by date.

  2. June 2012

    RFC 6648 advises against the X- prefix for new headers, so a custom version header should not start with X-.

  3. August 2017

    Stripe describes how it keeps old versions alive: small version-change modules transform today's response back, step by step.

  4. May 2019

    RFC 8594 defines the Sunset header: the date after which a resource is expected to stop responding.

  5. November 2022

    GitHub introduces calendar-based versions for its REST API, chosen with a header.

  6. 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.

Step 1 of 3

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.

A better socket
One round socket

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

VersionStateShare of traffic
v1deprecated5%
v2stable80%
v3beta15%
Example numbers. Measure your own per client, not only in total.

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

ChangeLooksBut
A new value in an enumsafeA client that handles every known value may crash on an unknown one
A response field may now be nullsmallCode that calls it as text breaks
Another default sort orderinvisibleClients relied on newest first
A bug fixgoodSomebody depends on the bug
Much slower answersno contract changeClient timeouts begin to fire
Tell clients from the first day: ignore unknown fields and expect new enum values.
Text
v1 response                      v2 response: breaking{                                {  "name": "iPhone 15",             "name": "iPhone 15",  "price": 999                     "price": { "amount": 999, "currency": "USD" }}                                }
A client that computes price x 2 now fails. A compatible way: keep price and add a new field, price_details.

Do I need a version?

How

Four places for a version

StrategyExampleGoodBad
URL path/api/v1/productsVisible, easy to test, works in a browser, separate cache keysURLs multiply; one thing has several addresses
Custom headerShop-Api-Version: 2Clean URLs, the address stays the sameHidden, harder to test; caches need Vary
Accept headerAccept: application/vnd.shop.v2+jsonStandard content negotiationComplex for clients; caches need Vary
Query parameter/api/products?version=2Easy to switchEasily forgotten; looks like a filter
Old pages often show X-API-Version. New headers should not use the X- prefix (RFC 6648).

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.

Text
GET https://api.shopeasy.example/v1/productsGET https://api.shopeasy.example/v2/products

Inside

One core, thin adapters

Old versions translate, they do not duplicate

Clients of versions 1, 2 and 3 reach a gateway that picks the version. In the products service, version 1 and 2 go through an adapter and version 3 goes directly, all to one core that uses one database.
A bug in the core is fixed once for everyone. Removing v1 means deleting its adapter.

The same product in three versions

v1v2v3
id: 123id: 123id: 123
name: "iPhone"name: "iPhone"title: "iPhone"
price: 999price: {amount: 999, currency: "USD"}pricing: {amount: 999, currency: "USD", discount: null}
no stockstock: 50inventory: {stock: 50, reserved: 5}

A v1 request

Each adapter knows one step. A new version adds one step and leaves the others alone.

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

copies: 3×40×5=600adapters: 40×5+2×10×5=300\text{copies: } 3 \times 40 \times 5 = 600 \qquad \text{adapters: } 40 \times 5 + 2 \times 10 \times 5 = 300
Assumptions: 3 versions, 40 endpoints, 5 tests each. Adapters cover only the 10 endpoints that changed per version.

Is 5% small?

50,000,000×5%=2,500,000 a day≈29/s,×3≈87/s at peak50{,}000{,}000 \times 5\% = 2{,}500{,}000 \text{ a day} \approx 29/\text{s}, \quad \times 3 \approx 87/\text{s at peak}
Assumption: 50 million requests a day. Five percent can still be two and a half million failures a day, or a single partner's whole business.

Goodbye

The deprecation lifecycle

Four phases

  1. 1

    Active

    The recommended version. Receives features and fixes.

  2. 2

    Deprecated

    Works, gets only fixes. Docs, changelog and headers point to the successor and a migration guide.

  3. 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. 4

    Removed

    Answer 410 Gone with a link to the guide, not a bare 404.

Text
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" } }
Deprecation (RFC 9745) is a time in seconds after an @: here 1 December 2026. Sunset (RFC 8594) is an HTTP date. The block in the body is optional and your own.

How long?

CallersCommon practice
Your own web appDays: you deploy it yourself
Your own mobile appsUntil old app versions are rare, or force an update
Partners and the public6 to 12 months at least; some give 24
Banking, health, governmentFollow the contract
A convention, not a standard. Promise a number in your policy and keep it.

Release

Rolling out a new version

From beta to default

Going back must be possible at every step. Keep data that both versions can read: expand first, contract later.
  • 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.

Sources