> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/mermaid-js/mermaid/llms.txt
> Use this file to discover all available pages before exploring further.

# Quickstart

> Create your first Mermaid diagram in under 5 minutes

# Quickstart

This guide will help you create and render your first Mermaid diagram. You'll learn the basic workflow for integrating Mermaid into your project.

## Step 1: Install Mermaid

First, install Mermaid in your project:

<CodeGroup>
  ```bash npm theme={null}
  npm install mermaid
  ```

  ```bash yarn theme={null}
  yarn add mermaid
  ```

  ```bash pnpm theme={null}
  pnpm add mermaid
  ```
</CodeGroup>

<Tip>
  Alternatively, you can use the CDN approach if you want to skip the installation. See the [installation guide](/installation#cdn) for details.
</Tip>

## Step 2: Create your HTML file

Create an HTML file with a div element that will contain your diagram. Use the class `mermaid` to mark elements for automatic rendering:

```html index.html theme={null}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>My First Mermaid Diagram</title>
</head>
<body>
  <h1>My First Mermaid Diagram</h1>
  
  <div class="mermaid">
    graph LR
      A[Start] --> B{Is it working?}
      B -->|Yes| C[Great!]
      B -->|No| D[Check the console]
      C --> E[End]
      D --> E[End]
  </div>

  <script type="module">
    import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';
    mermaid.initialize({ startOnLoad: true });
  </script>
</body>
</html>
```

<Note>
  The `mermaid.initialize()` function configures Mermaid. Setting `startOnLoad: true` tells Mermaid to automatically render all diagrams when the page loads.
</Note>

## Step 3: Initialize and render

If you're using Mermaid in a JavaScript application, here's how to initialize and render diagrams programmatically:

<Steps>
  <Step title="Import Mermaid">
    Import the Mermaid library in your JavaScript file:

    ```javascript app.js theme={null}
    import mermaid from 'mermaid';
    ```
  </Step>

  <Step title="Initialize Mermaid">
    Configure Mermaid with the `initialize()` function. This should be done before rendering:

    ```javascript app.js theme={null}
    mermaid.initialize({
      startOnLoad: true,
      theme: 'default',
      logLevel: 'error',
    });
    ```

    Common configuration options:

    * `startOnLoad`: Automatically render diagrams on page load (default: `true`)
    * `theme`: Visual theme (`'default'`, `'dark'`, `'forest'`, `'neutral'`)
    * `logLevel`: Console logging level (`'debug'`, `'info'`, `'warn'`, `'error'`, `'fatal'`)
  </Step>

  <Step title="Render diagrams">
    For automatic rendering, Mermaid will find all elements with the class `mermaid` and render them:

    ```javascript app.js theme={null}
    // This happens automatically when startOnLoad is true
    await mermaid.run();
    ```

    For manual rendering of a specific diagram:

    ```javascript app.js theme={null}
    const element = document.querySelector('#myDiagram');
    const graphDefinition = `
      graph TB
        A[Start] --> B[Process]
        B --> C[End]
    `;

    const { svg, bindFunctions } = await mermaid.render('graphDiv', graphDefinition);
    element.innerHTML = svg;

    // Bind any interactive functions (e.g., click handlers)
    if (bindFunctions) {
      bindFunctions(element);
    }
    ```
  </Step>

  <Step title="View your diagram">
    Open your HTML file in a browser. You should see your diagram rendered!
  </Step>
</Steps>

## Example: Complete application

Here's a complete example showing different ways to use Mermaid:

```html complete-example.html theme={null}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Mermaid Examples</title>
</head>
<body>
  <h1>Mermaid Diagram Examples</h1>
  
  <!-- Automatic rendering with class="mermaid" -->
  <h2>Sequence Diagram</h2>
  <div class="mermaid">
    sequenceDiagram
      Alice->>John: Hello John, how are you?
      loop HealthCheck
        John->>John: Fight against hypochondria
      end
      Note right of John: Rational thoughts!
      John-->>Alice: Great!
      John->>Bob: How about you?
      Bob-->>John: Jolly good!
  </div>
  
  <!-- Manual rendering -->
  <h2>Flowchart</h2>
  <div id="manualDiagram"></div>

  <script type="module">
    import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';
    
    // Initialize mermaid
    mermaid.initialize({
      startOnLoad: true,
      theme: 'default'
    });
    
    // Manual rendering example
    const diagram = `
      flowchart LR
        A[Hard] -->|Text| B(Round)
        B --> C{Decision}
        C -->|One| D[Result 1]
        C -->|Two| E[Result 2]
    `;
    
    const { svg } = await mermaid.render('manualId', diagram);
    document.getElementById('manualDiagram').innerHTML = svg;
  </script>
</body>
</html>
```

## Common patterns

### Parsing diagrams

Validate diagram syntax without rendering:

```javascript theme={null}
const diagramText = 'graph TD\n  A-->B';

try {
  const result = await mermaid.parse(diagramText);
  console.log('Valid diagram:', result.diagramType);
  // Output: { diagramType: 'flowchart-v2' }
} catch (error) {
  console.error('Invalid diagram:', error);
}
```

### Error handling

Handle rendering errors gracefully:

```javascript theme={null}
await mermaid.run({
  querySelector: '.mermaid',
  suppressErrors: true, // Don't throw errors, just log them
  postRenderCallback: (id) => {
    console.log('Rendered diagram:', id);
  }
});
```

### Detecting diagram types

```javascript theme={null}
const diagramText = 'sequenceDiagram\n  Alice->>Bob: Hello';
const type = await mermaid.detectType(diagramText);
console.log(type); // 'sequence'
```

## Next steps

Now that you've created your first diagram, explore more advanced topics:

<CardGroup cols={2}>
  <Card title="Diagram syntax" icon="book">
    Learn the syntax for different diagram types
  </Card>

  <Card title="Configuration" icon="gear">
    Customize themes, styling, and behavior
  </Card>

  <Card title="API reference" icon="code">
    Explore the complete Mermaid API
  </Card>

  <Card title="Examples" icon="lightbulb">
    Browse real-world diagram examples
  </Card>
</CardGroup>

<Tip>
  Try the [Mermaid Live Editor](https://mermaid.live/) to experiment with different diagram types and see the results in real-time.
</Tip>
