Swagger is a tool to document your API using the OpenAPI specification. Nest has a dedicated module for Swagger, which you will be using shortly.
Get started by installing the required dependencies:
npm install --save @nestjs/swagger swagger-ui-express
Now open main.ts
and initialize Swagger using the SwaggerModule
class.
Solution
// src/main.ts import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; async function bootstrap() { const app = await NestFactory.create(AppModule); const config = new DocumentBuilder() .setTitle('Median') .setDescription('The Median API description') .setVersion('0.1') .build(); const document = SwaggerModule.createDocument(app, config); SwaggerModule.setup('api', app, document); await app.listen(3000); } bootstrap();
While the application is running, open your browser and navigate to http://localhost:3000/api. You should see the Swagger UI.