SvelteKit OpenAPI
Generator
Document your APIs effortlessly with JSDoc annotations.
Live updates, TypeScript support, and zero configuration needed.
Powerful Features Out of the Box
Everything you need to create professional API documentation for your SvelteKit applications
Hot Module Replacement
Specs update live as you edit JSDoc comments. See changes instantly in dev mode.
Virtual Module
Import spec directly with import spec from "virtual:openapi-spec". No file I/O needed.
Smart Merging
Combines multiple specs intelligently using openapi-merge for unified documentation.
TypeScript Support
Full type support with automatic type stripping for .ts files. Type-safe by default.
SvelteKit Native
Handles route parameters, groups, and optional segments automatically from file structure.
Swagger UI Ready
Easy integration with Swagger UI for beautiful, interactive API documentation.
Quick Installation
Get started in minutes with bun, npm, yarn, or pnpm
bun add -d sveltekit-openapi-generatorConfigure the Plugin
Add the plugin to your vite.config.js before the SvelteKit plugin
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
import openapiPlugin from 'sveltekit-openapi-generator';
export default defineConfig({
plugins: [
openapiPlugin({
// OpenAPI info section
info: {
title: 'My API',
version: '1.0.0',
description: 'My API Description'
},
// OpenAPI servers configuration
servers: [
{ url: 'https://api.example.com', description: 'Production' },
{ url: 'http://localhost:5173', description: 'Development' }
],
// Path to shared schema definitions
baseSchemasPath: 'src/lib/schemas.js',
// Additional YAML files to include
yamlFiles: ['src/lib/extra-specs.yaml'],
// Path prefix for all routes
prependPath: '/api',
// Glob patterns to include
include: ['src/routes/**/{+server,+page.server}.{js,ts}'],
// Glob patterns to exclude
exclude: ['**/node_modules/**', '**/.svelte-kit/**'],
// Whether to fail on JSDoc parsing errors
failOnErrors: false,
// Output path for the spec file during build
outputPath: 'static/openapi.json',
// Debounce delay in milliseconds for file watching
debounceMs: 200
}),
sveltekit()
]
});Document Your Endpoints
Add @swagger JSDoc blocks to your server files
/**
* @swagger
* /api/users:
* get:
* summary: Get all users
* tags:
* - Users
* responses:
* 200:
* description: Success
* content:
* application/json:
* schema:
* type: array
* items:
* $ref: '#/components/schemas/User'
*/
export async function GET({ url }) {
const users = await db.getUsers();
return json({ users });
}Define Shared Schemas (Optional)
Create reusable component schemas to avoid duplication
/**
* @swagger
* components:
* schemas:
* User:
* type: object
* required:
* - id
* - email
* properties:
* id:
* type: string
* format: uuid
* email:
* type: string
* format: email
* name:
* type: string
*/You're all set!
Your OpenAPI spec is now available at /openapi-spec.json during development. Import it with import spec from 'virtual:openapi-spec' in your components.
Configuration
Customize your API documentation details in vite.config.ts
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
import openapiPlugin from 'sveltekit-openapi-generator';
export default defineConfig({
plugins: [
openapiPlugin({
// OpenAPI info section
info: {
title: 'My API',
version: '1.0.0',
description: 'My API Description'
},
// OpenAPI servers configuration
servers: [
{ url: 'https://api.example.com', description: 'Production' },
{ url: 'http://localhost:5173', description: 'Development' }
],
// Path to shared schema definitions
baseSchemasPath: 'src/lib/schemas.js',
// Additional YAML files to include
yamlFiles: ['src/lib/extra-specs.yaml'],
// Path prefix for all routes
prependPath: '/api',
// Glob patterns to include
include: ['src/routes/**/{+server,+page.server}.{js,ts}'],
// Glob patterns to exclude
exclude: ['**/node_modules/**', '**/.svelte-kit/**'],
// Whether to fail on JSDoc parsing errors
failOnErrors: false,
// Output path for the spec file during build
outputPath: 'static/openapi.json',
// Debounce delay in milliseconds for file watching
debounceMs: 200
}),
sveltekit()
]
});Advanced Configuration
| Option | Type | Description | Default |
|---|---|---|---|
| info | OpenAPIV3.InfoObject | OpenAPI info section (title, version, description) | - |
| servers | OpenAPIV3.ServerObject[] | OpenAPI servers configuration | - |
| baseSchemasPath | string | Path to file containing shared schema definitions | - |
| yamlFiles | string[] | Additional YAML files to include | - |
| prependPath | string | Path prefix to prepend to all routes (e.g., '/api') | - |
| include | string[] | Glob patterns to include | ['src/routes/**/{+server,+page.server}.{js,ts}'] |
| exclude | string[] | Glob patterns to exclude | ['**/node_modules/**', '**/.svelte-kit/**'] |
| failOnErrors | boolean | Whether to fail on JSDoc parsing errors | false |
| outputPath | string | Output path for the spec file during build | - |
| debounceMs | number | Debounce delay in milliseconds for file watching | 200 |
See It In Action
This website itself is built with SvelteKit OpenAPI Generator. Explore the generated documentation and API endpoints.
Ready to Get Started?
Join developers who are already using SvelteKit OpenAPI Generator
Comments & Discussions
Have questions or feedback? Join the discussion on GitHub.