API contracts: agree on and verify request-response behavior
An API contract is the shared expectation between a provider and its consumer about an interaction. It can include more than paths and payload schemas: method, status codes, headers, and error responses that clients rely on also matter. An OpenAPI document describes an API surface; consumer-driven tests such as Pact replay concrete interactions to check whether a provider meets a consumer's expectations. These provide different evidence and do not replace one another.
An API contract is the shared expectation between a provider and its consumer about an interaction. It can include more than paths and payload schemas: method, status codes, headers, and error responses that clients rely on also matter. An OpenAPI document describes an API surface; consumer-driven tests such as Pact replay concrete interactions to check whether a provider meets a consumer's expectations. These provide different evidence and do not replace one another.
What belongs in a contract?
For a consumer creating an order with POST /orders, document the method and path it sends, required authentication and content type, request fields it uses, success status and response fields it reads, and important error responses it handles. If duplicate orders return 409 Conflict, whether the client handles that response may be part of the agreement.
The consumer contract need not enumerate every provider validation rule or storage detail. Pact distinguishes message exchange expectations from provider side effects, such as whether an order was correctly persisted; those belong in provider functional tests. Overly specific scenarios can prevent harmless provider changes.
Schema descriptions and contract tests differ
OpenAPI is a language-agnostic description of an HTTP API that tools can use for documentation, code generation, and testing. Its existence alone does not prove the implementation follows it; provider checks can compare implementation behavior with the document.
Pact is one code-first, consumer-driven approach. Consumer tests define interactions the client actually uses and check that it sends the expected request and handles the expected response. The generated pact can then be replayed against the provider, with required provider states prepared, to verify its response. This is not an end-to-end test of both complete services and does not prove every business outcome.
| Question | Useful evidence |
|---|---|
| What is the full API surface and data structure? | OpenAPI or another schema/specification |
| What request-response interaction does a consumer use? | Consumer interaction and provider verification |
| Did the request cause the correct persistence, payment, or event? | Provider functional/integration test |
| Is a deployed consumer-provider version pair compatible? | Current verification results and deployment policy |
Keep contracts focused
Include details the consumer depends on: status, headers, response fields, and error handling that could break integration. Avoid pinning incidental implementation details. Pact interactions are verified independently; declare provider states when an interaction requires specific preconditions.
Do not cover only the happy path. If a consumer interprets 404 as absence or avoids retrying 409, that behavior may deserve a contract interaction. But do not copy every business validation rule into consumer tests; include a distinction only when the consumer needs to understand it.
Verify both sides when contracts change. Consumer tests check request creation and response handling. Provider verification replays the contract and checks that status, headers, and body satisfy the expectation. If using Pact Broker, share verification results as well as contracts so deployment decisions use current evidence.
Checklist
Which consumer and provider share this contract?
Are method/path, authentication, content type, status, and used payload clear?
Does the contract avoid irrelevant implementation details and excessive validation rules?
Are important consumer-handled errors included alongside success responses?
Are schema conformance and consumer-provider interaction tests distinguished?
Do current provider verification results cover the versions being deployed?
Are side effects and business rules tested separately by the provider?
Sources
[OpenAPI Specification 3.1.1](https://spec.openapis.org/oas/v3.1.1.html)
[Pact docs: contract testing overview and schema distinction](https://docs.pact.io/)
[Pact docs: interactions and verification process](https://docs.pact.io/getting_started/how_pact_works)
[Pact docs: contract versus functional tests](https://docs.pact.io/consumer/contract_tests_not_functional_tests)
[Pact docs: provider verification results](https://docs.pact.io/getting_started/verifying_pacts)