Overview

Auto-generate API documentation.

Features

  • Auto-docs
  • Schema inference
  • OpenAPI spec
  • Dev-only mode

Installation

npx hanma add elysia-swagger --framework elysia

Dependencies

@elysiajs/swagger

Code

import type { Elysia } from "elysia";
import { swagger } from "@elysiajs/swagger";

/**
 * Swagger documentation setup for Elysia
 * Only enabled in non-production environments
 */
export const setupSwagger = <T extends Elysia>(app: T) => {
  const isProd = process.env.NODE_ENV === "production";

  if (isProd) {
    return app;
  }

  return app.use(
    swagger({
      documentation: {
        info: {
          title: process.env.npm_package_name || "Elysia API",
          version: process.env.npm_package_version || "1.0.0",
          description:
            process.env.npm_package_description || "API documentation",
        },
        tags: [
          { name: "auth", description: "Authentication endpoints" },
          { name: "users", description: "User management" },
          { name: "health", description: "Health check endpoints" },
        ],
      },
      path: "/docs",
      exclude: ["/docs", "/docs/json"],
    })
  );
};

/* USAGE:

import { Elysia } from "elysia";
import { setupSwagger } from "./plugins/swagger";

const app = new Elysia()
  .use(setupSwagger)
  .get(
    "/users/:id",
    ({ params }) => {
      return { id: params.id };
    },
    {
      detail: {
        summary: "Get user by ID",
        tags: ["users"],
      },
    }
  );

// Access docs at: http://localhost:3000/docs

*/

Usage

import { setupSwagger } from './plugins/swagger';

app.use(setupSwagger);

Dependencies

Frequently Asked Questions

How do I install Swagger Plugin?
Run the following command: npx hanma add elysia-swagger --framework elysia
What dependencies are required?
This snippet requires: @elysiajs/swagger. Make sure to install them before using the snippet.
How do I use this snippet?
Copy the code and paste it into your project. The snippet will be saved to src/plugins/swagger.ts.
Can I customize this snippet?
Yes! All snippets are copy-paste code that you own. Feel free to modify them to fit your needs.
Does this work with other frameworks?
This snippet is designed for the specific framework. Check the other framework pages for equivalent implementations.