Fumadocs
Integrations/OpenAPI

Configurations

Customise Fumadocs OpenAPI

File Generator

Pass options to the generateFiles function.

Input

An array of input files. Allowed:

  • File Paths
  • External URLs
  • Wildcard
import { generateFiles } from 'fumadocs-openapi';

void generateFiles({
  input: ['./unkey.json'],
});

Output

The output directory.

import { generateFiles } from 'fumadocs-openapi';

void generateFiles({
  output: '/content/docs',
});

Per

Customise how the page is generated, default to operation.

Operation in OpenAPI schema refers to an API endpoint with specific method like /api/weather:GET.

modeoutput
tagoperations with same tag{tag_name}.mdx
fileoperations in schema schema{file_name}.mdx
operationeach operation{operationId ?? endpoint_path}.mdx)
import { generateFiles } from 'fumadocs-openapi';

void generateFiles({
  per: 'tag',
});

Group By

In operation mode, you can group output files with folders.

valueoutput
tag{tag}/{operationId ?? endpoint_path}.mdx
route{endpoint_path}/{method}.mdx (ignores name option)
none{operationId ?? endpoint_path}.mdx (default)
import { generateFiles } from 'fumadocs-openapi';

void generateFiles({
  per: 'operation',
  groupBy: 'tag',
});

Name

A function that controls the output path of MDX pages.

import { generateFiles } from 'fumadocs-openapi';

void generateFiles({
  name: (output, document) => {
    // page info
    console.log(output);
    // parsed OpenAPI schema
    console.log(document);
    return 'my-dir/filename';
  },
});

Frontmatter

Customise the frontmatter of MDX files.

By default, it includes:

propertydescription
titlePage title
descriptionPage description
fullAlways true, added for Fumadocs UI
methodAvailable method of operation (operation mode)
routeRoute of operation (operation mode)
import { generateFiles } from 'fumadocs-openapi';

void generateFiles({
  input: ['./petstore.yaml'],
  output: './content/docs',
  frontmatter: (title, description) => ({
    myProperty: 'hello',
  }),
});

Add Generated Comment

Add a comment to the top of generated files indicating they are auto-generated.

import { generateFiles } from 'fumadocs-openapi';

void generateFiles({
  input: ['./petstore.yaml'],
  output: './content/docs',
  // Add default comment
  addGeneratedComment: true,

  // Or provide a custom comment
  addGeneratedComment: 'Custom auto-generated comment',

  // Or disable comments
  addGeneratedComment: false,
});

Tag Display Name

Adding x-displayName to OpenAPI Schema can control the display name of your tags.

openapi.yaml
tags:
  - name: test
    description: this is a tag.
    x-displayName: My Test Name

OpenAPI Server

The server to render pages.

Generate Code Samples

Generate custom code samples for each API endpoint. Make sure to install the types package to give you type-safety when customising it:

npm install openapi-types -D
pnpm add openapi-types -D
yarn add openapi-types --dev
bun add openapi-types --dev
import { createOpenAPI } from 'fumadocs-openapi/server';

export const openapi = createOpenAPI({
  generateCodeSamples(endpoint) {
    return [
      {
        lang: 'js',
        label: 'JavaScript SDK',
        source: "console.log('hello')",
      },
    ];
  },
});

In addition, you can also specify code samples via OpenAPI schema.

paths:
  /plants:
    get:
      x-codeSamples:
        - lang: js
          label: JavaScript SDK
          source: |
            const planter = require('planter');
            planter.list({ unwatered: true });

Disable Code Sample

You can disable the code sample for a specific language, for example, to disable cURL:

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

export const openapi = createOpenAPI({
  generateCodeSamples(endpoint) {
    return [
      {
        lang: 'curl',
        label: 'cURL',
        source: false,
      },
    ];
  },
});

Renderer

Customise components in the page.

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

export const openapi = createOpenAPI({
  renderer: {
    Root(props) {
      // your own (server) component
    },
  },
});

Advanced

Using API Page

This is not a public API, use it carefully.

To use the APIPage component in your MDX files:

---
title: Delete Api
full: true
---

<APIPage
  document="./unkey.json"
  operations={[{ path: '/v1/apis.deleteApi', method: 'post' }]}
  hasHead={false}
/>

Unlike using the generateFiles() function, this supports revalidation of the OpenAPI schema if given an URL.

PropDescription
documentOpenAPI Schema
operationsOperations (API endpoints) to be rendered
hasHeadEnable to render the heading of operation

How is this guide?

Last updated on