Hanzo
Quick StartIntegrations

OpenAPI

Generating docs for OpenAPI schema

Before Reading

  • Only OpenAPI 3.0 and 3.1 are supported.
  • It only works under RSC environments.

Manual Setup

Install the required packages.

npm i @hanzo/docs-openapi shiki

For Bun PM

If you encountered any issues, please check #2223.

Generate Styles

Add the following line:

@import 'tailwindcss';
@import '@hanzo/docs-ui/css/neutral.css';
@import '@hanzo/docs-ui/css/preset.css';
@import '@hanzo/docs-openapi/css/preset.css';

Configure Plugin

Create an OpenAPI instance on the server.

import { createOpenAPI } from '@hanzo/docs-openapi/server';

export const openapi = createOpenAPI({
  // the OpenAPI schema, you can also give it an external URL.
  input: ['./unkey.json'],
});

Create a APIPage component:

import { openapi } from '@/lib/openapi';
import { createAPIPage } from '@hanzo/docs-openapi/ui';
import client from './api-page.client';

export const APIPage = createAPIPage(openapi, {
  client,
});

Generate Pages

You can generate MDX files directly from your OpenAPI schema.

Create a script:

import { generateFiles } from '@hanzo/docs-openapi';
import { openapi } from '@/lib/openapi';

void generateFiles({
  input: openapi,
  output: './content/docs',
  // we recommend to enable it
  // make sure your endpoint description doesn't break MDX syntax.
  includeDescription: true,
});

Generate docs with the script:

bun ./scripts/generate-docs.ts

Add the APIPage component to your MDX components.

import defaultComponents from '@hanzo/docs-ui/mdx';
import { APIPage } from '@/components/api-page';
import type { MDXComponents } from 'mdx/types';

export function getMDXComponents(components?: MDXComponents) {
  return {
    ...defaultComponents,
    // should be available in MDX files
    APIPage,
    ...components,
  } satisfies MDXComponents;
}

Features

The official OpenAPI integration supports:

  • Basic API endpoint information
  • Interactive API playground
  • Example code to send request (in different programming languages)
  • Response samples and TypeScript definitions
  • Request parameters and body generated from schemas

Demo

How is this guide?

Last updated on

On this page