API versioning
Версионирование API
API versioning lets a service change how its API works while keeping the old behavior available to clients that haven't updated yet.
Why APIs have versions
An API contract is what callers can rely on: which requests are accepted, what responses look like, and what those responses mean. A server can be deployed in minutes, but a partner integration or an installed mobile app may take months to update. Keeping two versions of the contract available lets those clients migrate on their own schedule. It does not necessarily mean running two copies of the service.
What counts as a breaking change?
A change breaks compatibility if a client that followed the old contract can no longer work correctly. Removing a response field, changing its type, or making a request parameter mandatory are common examples. Behavior matters as much as shape: changing a duration from seconds to milliseconds can break callers even if it is still an integer. Adding a response field is often safe, but only if clients can ignore fields they don't recognize. Fixing an internal query or improving latency doesn't normally require a new API version.
Example: returning a price with its currency
GET /v1/orders/42 → {"total": 1999}
GET /v2/orders/42 → {"total": {"amount": 1999, "currency": "USD"}}
In v1, callers read total as an integer in cents. In v2, they read an object containing the amount in minor currency units and a currency code. Returning the object to a v1 caller could cause a parsing error or an incorrect calculation. Both endpoints can fetch the same order and apply the same business rules; only the code that builds the response needs to differ in this example.
How a request selects a version
Putting the version in the path (/v2/orders/42) makes it visible in request logs and straightforward to route. A query parameter (?version=2) also makes it visible in the URL, but you should verify that caches include it in their cache keys. A request header such as API-Version: 2 leaves the URL unchanged. For cacheable responses that depend on that header, Vary: API-Version tells HTTP caches to distinguish the variants. Whichever method you choose, document what happens when the version is missing or unsupported. An implicit default that moves to the latest version can break callers that haven't changed their requests.
Rolling out and retiring a version
Publish the new contract, provide migration examples, and announce when the old version is expected to be turned off. Keep contract tests for both versions while they are supported. Measure usage by version and, where possible, by client: a drop in traffic doesn't prove that everyone has migrated, especially if a partner only runs a job once a month. Supporting old contracts costs time in maintenance and testing, which is why teams usually prefer compatible changes over creating a version for every release.
How it comes up in interviews
You might be asked to rename a field without breaking existing integrations. Start by asking who controls the clients and whether the old field can stay alongside the new one. If keeping both fields meets the requirements, callers can migrate without a new version. If the old contract can't be preserved alongside the change, explain how clients would select a version, how you'd test both contracts, and what evidence you'd want before retiring the old one.