Architecture
Mermaid’s API has two main layers:- mermaid - High-level, user-facing API with automatic queueing and error handling
- 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: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
Themermaid.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 class when the page loads.
Workflow 2: Manual rendering
For dynamic applications:Workflow 3: Validation and parsing
For editors and validators:API comparison
| Feature | mermaid | mermaidAPI |
|---|---|---|
| Status | Recommended | Deprecated for external use |
| Queueing | Automatic | Manual |
| Error handling | Built-in | Manual |
| Use case | All applications | Internal implementation |
TypeScript support
Mermaid includes full TypeScript definitions:Next steps
mermaid API
Learn about the high-level mermaid API
Configuration
Explore configuration options