Web services have been around for a few decades, and we’ve seen a number of API patterns evolve over the years. It started with SOAP (originally named XML-RPC, if Wikipedia is to be trusted). Then we saw the rise of REST, often paired with JSON payloads.
Now, Twitter wisdom says we should use GraphQL, or perhaps gRPC. There’s also a new kid on the block: tRPC.
These newer technologies have one thing in common: an improved developer experience enabled by explicit API contracts. Let’s dig into what API contracts are and why they’re important.
A common REST scenario
Two developers have a meeting to discuss a new feature they need to build. The frontend dev shares the UX mocks and describes what they need from the backend. The backend dev goes away for a day or two and implements a couple of new REST endpoints. Once they’re done, they send the frontend dev a Postman collection with a series of sample requests, which act as documentation. The frontend dev then starts building their own code on top of it. They quickly realize that one of the endpoints is missing some data that needs to be shown in the UI, so they talk to the backend dev, who updates the endpoint’s implementation the next day. This back-and-forth goes on with various tweaks to the endpoints over the duration of two weeks. The frontend dev gets a little frustrated. The backend dev isn’t enjoying the process either and starts to grow resentful. How they wish there was a better way! What we’re missing here is an API contract, agreed upon before implementation even started. This could take the form of a simple text document that describes for each endpoint:- what the endpoint does, in unambiguous, human-friendly terms
- its HTTP method and path (including any dynamic path parameters)
- the request payload schema
- the response payload schema
- any other information such as headers, query parameters and so on
- example requests and responses
**Open
API (aka Swagger)** An alternative to a text document is to use the OpenAPI format, which lets you formally describe REST APIs, down to the type of every request/response field, using YAML (or JSON). Here is how the official docs describe it: The OpenAPI Specification (OAS) defines a standard, language-agnostic interface to RESTful APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection. Here is how you would use it to describe a GET /users endpoint:- brought about 100% compatibility with JSON Schema!
**Graph
QL** GraphQL is a graph-based API architecture designed by Facebook. You define your nodes, their fields and relationships to other nodes, then a client can query those nodes in a very efficient way, only requesting the fields it needs, and fetching any related nodes in a single query. Instead of HTTP endpoints, GraphQL uses the concept of “queries”, which are entry points to the node graph. For example the query “human(id: String)” would let you fetch a given Human node by its ID, along with any related nodes (e.g. friends, reviews, etc).gRPC
gRPC is a language-agnostic RPC-based API architecture designed by Google. It’s based on HTTP/2 and in most scenarios, it uses protocol buffers as its serialization format. Each data type (called “message”) can be efficiently encoded in binary form thanks to the use of field indexes instead of field names:tRPC
tRPC is an alternative API technology that’s taking off in- It relies primarily on TypeScript to enable a type-safe API between frontend and backend. You’ll need a monorepo to use it (but you’re already using a monorepo anyway, right?).