Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Designing RESTful APIs  ›  Lesson

Versioning Strategies

Versioning Strategies 14 minAdditive changes are safe within a version, while removing, renaming or changing the meaning of anything needs a new version, which can live in the path, a query parameter or a header.

Versioning Strategies

BookNook wants to split a book's author string into a list of authors, since many books have several. Thousands of installed phone apps still read author. Change it in place and they break tonight. Versioning is how an API grows without pulling the floor out from under the people already standing on it.

The rule

Add freely; never remove or rename without a new version.

First, sort every change into one of two kinds:

  • Non-breaking (safe in the current version): adding a new endpoint, adding an optional request field, adding a new field to a response. Well-written clients ignore fields they do not know.
  • Breaking (needs a new version): removing or renaming a field, changing a field's type or meaning, making an optional field required, changing a URL or status code that callers rely on.

When a break is truly needed, there are three common places to put the version:

GET /v2/books/42                                     URI path
GET /books/42?version=2                              query parameter
GET /books/42   Accept: application/vnd.booknook.v2+json   header / media type
  • URI path (/v1, /v2): the most visible and easiest to test, route and cache. BookNook uses it.
  • Query parameter: easy to add, but easy to forget, and it muddles the "which thing" of the path.
  • Header or media type: keeps URLs clean and fits content negotiation, but is invisible in a browser bar and easy to miss in logs.

Whichever you choose, run old and new side by side, and deprecate the old one politely: announce a date, send a Deprecation or Sunset header on old responses, and watch traffic until it falls away.

The shortcut

For each proposed change, ask: "Would a client written yesterday still work tomorrow?" Yes → ship it in the current version. No → find a non-breaking way, or plan a new version.

Splitting `author` into `authors`Step 1 of 4
  1. Would yesterday's clients still work?

    If author disappears, apps reading it break. So the plain rename is breaking.

The trap is versioning too often. Every version is a copy you must run, test and secure. Additive change should carry you most of the way.

Drills

Answer fast — trust the rule.

quick drill

Which change is safe without a new version?

quick drill

Where does GET /v2/loans put the version?

quick drill

What is a real drawback of header-based versioning?

quick drill

A new version is released. What should happen to the old one?

Remember

  • Add freely; never remove or rename without a new version.
  • Shortcut: would a client written yesterday still work tomorrow?
  • Trap: breaking changes in place, and creating versions when an additive change would do.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainBreaking or not?

BookNook wants to replace the string field author with a list authors. Explain whether this is breaking, how to ship it without breaking v1 callers, and how v1 should eventually be retired.