User Avatar

I write about software development, authenticity and facing your own mortality.

<h1><h1>Why contract-driven API development is better than auto-generated contracts for most delivery teams</h1></h1>
Lucas Eagleton

I love building APIs, and I especially love documenting them.

As with all other development topics, there's more than one way to do this, and I don't mean Swagger/OpenAPI vs RAML vs API Blueprint etc. In delivery teams, there's an ever-present debate between whether to write the API contract first or have the codebase render the API contract.

Of course, both sides are right, but I posit that one is more right than the other.

Here are my top three reasons to write your API documentation before even firing up a code editor, or choosing what language to build it in.

Reason #1: Hours of planning can save you from weeks of delivery.

Thinking through all the resources and methods you'll need in your shiny new API before starting to code allows you to realise overlaps, build for maximum utility, only build needed functionality and gives you a leg-up on test-driven development - after all, you've just described what a method should do, TDD should be easy now.

Reason #2: Solve for everyone, all at once

Building your code first, then publishing a generated contract means that all the thought going into the API design has been done by the developers. But what about BAs, QAs, product owners, consumers and clients? Being able to step above the code and discuss the API design at an intention (rather than solution) level allows you to create an API that is ready for adoption by all intended parties when you publish.

Reason #3: Coordinated development

Unless you're building an API without existing consumers (e.g. "build it and they will come"), having the contract ready before any teams start development allows all parties to work to the same design rather than having the API producers work first and the consumers work second or constantly be changing implementations as the design evolves. Concurrent delivery allows for a quicker time to market.

Debates are good - there's plenty of reasons for the counterargument as well. What's your take?