ReDoc is a fantastic tool for generating OpenAPI documentation, but its true power lies in its ability to make that documentation interactive and discoverable, not just a static dump of your API spec.
Let’s see ReDoc in action. Imagine we have a simple OpenAPI 3.0 specification for a basic pet store API.
openapi: 3.0.0
info:
title: Pet Store API
version: 1.0.0
paths:
/pets:
get:
summary: List all pets
operationId: listPets
responses:
'200':
description: A list of pets.
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Pet'
post:
summary: Create a new pet
operationId: createPet
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/NewPet'
responses:
'201':
description: Null response
/pets/{petId}:
get:
summary: Info for a specific pet
operationId: showPetById
parameters:
- name: petId
in: path
required: true
description: The ID of the pet to retrieve
schema:
type: string
responses:
'200':
description: Information about a pet
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
components:
schemas:
Pet:
type: object
required:
- id
- name
properties:
id:
type: string
format: uuid
description: Unique identifier for the pet
name:
type: string
description: The name of the pet
tag:
type: string
description: An optional tag for the pet
NewPet:
type: object
required:
- name
properties:
name:
type: string
description: The name of the pet
tag:
type: string
description: An optional tag for the pet
To generate ReDoc documentation from this, you’d typically use a tool like redoc-cli. After installing it (npm install -g redoc-cli), you can run:
redoc-cli bundle -o redoc-static.html spec.yaml
This command takes your spec.yaml file, bundles it into a single HTML file named redoc-static.html. When you open redoc-static.html in your browser, you’ll see a beautifully rendered documentation site.
The primary problem ReDoc solves is the disconnect between API development and API consumption. Developers often struggle to understand how to interact with an API, and manual documentation quickly becomes outdated or incomplete. ReDoc, by directly consuming the OpenAPI specification, ensures the documentation is always in sync with the API definition. It provides a structured, searchable, and interactive interface for exploring endpoints, understanding request/response schemas, and even trying out API calls directly from the documentation.
Internally, ReDoc is a JavaScript application that parses the OpenAPI specification and dynamically renders it into HTML. It uses a component-based architecture, with distinct components for displaying schemas, parameters, responses, and the overall navigation. The "Try it out" feature, for example, involves dynamically generating UI elements for parameters and request bodies based on the schema definitions and then using JavaScript to make actual HTTP requests to the specified API server.
The levers you control are primarily within your OpenAPI specification itself. The clarity and completeness of your summary, description, parameters, requestBody, and responses fields directly translate into the quality of the ReDoc output. For example, defining enum values for a parameter or schema property will result in ReDoc displaying these as selectable options, making it much easier for users. Similarly, using $ref for reusable components keeps your spec DRY and ensures consistency in the documentation.
One thing that trips people up is how ReDoc handles authentication. While the OpenAPI spec can define security schemes (like API keys, OAuth2, etc.), ReDoc’s default behavior for interacting with these might require explicit configuration in the ReDoc bundle itself, or the user needs to manually provide credentials in the "Try it out" interface. You can configure ReDoc to prompt for API keys or other credentials, but it’s not always as automatic as one might hope without additional setup.
The next concept you’ll likely explore is how to customize ReDoc’s appearance and behavior beyond the defaults, often through theme options or even by extending its JavaScript components.