Skip to lesson

An app on thousands of phones calls your API, and old app versions stay installed for months. Change the API carelessly and those phones break on the same day.

Breaking and non-breaking changes

A breaking change makes a correct existing client fail or misbehave. A non-breaking change keeps every existing client working; only breaking changes need a new version.

Five changes to the product JSON: which ones break an old client?

Example 1 · Five changes to the product JSON

Text
1. Add "brand": "Redmi"2. Rename "price" to "sellingPrice"3. Send "price": "17999.00" (a string) instead of 17999.004. Add an optional filter ?brand=Redmi to GET /products5. Make "brand" required when creating a product
Verdict
1. Non-breaking: old clients ignore the new field
2. Breaking: old clients find no "price"
3. Breaking: old clients doing maths on a number get a string
4. Non-breaking: old requests do not send it and work as before
5. Breaking: old create requests without "brand" now fail with 400

What this code does

  • The test for each change: would a client written for v1 yesterday still work today, without any update?
  • Change 5 is easy to miss: the server change looks small, but every old app that creates products starts getting 400.

The rule of thumb: you may add, you may not remove or change. Adding a response field, a new endpoint or an optional parameter is safe, as long as clients ignore fields they do not know. Removing or renaming a field, changing its type or meaning, making an optional input required or changing a status code breaks someone.

Typical cases:

  • Non-breaking: new response field, new endpoint, new optional query parameter, new problem type for a new case.
  • Breaking: remove or rename a field, price from number to string, new required request field.
  • Breaking: change a status code (200 to 201), a URL, or the meaning of a value (price in paise instead of rupees).
  • Also breaking: a smaller maximum page size, or a new rule that rejects requests that used to work.

Where the version goes: path, header or media type

API versioning lets an API grow while every existing client keeps working. The version number travels in the path, in a header or in the media type.

The same request for product 1, with the version in three different places.

Example 2 · The same request, three versioning styles

Text
GET /api/v1/products/1 HTTP/1.1Host: localhost:8080GET /api/products/1 HTTP/1.1Host: localhost:8080API-Version: 1GET /api/products/1 HTTP/1.1Host: localhost:8080Accept: application/vnd.shoplite.v1+json
Trade-offs
Path:       the version is in the URL; visible everywhere, easy to route and test
Header:     clean URL; the client must remember to send the header every time
Media type: clean URL, standard content negotiation; hardest to try by hand

What this code does

  • All three ask for the v1 representation of product 1; they only differ in where the version travels.
  • With the header or media-type styles, a request without the header needs a rule too: usually the oldest supported version, which keeps old clients working.

The media-type style sends Accept: application/vnd.shoplite.v1+json, where vnd. marks a vendor-specific type. The path is the most visible choice: you see the version in every log line, browser address bar and curl command, and a server or gateway can route on it easily. Headers and media types keep URLs clean, and they fit the idea that a URL names a resource, not a version of it. But they are easy to forget and harder to test from a browser.

Only the major version goes in the path. A minor, non-breaking release does not change /v1.

Adding fields safely and deprecating old ones

A tolerant reader is a client that reads the fields it needs and ignores the rest, so a new response field never breaks it.

A product that still sends the old mrp field, with headers that announce its removal.

Example 3 · Deprecating a field with Deprecation and Sunset

Text
GET /api/v1/products/1 HTTP/1.1Host: localhost:8080
Response (a possible future change)
HTTP/1.1 200 OK
Deprecation: @1798761600
Sunset: Wed, 30 Jun 2027 23:59:59 GMT
Link: <https://shoplite.example/docs/changes#mrp>; rel="deprecation"
Content-Type: application/json

{
  "id": 1,
  "sku": "MOB-RN13",
  "name": "Redmi Note 13 5G",
  "categoryId": 2,
  "categoryName": "Mobiles",
  "price": 17999.00,
  "stock": 40,
  "isActive": true,
  "mrp": 21999.00
}

What this code does

  • Suppose ShopLite wants to stop sending mrp (maximum retail price). It keeps sending it, and the headers announce the plan: Deprecation: @1798761600 is a Unix timestamp for 1 January 2027 00:00 UTC.
  • Sunset gives the date after which mrp may disappear, and the Link with rel="deprecation" points at a page that explains what to use instead.

The ShopLite contract says it in plain words: clients must ignore unknown response fields, because v1 may add fields. To replace a field, add the new one next to the old one and send both. Then deprecate the old one: announce that it will go away, and when.

Two standard headers carry the announcement. Deprecation (RFC 9745) says since when something is deprecated, and Sunset (RFC 8594) gives the date after which it may stop working. Only in the next major version is the old field removed, so clients get months to move and the server logs show who still uses it.

Running two versions side by side

Side-by-side versions means the server runs v1 and v2 at once on the same data and rules. Only the layer that reads requests and writes JSON differs.

Product 1 read from /api/v1 and from a possible /api/v2.

Example 4 · v1 and v2 side by side (a possible future v2)

Text
GET /api/v1/products/1 HTTP/1.1Host: localhost:8080GET /api/v2/products/1 HTTP/1.1Host: localhost:8080
Responses
HTTP/1.1 200 OK
Content-Type: application/json

{
  "id": 1,
  "sku": "MOB-RN13",
  "name": "Redmi Note 13 5G",
  "categoryId": 2,
  "categoryName": "Mobiles",
  "price": 17999.00,
  "stock": 40,
  "isActive": true
}

HTTP/1.1 200 OK
Content-Type: application/json

{
  "id": 1,
  "sku": "MOB-RN13",
  "name": "Redmi Note 13 5G",
  "categoryId": 2,
  "categoryName": "Mobiles",
  "price": {
    "amount": 17999.00,
    "currency": "INR"
  },
  "stock": 40,
  "isActive": true
}

What this code does

  • v2 turns price into an object with a currency, ready for selling abroad. That is a breaking change, so it lives at /api/v2, and v1 answers exactly as before.
  • Both answers come from the same database row and the same rules; only the JSON writing differs. ShopLite has no v2 today; this is what one would look like.

When a breaking change is unavoidable, /api/v1/... keeps answering exactly as before and /api/v2/... answers in the new way, so old app versions keep working while users update. Copying the whole application per version would mean fixing every bug twice.

After the sunset date, and when the logs show almost no v1 traffic, v1 is switched off. A call to it then gets a clear error such as 410 Gone.

Order of execution

Retire version 1

Put the steps in the order a team follows.

The ShopLite choice: /api/v1

Path versioning puts the major version in the URL path, as in /api/v1/products. Only a breaking change moves to /api/v2.

ShopLite's choice is easy to see, easy to test with a browser or curl, easy to route, and simple for anyone reading the logs.

The rules that go with it: additions stay in v1 and clients ignore unknown fields; anything breaking goes to /api/v2, which would run next to v1 until v1's sunset date. Being careful with additions means ShopLite should rarely need a v2 at all.

Next step

Check your understanding

11 quick questions