NestJS
Install
npm install @nestjs/swagger
Setup
Initialize the Swagger module in main.ts:
// main.ts
import { NestFactory } from '@nestjs/core'
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'
import { AppModule } from './app.module'
async function bootstrap() {
const app = await NestFactory.create(AppModule)
const config = new DocumentBuilder()
.setTitle('My API')
.setDescription('API description')
.setVersion('1.0')
.build()
const document = SwaggerModule.createDocument(app, config)
SwaggerModule.setup('api', app, document)
// Also expose raw JSON
app.getHttpAdapter().get('/openapi.json', (req, res) => {
res.json(document)
})
await app.listen(3000)
}
bootstrap()
The spec is now available at:
http://localhost:3000/openapi.json
Add descriptions
Use decorators to enrich your spec:
import { ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger'
@ApiTags('items')
@Controller('items')
export class ItemsController {
@Get(':id')
@ApiOperation({ summary: 'Get a single item', operationId: 'getItem' })
@ApiParam({ name: 'id', type: Number })
getItem(@Param('id') id: number) {
return { id }
}
}
Make it publicly accessible
Deploy your app or use a tunnel for local testing:
ngrok http 3000
# then use: https://<random>.ngrok.io/openapi.json
Verify
Visit /openapi.json in your browser. You should see a valid OpenAPI 3.x JSON document.
Paste this URL into Automiel’s import step.