Getting started
Install contractdiff, diff two versions of a small OpenAPI contract and read the verdict, then generate TypeScript types and truefit validators from it and call one.
contractdiff reads an API contract and answers three questions about it: what a change breaks and
for whom (diff), whether its form holds up (lint), and what code it implies (gen). This page
runs the first and the third on a contract of one operation.
Install
npm install -D contractdiffInstall it as a dev dependency of the project that owns the contract. It needs Node 22.13 or
later, and its four dependencies are all in the burgee family: bellpull, burgee, seniority
and truefit.
Two versions of a contract
Here is a contract with one operation, getPet, and the next version of it. The new version adds
a required query parameter, fields, and drops the optional tag from the response.
openapi: 3.1.0
info:
title: Pets
version: 1.0.0
paths:
/pets/{id}:
get:
operationId: getPet
parameters:
- name: id
in: path
required: true
schema:
type: string
responses:
"200":
description: A pet
content:
application/json:
schema:
type: object
required: [name]
properties:
name:
type: string
tag:
type: stringopenapi: 3.1.0
info:
title: Pets
version: 1.1.0
paths:
/pets/{id}:
get:
operationId: getPet
parameters:
- name: id
in: path
required: true
schema:
type: string
- name: fields
in: query
required: true
schema:
type: string
responses:
"200":
description: A pet
content:
application/json:
schema:
type: object
required: [name]
properties:
name:
type: stringDiff them
breaking: 1 breaking, 0 unknown, 1 non-breaking (--mode backward --side caller)
getPet #/paths/~1pets~1{id}/get
breaking new-required-request-parameter param:query:fields - -> {"type":"string"}
fix: keep accepting at getPet param:query:fields what the old contract accepted, or release this as a major version
non-breaking response-optional-property-removed response:200:application/json /properties/tag {"type":"string"} -> -
decisions: 1 (--decisions warn)
warn ac-decision-missing getPet param:query:fields - -> {"type":"string"}
fix: breaking change (`new-required-request-parameter`) at getPet param:query:fields with no decision: add `x-change: <the decision>` to getPet param:query:fields, or an ancestor or its operation, naming who decided it; or revert the changeThe first line is the verdict and how it was reached: by default the question is whether a caller
built on old.yaml keeps working (--mode backward --side caller). A caller that never sends
fields now gets a 400, so that change is breaking. A caller that read tag already had to cope
with it missing, since it was optional, so dropping it is not.
Each finding names its rule (new-required-request-parameter is oasdiff's id for the same
change), where it is, the value before and after, and a fix: line. The decisions block is a
second check: a breaking change should name the decision behind it, and this one names none.
Decisions covers it; it warns by default and does not change the exit
code. The exit code is 1 because of the breaking change, which is what a CI job needs.
The same pair, asked from the server's side, is fine: a server that starts requiring a parameter still accepts every request it accepted before, and nothing it sends is new.
non-breaking: 0 breaking, 0 unknown, 2 non-breaking (--mode backward --side server)
getPet #/paths/~1pets~1{id}/get
non-breaking new-required-request-parameter param:query:fields - -> {"type":"string"}
non-breaking response-optional-property-removed response:200:application/json /properties/tag {"type":"string"} -> -Generate code from it
gen reads contractdiff.config.json: the contract, and one target per thing to write.
{
"input": "new.yaml",
"targets": {
"types": { "generator": "types", "out": "src/api" },
"validators": { "generator": "validator", "out": "src/api" }
}
}write src/api/types.ts
write src/api/validators.ts
write contractdiff.map.jsonIt wrote src/api/types.ts (the paths, components and operations interfaces
openapi-typescript users know), src/api/validators.ts (one compiled guard per payload) and
contractdiff.map.json, which records every file and symbol it wrote. Run it again and every file
is unchanged; the same input always writes the same bytes.
The validators are plain TypeScript functions, so a script can use them straight away:
import { GetPetResponse200, operations } from "./src/api/validators.ts";
console.log(GetPetResponse200({ name: "Rex" }));
console.log(GetPetResponse200({ name: 7 }));
console.log(Object.keys(operations.getPet));true
false
[
'param:path:id',
'param:query:fields',
'response:200:application/json'
]Every output block on this site is checked: tests/examples.test.ts writes each titled file,
runs the command in the block's title, and compares what it printed and how it exited.
Where next
- Breaking changes: modes, sides, verdicts, rule ids,
--usage,--transitive, SARIF and changesets. - Code generation and the validator target:
every target, the map file,
--checkfor CI. - Lint: the built-in rulesets, Spectral and Redocly files, and adopting rules without a big-bang cleanup.
- Coming from openapi-typescript, oasdiff or Optic.
- CI gate: the workflow that fails a pull request on a breaking change.
contractdiff
Breaking-change diff, lint and code generation for API contracts (OpenAPI, JSON Schema, AsyncAPI, MCP), and the spec back from the code. No dependency outside the burgee family. An openapi-typescript migration path.
Breaking changes
How contractdiff diff decides whether a contract change breaks callers or servers: the backward, forward and full modes, the caller and server sides, the four verdicts, rule ids, --usage, --transitive, SARIF, changesets and exit codes.