24. APIs and Idempotency
1
The picture to keep in your head2
24.1 What is an API contract?3
24.2 What are the main API styles?4
24.3 How do you version an API?5
24.4 How does pagination work (offset vs cursor)?6
24.5 How should errors and status codes work?7
24.6 How does API auth work, in one page?8
24.7 What is the double-charge problem?9
24.8 How do idempotency keys work?10
24.9 How do you retry safely?11
24.10 How do webhooks work?12
Memory card and where the analogy breaks13
Practice: five levels14
Interview questions15
Solutions16
Sources17
Code lab24. APIs and Idempotency
24.3 How do you version an API?
3 min read+10 XP
At the machine: The station installs a new model where prices show in a different format. Old customers who memorised the old buttons would be confused, so the station keeps the old machine running beside the new one for a while, with a sign: "old machine leaving on 1 March".
In the system: Once clients depend on your API, some changes break them:
- Breaking: removing or renaming a field, changing its type or meaning, making an optional field required, changing error codes. In the chapter's code, v2 changes
pricefrom a number (499.0) to an object ({amount: 49900, currency: "INR"}), and a v1 client reading it crashes. - Non-breaking: adding a new optional field, a new endpoint, a new optional parameter. Clients that ignore unknown fields keep working.
How to version:
- In the URL:
/v1/books/42,/v2/books/42. Simple and visible; the common choice. - In a header:
Accept: application/vnd.bookstore.v2+jsonor a customAPI-Version: 2026-10-01(Stripe uses dated versions pinned per account). - For gRPC and events, evolve the schema compatibly: add fields with new numbers, never reuse old ones (Chapter 23).
Rules that save pain:
- Prefer additive changes; most changes never need a new version.
- Run old and new side by side, translate old requests to the new internals, and announce a sunset date (there's even a
SunsetHTTP header). - Measure who still calls v1 before turning it off.
Memory code: "Add freely; to change a button, put in a new machine and keep the old one running."