API contracts
An API contract is the definition of a web API. Like any standard API, a consumer should know how to call an endpoint and what to expect from it in return. Each endpoint should have a signature, like a method, and should enforce that signature.
Using DTOs as input and output makes them part of that contract, adding even more value to them, locking in place the contract instead of using a more volatile model shared across multiple parts (layers) of the system. From this point forward, a DTO is more than a simple “object used to transfer data.” It becomes an integral part of the contract, and the only reason for a DTO to change is directly linked to that contract (and vice versa).
Now that we have an idea of what an API contract is, let’s see how to define those contracts to improve teamwork, system collaboration, and discoverability for consumers of APIs.
To define API contracts, we could do the following:
- Open any text editor...