If you want to build a simple but attractive looking API documentation site, you can’t really go wrong with an open-source tool like Slate. Despite being created by a teenager developer during a summer internship, it has become an incredibly popular tool with the project being forked more than 15,000 times and with well-known organisations including NASA, Best Buy, Monzo and Skyscanner all using it.
🎬 So what is Slate?
Slate is a Ruby-based tool that generates a great-looking, three-panelled API documentation static site from a set of markdown files. It was built by developer Robert Lord in 2013 when he was an 18-year-old intern at at travel software company Tripit. He convinced his boss at the time to let him open-source the project and the rest is history.
He told me found it pretty surreal that so many people were now using and maintaining his “buggy project” nearly six years later. However, the results speak for themselves — you can see some examples in the Slate in the Wild repository.
🛠️ Before you Begin
So before you begin, make sure you have met the following requirements:
- You have an Open API/ Swagger file in .yaml or .json format.
- You have installed Node.js. This includes npm (Node Package Manager).
- You have installed swagger-to-slate or widdershins (or similar) to convert your file into Markdown. I used the former in this example.
- You have installed Vagrant and VirtualBox.
In this example, I’m going to use the generic Swagger Petstore example which I have saved to my Desktop and called petstore.yaml. To convert this to Markdown using
swagger-to-slate, open a terminal and run:
swagger-to-slate -i ~/Desktop/petstore.yaml
This saves a file called petstore.md in the same location as the .yaml file. Once you have this, you can get started with Slate.
🔨 Build your site using Vagrant
To build your API documentation site using Vagrant, follow these steps:
- Go to the Slate repository on Github.
- Click Fork to create your own fork of the main repository.
- Clone the fork to your local machine using
git clone. For example:
git clone https://github.com/<your_github_username>/slate.git
- Go to
~/slate/sourceand remove the index.html.md file.
- Rename the Markdown file you created earlier as index.html.md and copy it into the source folder, changing the file locations as required:
cp ~/<local_path>/index.html.md ~/<local_path>/slate/source/
- To build your site using Vagrant, run:
- Go to http://localhost:4567 to see your site. It may take several minutes to build.
🐳 Build your site using Docker
You can also use Docker to create your site but you must edit some additional files. Although this method is not officially supported, I had no issues when I tried using it.
If you are using Ruby version 2.5.1 or newer, you will need to create three files:
- .dockerignore :
- Dockerfile :
FROM ruby:2.5.1 MAINTAINER Adrian Perez <email@example.com> VOLUME /usr/src/app/source EXPOSE 4567 RUN apt-get update && apt-get install -y nodejs \ && apt-get clean && rm -rf /var/lib/apt/lists/* CMD ["bundle", "exec", "middleman", "server", "--watcher-force-polling"]
- docker-compose.yml :
app: build: . ports: - 4567:4567 volumes: - ./source:/usr/src/app/source
After you create these files, follow these steps:
- Add the Docker files to your local Slate directory.
- To build your Docker site run:
- Go to http://localhost:4567 to view your site.
For alternative Docker methods refer to this documentation.
💎 Build your site using Bundler
Alternatively, if you want to run your Slate site locally, you can also use Bundler. To use this method you must install Ruby version 2.3.1 or newer. To check which version you have installed run:
If you need to install Ruby, see the installation documentation for the different methods available.
Once installed Ruby, you can install Bundler:
gem install bundler
To build your API docs site using builder:
- Navigate to your Slate directory:
- Install the dependencies such as Middleman specified in the Slate Gemfile:
- Launch the Middleman static site generator:
bundle exec middleman server
- Go to http://localhost:4567 to see your site.
That’s pretty much it! Good luck.