API Documentation with Swagger
Document APIs with Swagger. Generate interactive documentation.
API Documentation with Swagger
Setup
```bash npm install swagger-jsdoc swagger-ui-express ```
```javascript 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
```javascript /** * @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.