HTTP API Lean Workflow: from contract to documentation
Tags: api, rest, documentation, openapi
Published: 2021-07-02 18:04:23

How do we rely on open source software to achieve collaborative API design and produce our [documentation](https://developers.golem.ai/docuchecker)?

## TL:DR;
* A contract-first approach enables high collaboration and removes overhead.
* Versioning contracts with the code since they share the same lifecycle.
* Stoplight.io DX is a powerful API IDE for OpenAPI.
* Enriching the OpenAPI contract with markdown reduces the cost of maintenance. 
* Rely on Redoc to expose the contract as documentation with a standard UX & UI.


## Context

At [Golem.ai](https://golem.ai/), we are maintaining a platform and several services following the [SOA](https://en.wikipedia.org/wiki/Service-oriented_architecture) concepts. Our protocol between these services relies on [REST](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm) API [level 2](https://martinfowler.com/articles/richardsonMaturityModel.html#level2). 

These API are either:
- consumed programmatically (internally and by our customers)
- used by several web applications (addressing both our business analysts and customers)

## [Contract-first approach](https://openpracticelibrary.com/practice/contract-first-development/)

> There are only two hard things in Computer Science: cache invalidation and naming things.
> -- Phil Karlton

Designing API is a tough job. It's a highly structuring action where we define the boundary of "something". These boundaries exposes our knowledge and understanding about this "something": a service, a micro-service, a bounded context.

At Golem.ai, API design is not only about decision, responsibility & technicality. We advocate continuous collaboration to guarantee the quality & value of our APIs.

Adopting the [dogfooding philosophy](https://en.wikipedia.org/wiki/Eating_your_own_dog_food), we consume our own API. For every new feature, fix or improvement that impacts an endpoint, we follow this 3-step process:

1. **Discovery**: provider & consumer (example: backend & frontend) engineers sit together and discuss their technical challenges. Exploring code, questioning the [shape (specification)](https://basecamp.com/shapeup/webbook) and referring to the [REST approach](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm). 

2. **Agreement**: ultimately, engineers will come out with a pragmatic and relevant solution. They update with Stoplight.io and version the [OpenAPI contract](https://www.openapis.org/) with git. Usually, this contract has been drafted during the discovery by both parties.

3. **Implementation**: [TDD](https://en.wikipedia.org/wiki/Test-driven_development#:~:text=Test%2Ddriven%20development%20(TDD),software%20against%20all%20test%20cases.) is cheaper, our OpenAPI contracts include examples that provide  a free mock for the client and free test assertion & server-side validation. 

*Adjustment: nothing is perfect and near perfection takes time and iteration. Developers can adjust an endpoint but they have to go again through steps 1, 2 & 3. Hopefully, adjustments are usually small and the whole process is short, 5 minutes tops: a slack message, a meeting using screen sharing and a quick discussion will lead to a git commit updating the OpenAPI contract.*

## Contract as Documentation

We needed more than a contract or a reference to on-board, assist, and guide developers using our API. 

Documentation needs maintenance and its lifecycle is tightly bound to the implementation. 

After assessing solutions on the market, we realised that most of the option would either involve changing our workflow or adding complexity to sync the documentation. 

We finally decided to use our OpenAPI contract as documentation with Redoc (read more below). 

The final workflow is simple, involving only the OpenAPI contract, versioned with the code. Our regular pipeline deploy the code and the documentation.

**Side effects**: 
- The code review validates everything: contract, code & documentation. 
- We didn't increase the complexity and the cost of our API.
- Everything, from modelling to documenting, is tied to the lifecycle of the code: ideation, testing, release & deployment. 

We ended up maximizing the value of every step of our API management, reducing the overhead for our developers, and removing conflict during integration. **Collaboration versioned in an OpenAPI contract ensure predictable, meaningful, and enjoyable API development.**

## Tools: our API Stack

### Stoplight.io
[Stoplight.io](https://stoplight.io/) is a game changing tool when it comes to managing & editing OpenAPI contracts.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kfarzd7orpm656bxizl8.png)

1. We use the desktop application, so we can locally load our contract from our git repository into the editor.

2. Next level [DX](https://medium.com/swlh/what-is-dx-developer-experience-401a0e44a9d9): none of our developers were familiar with OpenAPI and they didn’t  have to be with stoplight: WYSIWYG for discovery, linter, direct edition in yaml/json, preview... Engineers can focus on the "what" & "why" rather than the "how".

3. Not intrusive: the desktop app lets you define your own convention, standard, directory structure, and workflow. It adapts to your needs and your organization doesn't have to fit the tool.

4. Provides a mock server locally. We can even quickly prototype our implementation: our local docker container fetches generated data complying to the defined contract.

### Redoc

[Redoc](https://github.com/Redocly/redoc) is a fantastic [DIY](https://en.wikipedia.org/wiki/Do_it_yourself) tool, it leverages the markdown syntax and the OpenAPI description fields to enrich your contract. Basically, **your contract is not only a reference, but becomes your documentation**, example: [this getting started](https://developers.golem.ai/docuchecker#section/Getting-started).

As we maintained and documented several services, we needed to aggregate all our contracts/documentations under a single url (developers.golem.ai). This [Fork](https://github.com/volbrene/redoc) by [Volbrene](https://www.rene-volbach.de/) solved this problem for us.

Once again, the [stoplight studio desktop app](https://stoplight.io/studio/) is bliss, offering markdown edition & preview:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3zpgubtmv29vvqfomf09.png)

### Git & Gitlab perk
API contracts are versioned in the same git repository as the related base code at `/doc/OpenAPI`. 

Gitlab offers a [nice perk based on a naming convention](https://docs.gitlab.com/ee/user/project/repository/#openapi-viewer). Adding "openapi" in your contract file name enables a swagger-like preview in the gitlab code explorer.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pmzi9e17zhc7opsauhxc.png)

*Our engineering team is hiring in Paris, France. php/Symfony junior, contact us!*

*Many thanks to Azure: if you need proofreading contact her at heatherklamb@gmail.com*