API Versioning
Why versioning is needed
- Clients ship on their own schedules; breaking changes without versions strand old apps.
- Versioning lets you deprecate fields and routes with a communicated timeline.
- It reduces fear of shipping improvements when the contract is explicit.
URI versioning (/v1, /v2)
- Put the major version in the path (
/v1/users): easy to see in logs and docs.
- Downside: proliferates URLs and caches; still the most common pattern for public REST.
- Avoid silent changes inside
/v1 once published; add /v2 for incompatible changes.
- Clients send
Accept: application/vnd.company.v2+json or API-Version: 2.
- Keeps URLs stable; harder to grep logs unless you log the header.
- Ensure intermediaries (CDNs, caches) vary on the version header when responses differ.
Backward compatibility strategies
- Additive changes: new optional fields, new endpoints, new enum values with tolerant readers.
- Deprecation headers (
Deprecation, Sunset, links to migration guides).
- Provide dual-write/dual-read windows and concrete sunset dates for removals.