Generate and host your API documentation for free with open-source tools
By Dan Bader — Get free updates of new posts here.
How to generate documentation for a RESTful API as part of your continuous integration build and then automatically deploy it to a website. Includes a full example project on GitHub.
For a recent project I worked on a distributed system that communicated internally through RESTful APIs. The implementation work was distributed around different teams working across several timezones.
Early on we identified integrating the different systems as one of the likely challenges for the project. To reduce the communication risk, we decided to follow a shared set of API design guidelines across all systems.
The goal was to have up-to-date API documentation for each system available at all times. We used a combination of tools and automated workflows that’s been working out well for the project.
In this article I’d like to share the approach we used for auto-generating our API documentation. We used the apiDoc documentation generator, automated builds for the documentation on CI, and automated deploys of the generated documentation to a website.
apiDoc is a command-line tool for generating API documentation directly from annotations in the source code of your app. Its syntax is similar to JavaDoc and relatively easy to pick up.
apiDoc works with most popular programming languages, which means you can use the same annotation syntax across multiple projects in a polyglot environment.
To give you a taste of apiDoc’s syntax, here are some example annotations describing a simple endpoint on a RESTful API. I’m using Python as an example here but things would look similar for JavaScript or Ruby:
@app.route('/api/v1/random/',method=['OPTIONS','GET'])defget_random_number():""" @api {GET} /v1/random/ Generate a random number @apiName GetRandomNumber @apiGroup Random @apiDescription Generates a random number in the range `[0.0, 1.0)`. @apiSuccess (Success 200) {UUID} request_id Unique id for the request @apiSuccess (Success 200) {Number} results Random number in `[0.0, 1.0)` @apiSampleRequest /v1/random/ @apiExample cURL example $ curl https://apidoc-example.herokuapp.com/api/v1/random/ @apiSuccessExample {js} Success-Response: HTTP/1.0 200 OK { "request_id": "ad506913-a073-4d23-9f95-388d1c1e2c46", "result": 0.3606252123151169 } """# ...
apiDoc parses out these annotations and then generates a static website with API documentation meant for people to read. Here’s an example of what that website export looks like:
The standard website template that comes with apiDoc looks fairly clean and organized. You can fully customize the look and feel, for example, to use your company branding.
The sites generated by apiDoc also include fancy features like the ability to send a sample request to your API directly from the website and inspect the returned result. Here’s what that looks like1:
How to write a great README for your GitHub project – A great README file helps your project to stand out from the sea of open-source software on GitHub. In this article I go over the key elements every README for an open-source project should contain. It also includes a README.md template for use in your own projects.
Python Code Review: Unplugged – Episode 3 – In this third episode of my video code review series I take a look at a reader’s web scraping project and start adding some unit tests to it.
Software engineer reading list: My favourite books about programming – Reading books is one of the best ways to improve your craftsmanship and to become a better software developer. This is a continuously updated list with my favourite programming books, sorted by topic. I link to the ebook version where possible but most books should be available made from dead trees as well.
Debugging memory usage in a live Python web app – I worked on a Python web app a while ago that was struggling with using too much memory in production. A helpful technique for debugging this issue was adding a simple API endpoint that exposed memory stats while the app was running.
Interfacing Python and C: The CFFI Module – How to use Python’s built-in CFFI module for interfacing Python with native libraries as an alternative to the “ctypes” approach.
Working With File I/O in Python – Learn the basics of working with files in Python. How to read from files, how to write data to them, what file seeks are, and why files should be closed.
How to Reverse a String in Python – An overview of the three main ways to reverse a Python string: “slicing”, reverse iteration, and the classic in-place reversal algorithm. Also includes performance benchmarks.
Working with Random Numbers in Python – An overview for working with randomness in Python, using only functionality built into the standard library and CPython itself.