Generating API Documentation

Learn how to generate API documentation using SwaggerHub and ReDocly.

API documentation

No matter how well we design our API, both server developers and API client developers need some level of documentation in order to understand and use our API. And OpenAPI Specification has some really nice support for generating simple documentation.

API documentation is very much along the lines of technical documentation—it only includes the set of endpoints and associated parameters, request bodies, response bodies, and schemas we included in our document. There is no extended prose covering why someone might want to use our API—nothing like a how-to guide or list of common problems. But basic technical docs are a good start.

SwaggerHub’s generated documentation

We can use SwaggerHub to generate standalone HTML documentation in a couple quick easy steps. The “Export” menu in the SwaggerHub Editor gives us three options. We’ll see them here:

  • Dynamic HTML: This is a Node.js website we can host that renders the documentation in a fairly simple layout. It might make a nice starter site for our API.
  • HTML: This is a static site that lists all the operations and has a very simple layout. It’s a single, standalone page. There’s no need to host a Node.js site.
  • HTML2: This is also a static site, but it has the added feature of showing API calling examples in a number of languages (curl, Java, Objective-C, JavaScript, C#, PHP, Perl, and Python).

External API documentation tools

Many other tools and services can render our OAS documents. One of them is known as ReDocly, which currently offers an open-source GitHub repository where we can download the code and read through examples. The company behind ReDocly is also planning to launch an online hosted version (https://redoc.ly) of this service in the future.

The best thing about ReDocly is that we can use the HTML template page that relies on its hosted JavaScript library to render a very nice HTTP API documentation site very easily. All we need is the ReDocly HTML template and a URL that holds the OAS document. For this course, the following file is the copy of the prototype OAS definition file. We can place it in our public GitHub repository and then update a copy of the ReDocly HTML page to point to that file, and then we’ll have a fully working set of API documentation. See the following screenshot:

Get hands-on with 1200+ tech skills courses.