Swagger helps you create clear and easy-to-read API docs automatically. It shows what your API can do and how to use it.
Swagger API documentation in NestJS
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; const config = new DocumentBuilder() .setTitle('API Title') .setDescription('API description') .setVersion('1.0') .build(); const document = SwaggerModule.createDocument(app, config); SwaggerModule.setup('api', app, document);
Use DocumentBuilder to set your API info like title and version.
Call SwaggerModule.setup() to serve the docs at a URL (e.g., '/api').
const config = new DocumentBuilder() .setTitle('My App API') .setDescription('API for my app') .setVersion('0.1') .build();
const config = new DocumentBuilder() .addBearerAuth() .setTitle('Secure API') .setVersion('1.0') .build();
SwaggerModule.setup('docs', app, document);This example creates a NestJS app and sets up Swagger docs at /api. When you run it, you can open the URL to see interactive API docs.
import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; async function bootstrap() { const app = await NestFactory.create(AppModule); const config = new DocumentBuilder() .setTitle('Cats API') .setDescription('API to manage cats') .setVersion('1.0') .build(); const document = SwaggerModule.createDocument(app, config); SwaggerModule.setup('api', app, document); await app.listen(3000); console.log('API docs available at http://localhost:3000/api'); } bootstrap();
Remember to decorate your controllers and DTOs with Swagger decorators like @ApiTags() and @ApiProperty() to improve docs.
Swagger UI is interactive, so you can test API calls directly from the browser.
Keep your API docs updated by running the app after code changes.
Swagger auto-generates API docs that are easy to read and test.
Use DocumentBuilder to configure your API info.
Serve docs with SwaggerModule.setup() at a chosen URL.
Practice
Solution
Step 1: Understand Swagger's role
Swagger is a tool that creates easy-to-read and interactive API documentation automatically.Step 2: Compare options
The other options relate to database performance, authentication, and state management, which Swagger does not handle.Final Answer:
To automatically generate interactive API documentation -> Option AQuick Check:
Swagger = API docs [OK]
- Confusing Swagger with authentication tools
- Thinking Swagger improves database queries
- Assuming Swagger manages app state
Solution
Step 1: Identify configuration class
DocumentBuilder is the class used to set API title, description, version, and other info for Swagger.Step 2: Differentiate from other classes
SwaggerModule sets up the docs route, ApiProperty decorates DTO properties, HttpModule handles HTTP requests.Final Answer:
DocumentBuilder -> Option DQuick Check:
API info config = DocumentBuilder [OK]
- Confusing SwaggerModule with DocumentBuilder
- Using ApiProperty for API config instead of DTO properties
- Thinking HttpModule configures Swagger
const config = new DocumentBuilder()
.setTitle('Cats API')
.setDescription('API for cats')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api-docs', app, document);Solution
Step 1: Identify the setup path
The setup method uses 'api-docs' as the first argument, which defines the URL path for Swagger UI.Step 2: Combine with default host and port
By default, NestJS runs on localhost:3000, so the full URL is http://localhost:3000/api-docs.Final Answer:
http://localhost:3000/api-docs -> Option AQuick Check:
SwaggerModule.setup path = api-docs [OK]
- Assuming default path is /swagger or /docs
- Confusing createDocument with setup URL
- Ignoring the string passed to setup()
const config = new DocumentBuilder()
.setTitle('Dogs API')
.setDescription('API for dogs')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(config, app);
SwaggerModule.setup('docs', app, document);Solution
Step 1: Check createDocument arguments
The correct order is (app, config), but here it is (config, app), which causes an error.Step 2: Verify other parts
build() is called correctly, setup path 'docs' is valid, and DocumentBuilder can set version.Final Answer:
The order of arguments in createDocument is incorrect -> Option CQuick Check:
createDocument(app, config) order matters [OK]
- Swapping app and config in createDocument
- Forgetting to call build() on DocumentBuilder
- Thinking setup path must be '/swagger'
Solution
Step 1: Understand global prefix effect
Setting a global prefix like 'api' adds it to all routes except those explicitly set in SwaggerModule.setup.Step 2: Confirm Swagger path independence
SwaggerModule.setup('docs', app, document) serves docs at '/docs' ignoring global prefix, so docs remain accessible.Step 3: Analyze other options
Other sequences either omit the global prefix or incorrectly include it in the Swagger path, leading to double prefixing or wrong Swagger location.Final Answer:
app.setGlobalPrefix('api'); SwaggerModule.setup('docs', app, document); -> Option BQuick Check:
Global prefix separate from Swagger path [OK]
- Adding global prefix inside Swagger path causing double prefix
- Not setting global prefix when needed
- Changing Swagger path to include global prefix unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
