TestingAPIs is still being built. If something breaks, email support@testingapis.com.

Tutorial

Learn contract testing

An API's clients depend on it to keep its shape: the fields, the types, the status codes and the errors they were built against. Contract testing checks that promise is kept, and catches the changes that would break clients before they ship, including the ones every team's own tests miss.

These lessons start from what a contract is and build up to checks that run on every pull request: reading an OpenAPI document, validating responses, finding drift, spotting breaking changes and verifying consumer contracts. Everything runs against the API at https://api.testingapis.com and its published spec, and the code is plain JavaScript with no libraries, so you can read every line of it. You should be comfortable with JSON and with async JavaScript; the REST tutorial's lesson on schemas and contract testing is a one-page overview if you want one first. Some endpoints need a free account; set your key in TESTINGAPIS_KEY and the example project sends it with every request.

Download the example project

contract-testing-examples.zipThe validator, the diff, a consumer contract and 44 tests. Node.js 22 or later, no dependencies. Unzip and run npm test.

8 lessons3 levelsFree

Words you'll meet

13 terms

Each one is explained properly in its lesson. This list is for looking one up later.

TermMeaningExplained in
ContractEverything an API's clients can rely on: endpoints, parameters, bodies, statuses and response shapesLesson 1
ProviderThe API in a contractLesson 1
ConsumerAny program that calls the API, including a test suiteLesson 1
OpenAPIThe standard format for describing an HTTP API in one documentLesson 2
OperationOne method on one path in an OpenAPI document, such as GET /api/Products/{id}Lesson 2
$refA pointer from one part of a document to a definition elsewhere, usually in componentsLesson 2
JSON SchemaA vocabulary for describing what a JSON value must look likeLesson 3
ValidatorA program that checks a value against a schema and reports every mismatchLesson 3
DriftAny mismatch between what a spec says and what the API doesLesson 5
Breaking changeA change that makes a client that worked yesterday fail todayLesson 6
Expand and contractAdding the new before removing the old, so a breaking change happens in safe stepsLesson 6
Consumer-driven contractA file in which a consumer records the requests it makes and the fields it usesLesson 7
Provider verificationReplaying consumer contracts against the real providerLesson 7

OpenAPI is a specification of the OpenAPI Initiative. Pact, Ajv, Schemathesis, oasdiff and openapi-diff are independent open-source projects, mentioned for reference. This tutorial isn't affiliated with any of them.