Full answer
Compatible changes
- Add a new optional request field or a new response field. Add a new endpoint. Add a new enum value only if clients are told to handle unknown values.
Breaking changes
- Remove or rename a field, change its type, make an optional field required, change the meaning of a value, tighten validation, change an error code that clients depend on, or change default sorting or paging.
Versioning options
- URL (/v2/students): clear, easy to route and cache, but it versions the whole API.
- Header or media type (Accept: application/vnd.school.v2+json, or a custom header): clean URLs, harder to test by hand and to cache.
- Date based versions (Stripe style): each account is pinned to a version date, and the server applies transforms. Powerful, costs a lot to maintain.
- No versions, only additive evolution (GraphQL style, or protobuf rules): works when you control or can reach all clients.
Process
- Contract tests and a schema diff in CI (OpenAPI diff, protobuf breaking checks) catch accidental breaks.
- Track usage per version and per client. Announce deprecations with a date, send Deprecation and Sunset headers, log callers that still use old versions, and contact them.
- Keep the number of live versions small (two). Share the domain code and keep only the mapping layer per version.
javascript
// A tiny compatibility checker for object shapes: { field: { type, required } }
function diff(oldSchema, newSchema) {
const breaking = [], compatible = [];
for (const [name, spec] of Object.entries(oldSchema)) {
const next = newSchema[name];
if (!next) breaking.push(`removed field "${name}"`);
else if (next.type !== spec.type) breaking.push(`type of "${name}" changed ${spec.type} -> ${next.type}`);
else if (!spec.required && next.required) breaking.push(`"${name}" became required`);
}
for (const [name, spec] of Object.entries(newSchema)) {
if (oldSchema[name]) continue;
(spec.required ? breaking : compatible).push(`${spec.required ? 'new required' : 'new optional'} field "${name}"`);
}
return { breaking, compatible };
}
const v1 = { id: { type: 'number', required: true }, name: { type: 'string', required: true } };
const v1b = { ...v1, nickname: { type: 'string', required: false } };
const v2 = { id: { type: 'string', required: true }, fullName: { type: 'string', required: true } };
console.log('v1 -> v1 + optional field:', JSON.stringify(diff(v1, v1b)));
console.log('v1 -> v2 (rename, new id type):', JSON.stringify(diff(v1, v2)));
// Tolerant reader: a v1 client keeps working when the server adds fields.
const oldClient = (response) => `Hello ${response.name} (#${response.id})`;
console.log('old client, new payload:', oldClient({ id: 7, name: 'Asha', nickname: 'Ash', badges: ['early'] }));
// Sunset information for a retiring version
const headers = { Deprecation: 'true', Sunset: 'Wed, 30 Jun 2027 23:59:59 GMT', Link: '</v2/students>; rel="successor-version"' };
const daysLeft = Math.round((Date.parse(headers.Sunset) - Date.parse('2026-12-04T00:00:00Z')) / 86400000);
console.log('days until v1 is switched off:', daysLeft);The checker compares two simple schemas and a fixed date is used for the sunset count, so the output is stable. Real projects use OpenAPI diff tools or protobuf breaking-change checks for the same job. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, run once): v1 -> v1 + optional field: {"breaking":[],"compatible":["new optional field \"nickname\""]} v1 -> v2 (rename, new id type): {"breaking":["type of \"id\" changed number -> string","removed field \"name\"","new required field \"fullName\""],"compatible":[]} old client, new payload: Hello Asha (#7) days until v1 is switched off: 209
Key points
- Additive, optional changes are compatible; removal, rename and type changes are not.
- Clients should ignore unknown fields.
- Versions can live in the URL, a header or a date pin.
- Automate breaking change detection in CI.
- Deprecate with a date, headers and usage tracking.
Common mistakes
- Renaming a field because the new name is nicer.
- Adding a new required request field to an existing endpoint.
- Changing the meaning of a value without a new field.
- Keeping five old versions alive forever.
- Retiring a version with no usage data and no notice.
Follow-ups
- How would you version a webhook payload?
- Why do date-based versions work for Stripe but are hard for small teams?
- What does the tolerant reader rule ask of clients?
Tips
- List what counts as breaking.
- Compare URL and header versioning.
- Describe the retirement process.