Member-only story
Integrating Swagger with NestJS
A step-by-step guide to generating OpenAPI documentation for your RESTful API
In this article, we will integrate Swagger with a RESTful API built using NestJS. As a quick refresher, NestJS is a server-side web framework that supports both TypeScript and JavaScript. It brings an opinionated architecture to building a Node.js backend that is inspired by the Angular ecosystem. For more information, check out this introduction to NestJS.
On the other hand, Swagger is a tool for implementing the OpenAPI specification. The goal of the OpenAPI specification is to provide a standardized way to understand the capabilities of an API service without actual access to the source code. On the whole, Swagger is a tremendously helpful documentation tool for any modern web backend that exposes a RESTful API. More than just documentation, the OpenAPI specification served up by Swagger can be used to autogenerate servers/clients, facilitate automated testing, and much more.
In the following sections, we’ll add Swagger to a basic RESTful API built using NestJS.
Initial Setup
The first dependency that we’ll need is the NestJS CLI. Go ahead and install it globally by running npm install -g @nestjs/cli
. Once this is in place, bootstrap a new NestJS project using the CLI command nest new nestjs-with-swagger
. At this point, we should have a boilerplate NestJS project setup with a module, controller, and service.
We’ll now create a simple task tracking application by adding a new module for handling tasks and a few other files, as shown below: