Skip to main content

generateFiles

Generate MDX documentation files from OpenAPI schemas and write them to the filesystem.
import { generateFiles } from 'fumadocs-openapi';

await generateFiles({
  input: server,
  output: './content/docs',
  per: 'operation',
  groupBy: 'tag',
});

generateFilesOnly

Generate MDX files without writing to filesystem. Returns array of OutputFile objects.
import { generateFilesOnly } from 'fumadocs-openapi';

const files = await generateFilesOnly({
  input: server,
  per: 'operation',
});

// files: OutputFile[]
// { path: string, content: string }[]
Accepts same options as generateFiles() except output.

fromSchema

Generate output entries from a processed OpenAPI document.
import { fromSchema } from 'fumadocs-openapi/utils/pages/builder';
import { createAutoPreset } from 'fumadocs-openapi/utils/pages/preset-auto';

const entries = fromSchema(
  'schema-id',
  processedDocument,
  createAutoPreset({ per: 'operation' })
);
Returns: OutputEntry[] - Array of generated page entries

createAutoPreset

Create automatic page generation configuration.
import { createAutoPreset } from 'fumadocs-openapi/utils/pages/preset-auto';

const preset = createAutoPreset({
  per: 'operation',
  groupBy: 'tag',
  slugify: (name) => name.toLowerCase(),
});
Accepts SchemaToPagesOptions configuration.

IndexConfig

Configuration for generating index pages with cards.
interface IndexConfig {
  items: IndexItem[] | ((ctx: BeforeWriteContext) => IndexItem[]);
  url:
    | ((filePath: string) => string)
    | {
        baseUrl: string;
        contentDir: string;
      };
}

IndexItem

interface IndexItem {
  path: string;
  title?: string;
  description?: string;
  only?: string[];
}

OutputEntry Types

Generated output entries with metadata:

OperationOutput

interface OperationOutput {
  type: 'operation';
  path: string;
  schemaId: string;
  item: OperationItem;
  info: {
    title: string;
    description?: string;
  };
}

WebhookOutput

interface WebhookOutput {
  type: 'webhook';
  path: string;
  schemaId: string;
  item: WebhookItem;
  info: {
    title: string;
    description?: string;
  };
}

TagOutput

interface TagOutput {
  type: 'tag';
  path: string;
  schemaId: string;
  tag: string;
  rawTag: TagObject;
  operations: OperationItem[];
  webhooks: WebhookItem[];
  info: {
    title: string;
    description?: string;
  };
}

OutputGroup

interface OutputGroup {
  type: 'group';
  path: string;
  schemaId: string;
  operations: OperationItem[];
  webhooks: WebhookItem[];
  info: {
    title: string;
    description?: string;
  };
}

Example: Custom Generation

import { generateFiles } from 'fumadocs-openapi';
import { createOpenAPI } from 'fumadocs-openapi/server';

const server = createOpenAPI({
  input: ['./openapi.yaml'],
});

await generateFiles({
  input: server,
  output: './content/docs/api',
  per: 'operation',
  groupBy: 'tag',
  name: {
    algorithm: 'v2',
  },
  index: {
    items: [
      {
        path: 'index',
        title: 'API Reference',
        description: 'REST API endpoints',
      },
    ],
    url: {
      baseUrl: '/docs',
      contentDir: './content/docs',
    },
  },
  beforeWrite(files) {
    console.log(`Generated ${files.length} files`);
  },
});

Example: Custom File Naming

await generateFiles({
  input: server,
  output: './content/docs',
  per: 'operation',
  name(output, document) {
    if (output.type === 'operation') {
      const operation = document.paths![output.item.path]![output.item.method]!;
      return operation.operationId || `${output.item.method}-${output.item.path}`;
    }
    return 'default';
  },
});

Example: Group by Custom Function

await generateFiles({
  input: server,
  output: './content/docs',
  per: 'operation',
  groupBy: (entry) => {
    // Group by API version from path
    if (entry.type === 'operation') {
      const match = entry.item.path.match(/^\/v(\d+)/);
      return match ? `v${match[1]}` : 'general';
    }
    return 'general';
  },
});

Build docs developers (and LLMs) love