Skip to main content
Mermaid provides a JavaScript API for programmatic diagram rendering and parsing. The API is designed to support both simple use cases and advanced integrations.

Architecture

Mermaid’s API has two main layers:
  1. mermaid - High-level, user-facing API with automatic queueing and error handling
  2. mermaidAPI - Lower-level internal API (deprecated for direct use)
Most users should use the mermaid export. The mermaidAPI is marked as deprecated for external use and is primarily for internal implementation.

Main exports

The mermaid package exports the following primary interfaces:

mermaid

High-level API with run, render, parse, and initialization methods

mermaidAPI

Low-level API (deprecated for external use)

Installation

Install mermaid from npm:
Import in your JavaScript/TypeScript code:

Core concepts

Initialization vs. runtime

Mermaid separates configuration (initialization) from execution:
  • initialize() - Set configuration before rendering
  • run() - Find and render diagrams in the DOM
  • render() - Render a specific diagram programmatically

Queue-based execution

The mermaid.render() and mermaid.parse() methods use an internal execution queue to ensure diagrams are processed serially. This prevents race conditions and ensures consistent results.

Error handling

Mermaid provides multiple ways to handle errors:
  • suppressErrors option in run() to log instead of throw
  • parseError callback for custom error handling
  • setParseErrorHandler() to set error handler globally

Common workflows

Workflow 1: Auto-rendering on page load

The simplest approach for static sites:
Mermaid will automatically find and render all elements with the .mermaid class when the page loads.

Workflow 2: Manual rendering

For dynamic applications:

Workflow 3: Validation and parsing

For editors and validators:

API comparison

TypeScript support

Mermaid includes full TypeScript definitions:

Next steps

mermaid API

Learn about the high-level mermaid API

Configuration

Explore configuration options