createOpenAPI()
The OpenAPI server instance.
OpenAPI Server
The main config for Fumadocs OpenAPI.
It should not be referenced in browser environments.
input
The OpenAPI schemas to read from.
- File Paths
- External URLs
- Functions (see below)
import { createOpenAPI } from 'fumadocs-openapi/server';
export const openapi = createOpenAPI({
input: ['./unkey.json'],
});Creating Proxy
A proxy server is useful for executing HTTP (fetch) requests, as it doesn't have CORS constraints like on the browser.
We can use it for executing HTTP requests on the OpenAPI playground, when the target API endpoints do not have CORS configured correctly.
Warning
Do not use this on unreliable sites and API endpoints, the proxy server will forward all received
headers & body, including HTTP-only Cookies and Authorization header.
Setup
Create a route handler for proxy server.
import { openapi } from '@/lib/openapi';
export const { GET, HEAD, PUT, POST, PATCH, DELETE } = openapi.createProxy({
// important: set a list of allowed origins for proxied requests
allowedOrigins: ['https://example.com'],
});And set the proxy URL in createOpenAPI.
import { createOpenAPI } from 'fumadocs-openapi/server';
export const openapi = createOpenAPI({
proxyUrl: '/api/proxy',
});OAuth Redirect
OAuth providers only accept registered redirect URIs, but the API playground redirects to the current page by default. Create a route handler as the redirect URI of every page, it sends users back to the page that started the flow.
import { openapi } from '@/lib/openapi';
export const GET = openapi.createOAuthHandler();Pass its URL to createOpenAPIPage(), and register the full URL (e.g. https://docs.example.com/api/oauth) on your OAuth provider.
export const OpenAPIPage = createOpenAPIPage({
oauthRedirectUrl: '/api/oauth',
});How is this guide?
Last updated on
