Full NestJS Starter Kit — Part 5 API Documentation & Client Code Generation

Andrew Larsen
8 min readJan 25, 2024

In this series of articles we’re walking through how to create a NestJS starter kit to use for your cloud applications. In our previous article we walked through how to setup observability in our app via logging, tracing, and metrics.

In this article we’ll setup our starter kit to expose documentation of our APIs via Swagger & OpenAPI. Additionally, we’ll generate typescript client code that can be leveraged by our front end applications.

If you are interested in learning more about what we do at Compoze Labs, please reach out!

connect@compozelabs.com

OpenAPI & Swagger

OpenAPI is an open source standard for describing RESTful APIs. It is intended for both human and machine readability. On top of this documentation, we can leverage tools to generate clickable documentation (Swagger UI) and generate actual source code to call our APIs with. We’ll walk through configuring Swagger and generating client code!

Configuring Swagger

NestJS offers first class support for swagger which makes the initial setup easy. First, install the NestJS module:

pnpm install @nestjs/swagger

Next, we need to configure our app to use it. For this we’ll modify the bootstrap method in our main.ts file.

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';

import { LoggerProvider } from './logging/logger.provider';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const logger = await app.resolve(LoggerProvider)
app.useLogger(logger);

const config = new DocumentBuilder() // define Swagger UI via buildter
.setTitle('Starter Kit API')
.setDescription('')
.setVersion('1.0')
.addTag('startkit')
.build();

const document = SwaggerModule.createDocument(app, config); // create doc

SwaggerModule.setup('api', app, document); // add to our nestjs app

await app.listen(3000);
}
bootstrap();

If we run our app and navigate to the /api url we’ll see our Swagger UI is exposed.

--

--