# Consumer-Driven Contracts to the Rescue source: https://developer.mastercard.com/blog/consumer-driven-contracts-to-the-rescue/index.md Authors: Tanya Heuiser. In a world of reusable services, applications depend on APIs for data. Such is the case for one of the Loyalty Rewards applications my team and I are building. It is a web app for configuring rebate promotions that cardholders can take advantage of depending on the credit card they signed up for. An example of a promotion would be that I can receive 5% cash back when I use my Mastercard credit card at a particular merchant (i.e. my favorite restaurant) in a given time frame such as a month. This app relies on a handful of REST APIs in order to operate. Any changes to the API specifications of one of these services could potentially break the app. Prior to development, our team would collaborate with the various API teams to agree on the API specs. However, during development, there could be discrepancies between the expected and actual payloads of some of the APIs. Sometimes discovering these discrepancies does not happen soon enough and causes rework, which in turn causes delays. To fix this pain point, our team decided to utilize contract testing, particularly the Pact framework. From Pact's documentation, "Contract testing ensures that a pair of applications will work correctly together by checking each application in isolation to ensure the messages it sends or receives conform to a shared understanding that is documented in a 'contract'... A contract is between a consumer (for example, a client that wants to receive some data) and a provider (for example, an API on a server that provides the data the client needs)... The contract is generated during the execution of the automated consumer tests. A major advantage of this pattern is that only parts of the communication that are actually used by the consumer(s) get tested. This in turn means that any provider behavior not used by current consumers is free to change without breaking tests." We chose Pact because it is a consumer-driven contract (CDC) testing tool as opposed to other frameworks that are provider-driven. CDCs put the emphasis on the consumer by specifying which parts of the API are actually used. On the other hand, provider-driven contracts put the emphasis on the provider and specify what the provider exposes in their API, whether or not those parts of the API are actually used by a consumer. It made sense for our app, being a consumer, to initiate and maintain the contracts. This does not, however, eliminate the need for collaboration with the API providers but actually encourages it. After collaboration with the API providers, we use the Pact libraries to codify and generate the contracts in JSON format. The contract can be shared with the provider in multiple ways. The recommended way is to publish the contract to a Pact broker "The Pact Broker is an application for sharing for consumer driven contracts and verification results. It is optimised for use with 'pacts' (contracts created by the Pact framework), but can be used for any type of contract that can be serialized to JSON." A verification result is what the provider generates after running automated unit tests against the retrieved contracts to ensure that the provider's code will in fact produce the expected result as specified in the contract. Our contracts became even more beneficial and powerful when we integrated them with our CI/CD pipeline. Our pipeline would generate the contracts and publish them to the broker to make available to the provider. The providers, through their CI/CD pipelines, would retrieve the contracts and run their automated contract tests against them as part of the verification process. A failure in a provider's contract tests would break their build, indicating that their code changes will negatively affect their consumer, thus making the provider team more careful of their changes. Utilizing CDCs has alleviated our problem of API specification drift. It has helped guarantee that the API specifications our team is dependent on are correct. Should they need to change, then a discussion with the provider teams will need to take place and the contracts updated, if necessary, based on the discussion. If you have experienced similar pain points, I highly recommend looking into CDCs and getting started with Pact .