Versioning, Webhooks and Alternatives · Lesson 23 of 30
REST API Versioning and Deprecation
Once other teams, partners or mobile apps depend on your API, you can't change it freely – an app installed two years ago still calls it. Good API teams avoid breaking changes wherever possible, version when they must, and retire old versions with clear, machine-readable warnings. This lesson covers all three.
In this lesson
- Breaking vs non-breaking changes
- URL, header and media-type versioning
- Two versions side by side – running
- Deprecation and Sunset headers
What breaks clients?
| Usually safe (non-breaking) | Breaking |
|---|---|
| adding a new endpoint | removing or renaming an endpoint or field |
| adding an optional request field | making an optional field required |
| adding a response field | changing a field's type or format (number → string, date format) |
| adding a new enum value – if clients were told to expect them | changing status codes or error formats |
| relaxing validation | tightening validation; changing default behaviour |
Design for evolution from day one: clients should ignore unknown fields, and your docs should say new fields and enum values may appear. Then most changes never need a new version.
Versioning styles
| Style | Example | Pros / cons |
|---|---|---|
| URL path | /api/v2/orders | obvious, easy to route, cache and test; most common (Twitter/X, Razorpay) |
| Header | Api-Version: 2026-11-01 | clean URLs; date-based versions per account (Stripe); harder to try in a browser |
| Media type | Accept: application/vnd.learnfrenzy.v2+json | most "RESTful" in theory (GitHub); least convenient |
| Query | ?api-version=2 | simple (Azure); easy to forget |
Two versions side by side
Version 2 splits name into first and last names – a breaking change. Both versions share the same data and business logic; only the representation differs:
// server.js
import express from 'express';
const app = express();
const customers = [{ id: 'c1', firstName: 'Asha', lastName: 'Verma', email: 'asha@example.com' }];
// v1 – deprecated: still served, but announces its retirement
const v1 = express.Router();
v1.use((req, res, next) => {
res.set({
Deprecation: '@1767225600', // deprecated since 2026-01-01 (RFC 9745)
Sunset: 'Wed, 30 Jun 2027 00:00:00 GMT', // will stop working on this date (RFC 8594)
Link: '<https://api.learnfrenzy.com/docs/migrate-v2>; rel="deprecation"',
});
next();
});
v1.get('/customers/:id', (req, res) => {
const c = customers.find((x) => x.id === req.params.id);
res.json({ id: c.id, name: `${c.firstName} ${c.lastName}`, email: c.email });
});
// v2 – current
const v2 = express.Router();
v2.get('/customers/:id', (req, res) => {
const c = customers.find((x) => x.id === req.params.id);
res.json({ id: c.id, firstName: c.firstName, lastName: c.lastName, email: c.email });
});
app.use('/api/v1', v1);
app.use('/api/v2', v2);
app.listen(process.env.PORT ?? 3000);
$ curl http://localhost:3000/api/v1/customers/c1
HTTP/1.1 200 OK
Deprecation: @1767225600
Sunset: Wed, 30 Jun 2027 00:00:00 GMT
Link: <https://api.learnfrenzy.com/docs/migrate-v2>; rel="deprecation"
Content-Type: application/json; charset=utf-8
{
"id": "c1",
"name": "Asha Verma",
"email": "asha@example.com"
}
$ curl http://localhost:3000/api/v2/customers/c1
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
{
"id": "c1",
"firstName": "Asha",
"lastName": "Verma",
"email": "asha@example.com"
}- Old clients keep working on v1, but every response tells them – and their monitoring – that v1 is deprecated, when it will disappear, and where the migration guide is.
- Keep version-specific code thin: shared services, different serialisers. Maintaining two full codebases doesn't scale.
Retiring a version
A respectful deprecation timeline
- Announcerelease v2, publish a migration guide, add Deprecation headers to v1
- Measurelog which API keys still call v1; contact them directly
- Remindemails, dashboard banners; brownouts (short planned outages) to surface forgotten callers
- Sunsetafter 6–12 months, v1 returns 410 Gone with a link to the docs
Version rarely, evolve often
Every version you run is code to maintain, test and secure (forgotten old versions are OWASP API9). Prefer additive changes; when you must break something, batch several breaking changes into one new version.
Practice
Try it yourself
Classify each change: (a) add
phoneto customer responses, (b) renameemailtoemailAddress, (c) return amounts in paise instead of rupees, (d) addGET /api/v2/customers/{id}/orders.Show answer
(a) non-breaking, (b) breaking, (c) breaking – same field, different meaning, the worst kind, (d) non-breaking.
Quick check
Is adding a response field a breaking change?
Normally no – clients should ignore unknown fields.
Which header announces the shutdown date?
Sunset.
What should a retired version return?
410 Gone with a link to migration docs.
Summary
- Avoid breaking changes; design clients to ignore unknown fields.
- Version in the URL (most common) or a header; share logic, vary representations.
- Deprecate with Deprecation, Sunset and Link headers, measure usage, then return 410.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.