Lesson 7 of 9 · 21m
API Versioning and Evolution
Prefer additive evolution, use explicit versions for breaking changes, and build deprecation as an operational process rather than a comment in the docs.
Versioning is a tax you pay when reality changes. Mature APIs avoid that tax by evolving additively as long as possible: add fields, add endpoints, add optional query parameters. When the contract truly must break, make the break explicit and give clients a migration path with dates and measurable traffic.
Additive changes are usually safe
Adding a new JSON field is often backward-compatible because well-written clients ignore unknown keys. Adding enum values is riskier: a client with exhaustive matching may fail when it sees paused for the first time. Changing types, removing fields, renaming keys, tightening validation, or altering error semantics are breaking changes.
usually safe add optional query parameter `include`
usually safe add `customer.email` when it is nullable and documented
risky add a new enum status clients must handle
breaking change `total` from cents integer to decimal string
breaking remove `legacyInvoiceId`
breaking make optional `name` required on createTreat validation changes as breaking. Clients often depend on the server accepting data exactly as it did yesterday.
URI versioning is boring and legible
/v1/invoices is not elegant, but it is obvious in logs, caches, routing tables, firewall rules, and support conversations. Header-based versioning is cleaner in theory and harder in practice because it hides critical routing information from casual inspection.
Route::prefix('/v1')->group($v1Routes);
Route::prefix('/v2')->group($v2Routes);Do not version every micro-change. A version is a compatibility boundary, not a sprint number.
Sunset is a process
When v1 must die, publish the timeline, emit migration warnings, measure usage by client, and communicate with the owners of high-volume tokens. A practical response can include:
Deprecation: true
Sunset: Sat, 01 Aug 2026 00:00:00 GMT
Link: <https://docs.sudosecurity.org/api/migrations/v1-to-v2>; rel="deprecation"Then monitor. If 30% of production traffic still uses the old version a month before sunset, deleting it is not a migration — it is a breaking incident you scheduled.
Compatibility layers need tests
Version adapters are seductive because they look small: map v1 input to v2 internally, map output back. They are also where subtle corruption lives. Round-trip tests should verify not just status codes but exact serialization, null behavior, and defaults.
Field notes
- Document compatibility expectations for unknown fields and enum values. Clients need guidance to be forward-compatible.
- Keep generated clients versioned alongside the API. A client generated from a draft schema is a compatibility bug with autocomplete.
- Do not run unlimited versions forever; operational cost grows nonlinearly. Sunset old versions with evidence and notice.
- Feature flags can stage behavior, but they are not a substitute for a public compatibility contract.
Was this lesson helpful?