Recently, I came across the need of providing some sort of documentation for an API I was building. At that moment, I had never heard of the API Blueprint specification. Though they might be several options to write a good API documentation, I found this one to be very helpful for the following reasons.
1. Easy to read
Sections are well defined and grouped by route. Each route can have several different HTTP methods with names associated to each method. The documentation shows:
- Attributes listed with their descriptions (when needed)
- Requests examples (headers and body)
- Responses of those examples (also headers and body)
2. Mock server
This language allows you to create a mock server that anyone can use to test the response of the actual server (there are tools for this, see more.)
So the mock server is actually a server that responds to the endpoints defined in the documentation and returns the expected responses (it does not care about authentication or headers, cause it actually does not run the production code, it just returns the response to the endpoint hit). So it's very helpful to have this kind of server in the early stages of an app, where the client application is still being built and the backend is not fully deployed to a production/testing cloud based environment.
3. Lots of tools
Renderers are services that take your blueprint as input and create a nice web page for others to refer to, when using you API. Apiary is one of them, and a widely used one. Apiary can be connected with your remote repository, so whenever you blueprint changes, it will automatically reflect the changes in the documentation page.
But I don't want to write another language, and probably neither do you. So here's where docs-generator libraries take place. For those using Rubyβ€οΈ, RSpec API documentation turned out to be a great tool to generate this blueprint (though it can be used to generate other type of docs). You just need to write some RSpec tests and run a rake task to generate the blueprint.
Here's an example of a basic endpoint being documented using this gem:
resource 'Books' do
before do
create_list :book, 5 # Creating some books
end
route '/books', 'Books collection' do # Name of the section
get 'Index' do # HTTP method with name
example 'Ok' do # A request example
do_request
expect(status).to eq 200 # Basic expectation of the request
end
end
end
end
After running rails docs:generate
it'll generate a file with something like this:
# Group Books
## Books collection [/books]
### Index [GET]
+ Request Ok
+ Response 200 ()
+ Body
{
... // The response body would be here
}
If you are writing an API give this language a try. Make your fellow developers happy by giving them a proper documentation of your API.
To sum up, some things I like about it
- Production code does not get mixed with the doc-generating code
- All documentation related code lives in one place
- As soon as a change is made in routes or in the response of the controllers the documentation gets updated automatically by the generator. I don't need to write anything new.
- Mock server just uses the data from the docs (it does not run the production code)
- There are bunch of services to render the documentation (renderers) or I can host my own π
- My API blueprint file lives in the same repository, so I can assure it's always synced with the current branch
In case you want to look a full set of doc-generating test examples
Top comments (2)
You could also use Dredd as a way to test your documentation against your code. You can even write test hooks in Ruby.
Nice find Juan. As you suspect there are a number of tools to convert code to docs. I like how this one takes the RSpec tests and generates the response which can be used by the mock server.