Back to Documentation

Generate OpenAPI Specification

Framework-specific guides to generate an OpenAPI 3.x specification from your existing API

Node.js / Express

Using swagger-jsdoc

The most popular way to generate OpenAPI specs in Express is using JSDoc comments with swagger-jsdoc.

# Install dependencies
npm install swagger-jsdoc swagger-ui-express
// swagger.js
const swaggerJsdoc = require('swagger-jsdoc');

const options = {
  definition: {
    openapi: '3.0.0',
    info: {
      title: 'My API',
      version: '1.0.0',
      description: 'API Documentation',
    },
    servers: [
      {
        url: 'http://localhost:3000',
        description: 'Development server',
      },
    ],
  },
  apis: ['./routes/*.js'], // Path to API routes
};

const specs = swaggerJsdoc(options);
module.exports = specs;
// app.js
const express = require('express');
const swaggerUi = require('swagger-ui-express');
const specs = require('./swagger');

const app = express();

// Swagger UI
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs));

// Your routes with JSDoc comments
/**
 * @swagger
 * /users:
 *   get:
 *     summary: Get all users
 *     responses:
 *       200:
 *         description: Success
 */
app.get('/users', (req, res) => {
  res.json({ users: [] });
});

Access your OpenAPI spec at http://localhost:3000/api-docs

Next Steps

Once you've generated your OpenAPI specification, you're ready to test your API.

Start Testing Your API