Jsdoc Open ApiQuick Start

Quick Start

Last updated:

Quick Start

Generate your first OpenAPI specification from JSDoc comments in under 5 minutes.

Add JSDoc Comments

Add OpenAPI documentation to your API endpoints:

// src/routes/users.js

/**
 * @openapi
 * /users:
 *   get:
 *     summary: Get all users
 *     description: Retrieve a list of all users
 *     tags:
 *       - Users
 *     responses:
 *       200:
 *         description: List of users
 *         content:
 *           application/json:
 *             schema:
 *               type: array
 *               items:
 *                 $ref: '#/components/schemas/User'
 */
export async function getUsers(req, res) {
    const users = await db.users.findMany();
    res.json(users);
}

/**
 * @openapi
 * /users/{id}:
 *   get:
 *     summary: Get user by ID
 *     tags:
 *       - Users
 *     parameters:
 *       - name: id
 *         in: path
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: User found
 *       404:
 *         description: User not found
 */
export async function getUserById(req, res) {
    const user = await db.users.findUnique({ where: { id: req.params.id } });
    if (!user) {
        return res.status(404).json({ error: "User not found" });
    }
    res.json(user);
}

Generate with CLI

Use the CLI to generate the specification:

# Initialize configuration
npx jsdoc-open-api init

# Generate specification
npx jsdoc-open-api generate src/routes/

Output swaggerSpec.json:

{
    "openapi": "3.0.0",
    "info": {
        "title": "My API",
        "version": "1.0.0"
    },
    "paths": {
        "/users": {
            "get": {
                "summary": "Get all users",
                "description": "Retrieve a list of all users",
                "tags": ["Users"],
                "responses": {
                    "200": {
                        "description": "List of users"
                    }
                }
            }
        },
        "/users/{id}": {
            "get": {
                "summary": "Get user by ID",
                "tags": ["Users"],
                "parameters": [
                    {
                        "name": "id",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "integer"
                        }
                    }
                ],
                "responses": {
                    "200": { "description": "User found" },
                    "404": { "description": "User not found" }
                }
            }
        }
    }
}

Generate Programmatically

Use the JavaScript API:

// scripts/generate-docs.js
import jsdocOpenApi from "@visulima/jsdoc-open-api";
import { writeFileSync } from "fs";

async function generate() {
    const spec = await jsdocOpenApi({
        definition: {
            openapi: "3.0.0",
            info: {
                title: "My API",
                version: "1.0.0",
                description: "API documentation",
            },
            servers: [
                {
                    url: "http://localhost:3000",
                    description: "Development server",
                },
            ],
        },
        sources: ["./src/routes/**/*.js"],
    });

    writeFileSync("./swagger.json", JSON.stringify(spec, null, 2));
    console.log("✅ OpenAPI spec generated!");
}

generate();

Run it:

node scripts/generate-docs.js

Short Syntax

Use the concise short syntax:

/**
 * GET /api/posts
 * @summary Get all posts
 * @tags Posts
 * @response 200 - Success
 */
export async function getPosts(req, res) {
    // ...
}

/**
 * POST /api/posts
 * @summary Create post
 * @tags Posts
 * @bodyparam {string} title.required - Post title
 * @bodyparam {string} content - Post content
 * @response 201 - Created
 * @response 400 - Validation error
 */
export async function createPost(req, res) {
    // ...
}

Define Schemas

Add reusable schemas:

/**
 * @openapi
 * components:
 *   schemas:
 *     User:
 *       type: object
 *       required:
 *         - id
 *         - email
 *       properties:
 *         id:
 *           type: integer
 *         email:
 *           type: string
 *           format: email
 *         name:
 *           type: string
 *         createdAt:
 *           type: string
 *           format: date-time
 */

Reference schemas in endpoints:

/**
 * @openapi
 * /users:
 *   post:
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/User'
 *     responses:
 *       201:
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/User'
 */
export async function createUser(req, res) {
    // ...
}

View Documentation

Use Swagger UI to view your specification:

// server.js with Express
import express from "express";
import swaggerUi from "swagger-ui-express";
import spec from "./swagger.json" assert { type: "json" };

const app = express();

app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(spec));

app.listen(3000, () => {
    console.log("API docs: http://localhost:3000/api-docs");
});

Next Steps

Syntax Guide

Learn all JSDoc comment syntaxes

CLI Usage

Explore CLI commands and options

Webpack Integration

Integrate with build tools

Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues