Provider and Consumer Contracts
You'll learn how defining clear expectations from both sides of an API helps prevent integration failures.
When two services communicate, both sides have responsibilities and expectations. A provider offers an API or service, while a consumer uses it. A contract defines what each side promises and expects—establishing a shared understanding of how they'll interact.
The Provider's Contract
The provider's contract specifies what it promises to deliver:
- Which endpoints or methods exist
- What request formats it accepts
- What response formats it returns
- Which status codes or errors it produces
- Performance guarantees (if any)
Think of it like a restaurant's menu: "We serve these dishes, prepared this way, at these prices."
The Consumer's Contract
The consumer's contract defines what it expects and how it'll use the provider:
- Which specific endpoints it calls
- What data it sends in requests
- Which response fields it actually uses
- How it handles errors
- What assumptions it makes about behavior
This is like a customer's order: "I'll request this dish, expect these ingredients, and won't complain if you change garnishes I don't eat."
Why Both Perspectives Matter
Without consumer contracts, the provider might maintain features nobody uses—or break features they didn't know were critical. Without provider contracts, consumers make dangerous assumptions that fail when the provider evolves.
Contract testing verifies both: the provider honors its promises, and the consumer's expectations remain valid. This prevents integration surprises and enables independent evolution.
Key Takeaway: Provider contracts define what a service promises to deliver, while consumer contracts define what callers actually expect and use—testing both perspectives catches integration problems early.