Node.js6 min read
API Documentation with Swagger
Document APIs with Swagger. Generate interactive documentation.
Michael Torres
December 19, 2025
0.0k0
API Documentation with Swagger
Setup
npm install swagger-jsdoc swagger-ui-express
const swaggerJsdoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const options = {
definition: {
openapi: '3.0.0',
info: {
title: 'My API',
version: '1.0.0'
},
servers: [
{
url: 'http://localhost:3000'
}
]
},
apis: ['./routes/*.js']
};
const specs = swaggerJsdoc(options);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs));
Document Routes
/**
* @swagger
* /api/users:
* get:
* summary: Get all users
* responses:
* 200:
* description: List of users
*/
app.get('/api/users', async (req, res) => {
const users = await User.find();
res.json(users);
});
Key Takeaway
Swagger generates interactive docs. Document as you code. Keep docs updated with code.
#Node.js#API#Documentation#Swagger