A React component wrapper for Swagger UI that enables seamless integration of OpenAPI documentation into React applications
npx @tessl/cli install tessl/npm-swagger-ui-react@5.28.0Swagger UI React is a React component wrapper for Swagger UI that enables seamless integration of OpenAPI documentation into React applications. It provides a declarative interface for rendering interactive API documentation with all the features of Swagger UI - including the ability to test API endpoints directly from the documentation.
npm install swagger-ui-reactimport SwaggerUI from "swagger-ui-react"
import "swagger-ui-react/swagger-ui.css"For CommonJS:
const SwaggerUI = require("swagger-ui-react")
require("swagger-ui-react/swagger-ui.css")import SwaggerUI from "swagger-ui-react"
import "swagger-ui-react/swagger-ui.css"
export default function App() {
return (
<SwaggerUI url="https://petstore.swagger.io/v2/swagger.json" />
)
}Swagger UI React is built around these key components:
The component uses React hooks internally and manages the Swagger UI system instance lifecycle, supporting dynamic updates to specs and URLs while maintaining React best practices.
The primary SwaggerUI React component for rendering OpenAPI documentation.
function SwaggerUI(props: SwaggerUIProps): JSX.Element | null;
interface SwaggerUIProps {
// Core content props
spec?: string | object;
url?: string;
// Layout and display props
layout?: string;
docExpansion?: "list" | "full" | "none";
defaultModelExpandDepth?: number;
defaultModelsExpandDepth?: number;
defaultModelRendering?: "example" | "model";
displayOperationId?: boolean;
showExtensions?: boolean;
showCommonExtensions?: boolean;
showMutatedRequest?: boolean;
// Interaction props
supportedSubmitMethods?: Array<"get" | "put" | "post" | "delete" | "options" | "head" | "patch" | "trace">;
tryItOutEnabled?: boolean;
displayRequestDuration?: boolean;
filter?: string | boolean;
deepLinking?: boolean;
// Network and security props
requestInterceptor?: (req: any) => any | Promise<any>;
responseInterceptor?: (res: any) => any | Promise<any>;
withCredentials?: boolean;
oauth2RedirectUrl?: string;
persistAuthorization?: boolean;
// Request snippets props
requestSnippetsEnabled?: boolean;
requestSnippets?: object;
// Plugin system props
plugins?: Array<object | Function> | Function;
presets?: Array<Function>;
queryConfigEnabled?: boolean;
// Lifecycle props
onComplete?: (system: any) => void;
initialState?: object;
uncaughtExceptionHandler?: Function;
}Usage Examples:
// Basic usage with remote spec URL
<SwaggerUI url="https://petstore.swagger.io/v2/swagger.json" />
// Usage with inline spec object
const openApiSpec = {
openapi: "3.0.0",
info: { title: "My API", version: "1.0.0" },
// ... rest of spec
}
<SwaggerUI spec={openApiSpec} />
// Advanced configuration
<SwaggerUI
url="https://api.example.com/openapi.json"
docExpansion="list"
defaultModelRendering="model"
displayOperationId={true}
tryItOutEnabled={true}
filter={true}
requestInterceptor={(req) => {
req.headers.Authorization = "Bearer " + getAuthToken()
return req
}}
onComplete={(system) => {
console.log("Swagger UI loaded:", system)
}}
/>Direct access to the underlying Swagger UI System class for advanced programmatic usage.
const System: typeof SwaggerUISystem;Usage Example:
import SwaggerUI from "swagger-ui-react"
// Access the System class for advanced usage
const system = new SwaggerUI.System({
// system options
})Access to Swagger UI preset configurations for customizing the UI behavior.
const presets: {
base: Function;
apis: Function;
[key: string]: Function;
};Usage Example:
import SwaggerUI from "swagger-ui-react"
// Use presets in advanced configuration
<SwaggerUI
url="https://api.example.com/openapi.json"
presets={[SwaggerUI.presets.apis, customPreset]}
/>Access to individual Swagger UI plugins for fine-grained customization.
const plugins: {
Auth: Function;
Configs: Function;
DeepLinking: Function;
Err: Function;
Filter: Function;
Icons: Function;
JSONSchema5: Function;
JSONSchema5Samples: Function;
JSONSchema202012: Function;
JSONSchema202012Samples: Function;
Layout: Function;
Logs: Function;
OpenAPI30: Function;
OpenAPI31: Function;
OnComplete: Function;
RequestSnippets: Function;
Spec: Function;
SwaggerClient: Function;
Util: Function;
View: Function;
ViewLegacy: Function;
DownloadUrl: Function;
SyntaxHighlighting: Function;
Versions: Function;
SafeRender: Function;
[key: string]: Function;
};Usage Example:
import SwaggerUI from "swagger-ui-react"
// Use specific plugins in configuration
<SwaggerUI
url="https://api.example.com/openapi.json"
plugins={[SwaggerUI.plugins.Auth, SwaggerUI.plugins.RequestSnippets]}
/>Access to configuration defaults and utility functions from the underlying Swagger UI system.
const config: {
defaults: object;
merge: Function;
typeCast: Function;
typeCastMappings: object;
};Usage Example:
import SwaggerUI from "swagger-ui-react"
// Access default configuration values
const defaultLayout = SwaggerUI.config.defaults.layout
console.log("Default layout:", defaultLayout)
// Use merge utility for complex configurations
const mergedConfig = SwaggerUI.config.merge(
SwaggerUI.config.defaults,
{ tryItOutEnabled: true, filter: true }
)spec - OpenAPI document as JavaScript object, JSON string, or YAML string
string | objecturl propurl - Remote URL to OpenAPI document for fetching and parsing
stringspec proplayout - Name of top-level layout component via plugin system
stringdocExpansion - Default expansion setting for operations and tags
"list" | "full" | "none"defaultModelExpandDepth - Default expansion depth for models
numberdefaultModelsExpandDepth - Default expansion depth for models section
numberdefaultModelRendering - Default model rendering mode
"example" | "model"tryItOutEnabled - Controls if Try it out section starts enabled
booleansupportedSubmitMethods - HTTP methods with Try it out enabled
Array<"get" | "put" | "post" | "delete" | "options" | "head" | "patch" | "trace">filter - Enables/configures operation filtering
string | booleanrequestInterceptor - Function to intercept and modify requests
(req: any) => any | Promise<any>responseInterceptor - Function to intercept and modify responses
(res: any) => any | Promise<any>withCredentials - Enables passing credentials in CORS requests
booleanonComplete - Callback triggered when Swagger UI finishes rendering
(system: any) => voidSwagger UI React handles errors through:
{
"react": ">=16.8.0 <20",
"react-dom": ">=16.8.0 <20"
}