# Fumadocs (Framework Mode, TanStack Start): OpenAPI
URL: /docs/tanstack-start/integrations/openapi
Source: https://raw.githubusercontent.com/fuma-nama/fumadocs/refs/heads/main/apps/docs/content/docs/(framework)/[framework]/integrations/openapi/index.mdx

Generating docs for OpenAPI schema.

Generate API reference pages from OpenAPI documents, with a playground to send requests. See the [demo](/docs/openapi).

## Setup [#setup]

Install the required packages.

```npm
npm i fumadocs-openapi shiki
```

### Generate Styles [#generate-styles]

Add the following line:

```css title="Tailwind CSS"
@import 'tailwindcss';
@import 'fumadocs-ui/css/neutral.css';
@import 'fumadocs-ui/css/preset.css';
/* [!code ++] */
@import 'fumadocs-openapi/css/preset.css';
```

### Configure Plugin [#configure-plugin]

Create the OpenAPI server instance and the `<OpenAPIPage />` component.

```ts tab="lib/openapi.ts"
import { createOpenAPI } from 'fumadocs-openapi/server';

// note: this is a server-side API
export const openapi = createOpenAPI({
  // the OpenAPI schema, you can also give it an external URL.
  input: ['./openapi.json'],
});
```

```tsx tab="components/api-page.tsx"
'use client';
import { createOpenAPIPage } from 'fumadocs-openapi/ui';

export const OpenAPIPage = createOpenAPIPage();
```

See [`createOpenAPI()`](/docs/tanstack-start/integrations/openapi/server) and [`createOpenAPIPage()`](/docs/tanstack-start/integrations/openapi/api-page) for their options.

### Generate Pages [#generate-pages]

Pass `staticSource()` to your [`loader()`](/docs/headless/source-api/source). It generates pages without writing files.

`staticSource()` reads files, so `source.ts` becomes server-only. Move `docs` into `collections.ts` for client code to import.

```ts tab="src/lib/source.ts"
import { loader } from 'fumadocs-core/source';
import { defineDocs } from 'fumadocs-mdx/macro'; // [!code --]
// [!code ++:2]
import { docs } from '@/lib/collections';
import { openapi } from '@/lib/openapi';

// [!code --:6]
export const docs = defineDocs({
  dir: 'content/docs',
  docs: {
    async: true,
  },
});

// server-only now, as `staticSource()` reads files [!code ++]
export const source = loader(
  // [!code ++:6]
  {
    docs: docs.toFumadocsSource(),
    openapi: await openapi.staticSource({
      baseDir: 'openapi',
    }),
  },
  {
    baseUrl: '/docs',
    // [!code ++:2] optional: adds a badge to each page item in page tree
    plugins: [openapi.loaderPlugin()],
  },
);
```

```ts tab="src/lib/collections.ts"
import { defineDocs } from 'fumadocs-mdx/macro';

export const docs = defineDocs({
  dir: 'content/docs',
  docs: {
    async: true,
  },
});
```

`staticSource()` generates a page per operation by default. See [Generating Pages](/docs/tanstack-start/integrations/openapi/generate-files) to customize them.

**Pages of `source` now have two types, `docs` and `openapi`.** Update every place that uses `source`, like where you return text for LLMs:

```ts
import { llms } from 'fumadocs-core/source';
import { source } from '@/lib/source';

export const docsLlms = llms(source, {
  renderPage: (page) => {
    // [!code ++:4]
    if (page.type === 'openapi') {
      // e.g. return the stringified OpenAPI schema
      return JSON.stringify(page.data.getSchema().bundled, null, 2);
    }

    // your original flow below...
  },
});
```

Render OpenAPI pages with `<OpenAPIPage />` in your docs page:

```tsx title="src/routes/docs/$.tsx"
import { docs, source } from '@/lib/source'; // [!code --]
// [!code ++:3]
import { docs } from '@/lib/collections';
import { source } from '@/lib/source';
import { OpenAPIPage } from '@/components/api-page';

export const Route = createFileRoute('/docs/$')({
  component: Page,
  loader: async ({ params }) => {
    const slugs = params._splat?.split('/') ?? [];
    const data = await serverLoader({ data: slugs });
    if (data.type === 'docs') await docs.getPage(data.path)?.preload(); // [!code highlight]
    return data;
  },
});

const serverLoader = createServerFn({
  method: 'GET',
})
  .validator((slugs: string[]) => slugs)
  .handler(async ({ data: slugs }) => {
    const page = source.getPage(slugs);
    if (!page) throw notFound();

    // [!code ++:9]
    const pageTree = await source.serializePageTree(source.getPageTree());
    if (page.type === 'openapi') {
      return {
        type: 'openapi',
        title: page.data.title,
        props: page.data.getOpenAPIPageProps(),
        pageTree,
      };
    }

    return { type: 'docs', path: page.path, pageTree }; // [!code highlight]
  });

function Page() {
  const data = useFumadocsLoader(Route.useLoaderData());

  return (
    <DocsLayout {...baseOptions()} tree={data.pageTree}>
      {/* [!code ++:8] */}
      {data.type === 'openapi' && (
        <DocsPage full>
          <DocsTitle>{data.title}</DocsTitle>
          <DocsBody>
            <OpenAPIPage {...data.props} />
          </DocsBody>
        </DocsPage>
      )}
      <Suspense>
        {/* [!code highlight] */}
        {data.type === 'docs' && <Content path={data.path} />}
      </Suspense>
    </DocsLayout>
  );
}
```

<Callout type="warn" title="Ensure the migration is complete!">
Run a type check before continuing:

```npm
npm run types:check
```
</Callout>

To write MDX files into your content directory instead, see [MDX Files](/docs/tanstack-start/integrations/openapi/generate-files#mdx-files).