What is redoc?
Redoc is an open-source tool for generating interactive API documentation from OpenAPI (formerly Swagger) definitions. It provides a user-friendly interface for exploring and understanding API endpoints, parameters, and responses.
What are redoc's main functionalities?
Render API Documentation
This feature allows you to render API documentation from an OpenAPI specification file. The `init` method takes the path to the OpenAPI file, optional configuration options, and the HTML element where the documentation should be rendered.
const Redoc = require('redoc');
Redoc.init('path/to/your/openapi.yaml', {
scrollYOffset: 50
}, document.getElementById('redoc-container'));
Customization Options
Redoc allows for extensive customization of the rendered documentation. You can change the theme, colors, fonts, and other styles to match your branding.
const Redoc = require('redoc');
Redoc.init('path/to/your/openapi.yaml', {
theme: {
colors: {
primary: {
main: '#dd5522'
}
}
}
}, document.getElementById('redoc-container'));
Standalone HTML Generation
Redoc provides a CLI tool to generate a standalone HTML file from an OpenAPI specification. This can be useful for hosting static API documentation without needing a server.
const { exec } = require('child_process');
exec('npx redoc-cli bundle path/to/your/openapi.yaml', (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
return;
}
if (stderr) {
console.error(`Stderr: ${stderr}`);
return;
}
console.log(`Stdout: ${stdout}`);
});
Other packages similar to redoc
swagger-ui
Swagger UI is another popular tool for generating interactive API documentation from OpenAPI specifications. It offers a more traditional interface compared to Redoc and is highly customizable. Swagger UI is often used in conjunction with other Swagger tools for a complete API development workflow.
api-docs
API Docs is a lightweight tool for generating API documentation from OpenAPI specifications. It focuses on simplicity and ease of use, making it a good choice for smaller projects or teams that need quick and straightforward documentation.
spectacle
Spectacle is a tool for generating API documentation from OpenAPI specifications with a focus on readability and user experience. It offers a clean and modern interface, similar to Redoc, but with different customization options and features.
ReDoc
Swagger-generated API Reference Documentation
Live demo
Deployment
tl;dr
<!DOCTYPE html>
<html>
<head>
<title>ReDoc</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
margin: 0;
padding: 0;
font-family: Verdana, Geneva, sans-serif;
font-size: 14px;
color: #333;
}
</style>
</head>
<body>
<redoc spec-url='http://petstore.swagger.io/v2/swagger.json'>
</redoc>
<script src="bower_components/redoc/dist/redoc.min.js"> </script>
</body>
</html>
1. Install redoc
Install using bower:
bower install redoc
or using npm:
npm install redoc --save
Alternatively you can just download redoc.min.js
.
2. Reference redoc script in HTML
Then reference redoc.min.js
in your HTML page:
<script src="bower_components/redoc/dist/redoc.min.js"> </script>
For npm:
<script src="node_modules/redoc/dist/redoc.min.js"> </script>
3. Add <redoc>
element to your page
<redoc spec-url="<url to your spec>"></redoc>
4. Enjoy :smile:
Configuration
Swagger vendor extensions
ReDoc makes use of the following vendor extensions:
x-logo
- is used to specify API logox-traitTag
- useful for handling out common things like Pagination, Rate-Limits, etcx-code-samples
- specify operation code samples
Options
spec-url
- relative or absolute url to your spec filescroll-y-offset
- If set, specifies a vertical scroll-offset. This is often useful when there are fixed positioned elements at the top of the page, such as navbars, headers etc.
scroll-y-offset
can be specified in various ways:
- number: A fixed number of pixels to be used as offset
- selector: selector of the element to be used for specifying the offset. The distance from the top of the page to the element's bottom will be used as offset.
- function: A getter function. Must return a number representing the offset (in pixels).
Advanced usage
Instead of adding spec-url
attribute to the <redoc>
element you can initialize ReDoc via globally exposed Redoc
object:
Redoc.init(specUrl, options)
options
is javascript object with camel-cased version of options names as the keys. For example:
Redoc.init('http://petstore.swagger.io/v2/swagger.json', {
scrollYOffset: 50
})
Running locally
- Clone repository
git clone https://github.com/Rebilly/ReDoc.git
- Go to the project folder
cd ReDoc
- Install node modules and front-end dependencies
npm install
npm run jspm-install
- (optional) Replace
demo/swagger.json
with your own schema - Start the server
npm start
- Open
http://localhost:9000