Automatic API Documentation in Node.js Using Swagger

Davi Baltar
The Startup
Published in
8 min readSep 12, 2020

--

Complete documentation: https://swagger-autogen.github.io

For those who develop APIs in Node.js, whether at a professional level or not, you know that keeping the project documentation up to date is not an easy task, especially with short deadlines and other priorities, the documentation ends up most of the time getting out of date or even ignored. To resolve this, today I’m going to talk about an NPM module for Node.js that automatically performs the documentation for Swagger whenever your project is started, without you having to keep doing repairs or touch-ups on the Swagger .json file. The module in question is swagger-autogen.

The swagger-autogen module generates only the Swagger .json file. To interpret it, we will have the help of the swagger-ui-express module and Express.js to create a server, leaving the documentation available on some port. Ah! But does it only work with Express.js? No, the module is independent of any framework, BUT, some frameworks like Express.js (or similar) will have some advantages and we will see more about them later. With the module it is possible to generate the documentation in two ways:

  1. Along with the project, that is, every time the project is started, new documentation is generated, making it always updated.
  2. Without starting the project, that is, running a script at the root of the project. This is similar to the npm test (testers will know…).

--

--