Skip to main content
The mermaid export is the primary API for integrating Mermaid into your application. It provides a queue-based, user-friendly interface for rendering diagrams, parsing syntax, and managing configuration.

Import

Interface

Core methods

initialize()

Configure Mermaid before rendering diagrams.
Call initialize() before using run() or render(). Configuration set here applies to all subsequent operations.
Example:
Parameters:
  • config - Configuration object. See Configuration for all options.

render()

Render a diagram to SVG code programmatically.
Parameters:
  • id - Unique identifier for the SVG element
  • text - Mermaid diagram definition
  • container (optional) - HTML element where the SVG will be inserted
Returns: Promise<RenderResult>
Example:
Multiple calls to render() are automatically queued and executed serially to prevent race conditions.
With container element:

parse()

Validate diagram syntax and return metadata.
Parameters:
  • text - Mermaid diagram definition to parse
  • parseOptions (optional)
    • suppressErrors?: boolean - Return false instead of throwing on error
Returns: Promise<ParseResult> or Promise<false> when suppressErrors: true
Example:
Validation workflow:

run()

Find and render all diagrams in the DOM.
Parameters:
  • options (optional)
Example - Basic usage:
Example - Custom selector:
Example - Specific nodes:
Example - Post-render callback:
Example - Error handling:
run() skips elements that have already been processed (marked with data-processed attribute). You can call it multiple times safely.

Registration methods

registerExternalDiagrams()

Register custom diagram types.
Parameters:
  • diagrams - Array of diagram definitions
  • options.lazyLoad - If false, load immediately. Default: true
Example:

registerLayoutLoaders()

Register custom layout algorithms.
Example:

registerIconPacks()

Register custom icon packs for use in diagrams.
Example:

Utility methods

detectType()

Detect the diagram type from text.
Example:

getRegisteredDiagramsMetadata()

Get metadata for all registered diagram types.
Example:

setParseErrorHandler()

Set a global error handler for parse errors.
Example:

Properties

startOnLoad

Control whether Mermaid automatically renders diagrams on page load.
Example:
It’s better to use initialize({ startOnLoad: false }) instead of setting this property directly.

Deprecated methods

init()

Deprecated. Use initialize() and run() instead.
Migration:

Complete example

See also

mermaidAPI

Lower-level API (internal use)

Configuration

Configuration options reference