Freeze your API's shape, not its data
You changed a field in the JSON. Whose integration did you just break? If your API feeds a widget embedded on other people's sites, you'll probably find out when someone says "it stopped working".
1. A value snapshot protects nothing
The classic snapshot stores the whole response. But responses carry dates, ids and text that change on their own, so the test breaks all the time and the team learns to regenerate it with -u without reading the diff.
Whoever integrates doesn't care about values: they care about keys and their types. That's what needs freezing.
2. Reduce the response to its shape
export function shape(v) {
if (v === null) return 'null';
if (Array.isArray(v)) return v.length ? [shape(v[0])] : ['(empty)'];
if (typeof v === 'object') {
const o = {};
for (const k of Object.keys(v).sort()) o[k] = shape(v[k]);
return o;
}
return typeof v;
}
shape({ a: 1, b: [{ c: 'x' }], d: null });
// β { a: 'number', b: [{ c: 'string' }], d: 'null' }
Keys are sorted, because reordering an object in code breaks nobody. An array is described by its first element, so build fixtures with homogeneous lists. What's left is readable: the list of everything a client can read.
3. Break it on purpose, and freeze the odd parts
A contract test you've never seen fail may not be testing anything. Before trusting it, rename a field in the response and check that the build goes red.
While writing the tests you'll find oddities already in production: a route that adds cached: true only sometimes, two 404s with different shapes. Don't fix them in the same change. The contract describes what clients receive today; "fixing" an odd shape is another contract change, with its own review.
4. The version goes at the root
$ curl -si https://api.example.com/v1/health | grep -i version
x-api-version: 1
Make /v1/<route> an alias for /<route>: a middleware strips the prefix before the router, so auth, CORS and rate limits see the usual route and nobody breaks on deploy day. Add a version header to every response so a client can tell which contract it talked to.
Don't put it inside each resource (/bookings/v1/:slug): the day a user picks v1 as their slug, you've taken over their page.
Start with the route most clients consume: write its shape test, break it once on purpose, and only then add the rest.
Comments and corrections
Build your first AI agent today
RAG over your documents, tools, workflows and a web widget β free tier included.
Get started free