Coderockr's Blog

Defining and Managing APIs with API Blueprint

7 min
Jun 28, 2017

This content was originally published at eltonminetto.dev by a current or former member of our team. We’re proud to share their insights and contributions with our community!

One of the best decisions we made at Coderockr was to adopt the "API First" approach for all the projects we started since 2010. However, in recent months, we recognized a need to improve the process of defining and documenting APIs.

We were already using other approaches , but most of them involved documenting the API within the code itself, using annotations. This approach works, but it has some issues, especially when documentation needs to be altered by someone in business. Additionally, generating mocks and tests from these annotations is a complex challenge.

With this in mind, we conducted some research and arrived at two alternatives: Swagger and API Blueprint .. Both are API documentation standards and have their advantages and disadvantages:

  • Swagger : It is the market standard and has been adopted by several companies like Amazon. To describe the API, it is necessary to create JSON files, which greatly facilitates the process for programmers but is a bit complex for non-technical individuals to visualize and modify the content. There is a series of tools that can help with this process, but it has become a minor barrier for us. (Well, at least it's not YML... Did I mention that I hate YML?)

  • API Blueprint : This is a more recent specification created by a company called Apiary , which was acquired by Oracle. The major advantage of API Blueprint is that it is written in Markdown, making it much easier to edit the documents, even for those who are not familiar with coding. Additionally, there are several tools available that allow the generation of documents in the Swagger format, mock servers, and tests.

We opted for API Blueprint because of its ease of use and the agility it brought us. I will demonstrate with a small example.

The definition is written in a file in Markdown format, which can be named “api.md” or “api.apib.” Both work, but if we use the ".apib" extension, we can take advantage of plugins for editors like Sublime Text that assist in writing. The plugins can be found on the official specification website.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
FORMAT: 1A HOST: http://api.sample.com.br/v1 # Sample da API Documentação da API Sample utilizando API Blueprint. # Group API ## Sobre [/] Nesta seção, você pode descrever detalhes comuns a todos os serviços, como formatos, headers, tipos de erros, autenticação, etc. # Group Mensagem ## Mensagens [/message] ### Criar mensagem [POST] + Request (application/json) + Headers Accept: application/json Content-Type: application/json + Body (Message) + Response 201 (application/json) + Body (Created) ### Listar mensagens [GET] + Response 200 (application/json) + Body (array[Message]) + Response 404 (application/json) + Body (Error) ## Mensagem específica [/message/{id_message}] + Parameters + id_message: `1` (number, required) - ID da mensagem ### Ver mensagem [GET] + Response 200 (application/json) + Body (Message) + Response 404 (application/json) + Body (Error) ### Excluir mensagem [DELETE] + Response 200 (application/json) + Body (Id) + Response 404 (application/json) + Body (Error) ### Alterar mensagem [PUT] + Request (application/json) + Headers Accept: application/json Content-Type: application/json + Body (MessageUpdate) + Response 200 (application/json) + Body (Created) + Response 400 (application/json) + Body (Error) ## Anexos de uma mensagem [/message/{id_message}/file] + Parameters + id_message: `1` (number) - ID da mensagem ### Listar anexos [GET] + Response 200 (application/json) + Body (array[File]) + Response 404 (application/json) + Body (Error) ## Anexo específico [/message/{id_message}/file/{file_id}] + Parameters + id_message: `1` (number) - ID da mensagem + file_id: `1` (number) - ID do arquivo ### Ver anexo [GET] + Response 200 (application/json) + Body (File) + Response 404 (application/json) + Body (Error) ### Remover anexo [DELETE] + Response 200 (application/json) + Body (Id) + Response 400 (application/json) + Body (Error) # Data Structures ## Message (object) + subject (string) - Assunto da mensagem + body (string) - Corpo da mensagem ## MessageUpdate (Message) + id_message (number) - ID da mensagem ## File (object) + id_file (number) - ID do arquivo + name (string) - Nome do arquivo + url (string) - URL do arquivo ## Created (object) + id (number) - ID gerado ## ID (object) + id (number) - ID a ser processado ## Error (object) + code (number) - Status code + message (string) - Mensagem do erro + description (string) - Descrição do erro ## Multi (object) + id (number) - Código da entidade + code (number) - Status code + message (string) - Descrição do status ## User (object) + id (number) - Código do usuário + name (string) - Nome do usuário + token (string) - Token do usuário conectado

On the specification website, you can see the details, but basically, what we do is define the URLs, the format of requests and responses. We can define simple and complex data structures and use them to describe what the API expects as input and what it should generate as output. The document is relatively simple to understand and modify, which was one of the key points in our choice. Even so, we can enhance the presentation.

Generating documentation

Among the tools available on the official website, aglio is one of the most interesting for generating an HTML presentation of our definition. It can be installed via:

1
npm install -g aglio

To generate the documentation, we can use the command:

1
aglio -i api.apib --theme-full-width --no-theme-condense -o index.html

On the tool's website, you can see all the options for theme and presentation customization. Another useful command is:

1
aglio -i api.apib --theme-full-width --no-theme-condense -s

It generates a local server on port 3000, which monitors changes in the ".apib" file and automatically updates the documentation page. This greatly facilitates the maintenance of the document. Here's an example of the generated documentation.

undefined

Documentation is very helpful in the development process for API clients, but we can go further.

Generating a mock server

With the API defined, front-end teams (web, mobile, etc.) and back-end teams (who will develop the API) can work in parallel. To make this even easier, we can create a "mock server" that generates fake data based on the API definition. This way, the front-end team can work without having to wait for the back-end team to finish the implementation. For this, we will use another tool, drakov .

To install the tool, simply run:

--- shell

npm install -g drakov

1
And to generate the server, run:

--- shell

drakov -f api.apib -p 4000

1
2
3
4
5
This way, we have a functional API that can be used for testing and development. The final step is to define a way to validate our API. Testing We can use a tool called to generate a file for our API and perform tests using some of features. We chose to use , which automates tests using both API Blueprint and Swagger. To install it, run:

--- shell

npm install -g dredd

1
And to run the tests, use the command:

--- shell

dredd api.apib http://localhost:4000

Post author profile picture

Elton Minetto

/ Principal Software Engineer /

Software developer since 1998 and teacher since 2004. Author of several books on development, including "Career Tips for Devs" and "Zend Framework in Practice."

Transform with Coderockr

We are ready to help you make the most of technology and achieve exceptional results.

WhatsApp