Mermaid diagrams

Code in, diagrams out

Write diagrams the way you write code. Describe flows, systems, and timelines in plain text, keep them in version control, and render them into structured visuals. Or let your AI agent do it, with MCP and the Excalidraw CLI.

A class diagram created in Excalidraw from Mermaid code.

What is a Mermaid diagram?

A Mermaid diagram is a chart or diagram written in plain text with Mermaid, an open-source, JavaScript-based diagramming tool. You describe nodes, connections, and labels in a few lines of code, and Mermaid turns them into a flowchart, sequence diagram, ER diagram, or another visual.

Mermaid is widely supported: GitHub, GitLab, and many documentation tools render it in Markdown. Because diagrams are text-based, they live next to the code, and changes are easier to include and track in pull requests. Because the source is plain, structured text, AI and automation tools read and write it easily.

Mermaid diagram types

Mermaid supports a long list of diagram types, from flowcharts to mind maps and pie charts. Here are five common ones, and some tips on how to create them.

A flowchart of a sign-up process generated with Mermaid code in Excalidraw.

Flowcharts

Flowcharts show steps, decisions, and the paths between them. Use them for onboarding flows, CI/CD pipelines, error handling, or any process with more than one possible outcome.

How to create flowcharts in Mermaid?

Start with flowchart plus a direction, like TD (top-down) or LR (left to right), and connect nodes with arrows. Brackets set the node shape: [ ] for a rectangle, { } for a decision, and ( ) for rounded edges.

Our example:

1flowchart LR
2 A[Sign up] --> B{Email verified?}
3 B -- Yes --> C[Dashboard]
4 B -- No --> D[Resend email]

A sequence diagram for a user login process with colored message blocks, generated with Mermaid code in Excalidraw.

Sequence diagrams

Sequence diagrams show how participants exchange messages over time. As such, they work best for any scenario where the order of events matters, like API calls or authentication flows.

How to create sequence diagrams in Mermaid?

Specify the diagram type, declare the participants, and write each message as an arrow: ->> for a request and -->> for a dashed reply. Mermaid draws the lifelines top to bottom, in the order you write the messages.

Our example:

1sequenceDiagram
2 Client->>API: POST /login
3 API->>DB: Find user
4 DB-->>API: User record
5 API-->>Client: 200 OK

A class diagram of an Animal class and a Dog subclass generated with Mermaid code in Excalidraw.

Class diagrams

Class diagrams describe the structure of a system: classes, their attributes, methods, and relationships. Use class diagrams to plan a new module, explain a codebase to new teammates, or spot tangled dependencies (before you spend the whole weekend refactoring).

How to create class diagrams in Mermaid?

Declare the type and use symbols like <|-- for inheritance and *-- for composition. Visibility markers like + (public) and - (private) go right before attribute and method names.

Our example:

1classDiagram
2 Animal <|-- Dog
3 Animal : +String name
4 Animal : +speak()
5 Dog : +fetch()

A state diagram of an order moving from pending to paid to shipped, generated with Mermaid code in Excalidraw.

State diagrams

State diagrams map the states an object moves through and the events that trigger each change, like order status or user sessions. If a state has no way out, or two events lead to the same place, you see it at a glance, which makes state diagrams great for spotting edge cases.

How to create state diagrams in Mermaid?

Specify the diagram type and use [*] to mark where the flow starts and ends. --> arrows mark transitions between states. Add notes to explain why a transition happens or what it does.

Our example:

1stateDiagram-v2
2 [*] --> Pending
3 Pending --> Paid : payment ok
4 Paid --> Shipped
5 Shipped --> [*]

An entity relationship diagram of customer, order, and order item tables generated with Mermaid code in Excalidraw.

Entity relationship diagrams

Entity relationship diagrams (ERDs) show the tables in a database and how they connect. Use them to plan a schema, review a migration, or explain a data model before anyone writes a JOIN.

How to create entity relationship diagrams in Mermaid?

Declare the type, name two entities, and link them with crow's foot symbols: || means exactly one, o{ means zero or more, and |{ means one or more. Add a label after a colon. Add attributes in curly braces, one type and name per line, and mark keys with PK or FK.

Our example:

1erDiagram
2 CUSTOMER ||--o{ ORDER : places
3 ORDER ||--|{ ORDER_ITEM : contains
4 CUSTOMER {
5 int id PK
6 string email
7 }

How to add colors to Mermaid diagrams

When you convert code with Mermaid to Excalidraw, diagrams are monochrome by default. To change stroke and background colors, you need to include code lines that are specific to different diagram types.

Mermaid code example

Description and supported diagrams

classDef blue fill:#a5d8ff,stroke:#4a9eed

Defines a color style named blue. Works in flowcharts, state diagrams, entity-relationship diagrams, and class diagrams.

A:::blue

Applies the blue style to A. Works in flowcharts, state diagrams, and class diagrams.

style A fill:#a5d8ff

Colors one node directly, without a named style. Works in flowcharts and class diagrams.

rect rgb(230, 240, 255) … end

Adds a background color behind the messages inside the block. Works in sequence diagrams.

%%{init: {'theme': 'forest'}}%%

Sets a color theme for the whole diagram (default, neutral, dark, forest, or base). Excalidraw's Mermaid conversion currently doesn't support themes.

How can specific professions use Mermaid diagrams?

Tips and tricks for creating Mermaid diagrams

  1. Best practices

    • Keep one idea per diagram: Don't try to cram a whole system into one diagram. Split it into smaller diagrams, one per flow or component.
    • Label components: Use short, clear node IDs and labels. They make diagrams easier to read and maintain.
    • Update visuals and the code: Once you change a diagram by hand, the Mermaid source won't match. Decide which one is your source of truth and keep both up to date.
    • Store Mermaid diagrams in your repo: When the diagram sits next to the code it describes, it's easier to review and update both in the same pull request.
    • Build diagrams from the terminal: Convert your .mmd files into editable Excalidraw scenes or SVG and PNG images with the Excalidraw CLI. Rerun the command, or add it to a script, whenever the Mermaid source changes, so your docs always show the latest version.
    • Use a simple color code: Highlight key elements of the diagram. Avoid decorating to keep it clear.
  2. How to create Mermaid diagrams in Excalidraw

    With Text to diagram

    Quickly generate simple Mermaid diagrams and code from prompts. Built into Excalidraw, no setup needed.

    • Click "More tools" in the toolbar and then "Text to diagram".
    • Be specific; define the type of diagram, its purpose, and details like labels.
    • You can view your AI-generated diagram as Mermaid code.
    • Here's an example of a prompt and a diagram generated: "Create a top-down flowchart of a pull request process. Start with "Open pull request", then a decision: "Checks pass?" If yes, go to "Code review", then "Merge". If no, go to "Fix and push", then loop back to the checks."

    An example of a flowchart for a pull request process generated in Excalidraw.

    With Mermaid to Excalidraw

    Turn Mermaid source into Excalidraw diagrams. Great when you know how to write and edit the code.

    • Click "More tools" in the toolbar and then "Mermaid to Excalidraw".
    • Type in Mermaid code to generate the diagram. If you need help writing the code, click the link in the hint above the code box to open the Mermaid syntax tutorial.
    • You can also paste the code onto the canvas.
    • The following Mermaid code produces an identical flowchart to the one generated with the prompt:
    1flowchart TD
    2 A[Open pull request] --> B{Checks pass?}
    3 B -- Yes --> C[Code review]
    4 B -- No --> D[Fix and push]
    5 D --> B
    6 C --> E[Merge]

    An example of a flowchart for a pull request process generated in Excalidraw.

    With the Excalidraw CLI

    Convert the Mermaid files in your repo from the terminal, or automate diagram workflows with scripts. No account or subscription needed.

    • Install the CLI with npm install -g @excalidraw/cli ( requires Node.js 22 or newer), or try it first with npx @excalidraw/cli --help.
    • Save your Mermaid code as a .mmd file, or use one that's already in your repo.
    • Convert it to an editable Excalidraw scene: excalidraw mermaid <file>.mmd --out <file>.excalidraw.
    • Need an image for your docs? Change the output to SVG or PNG: excalidraw mermaid <file>.mmd --out <file>.svg or --out <file>.png.
    • Open the .excalidraw file in Excalidraw to adjust the layout by hand.

    For example, excalidraw mermaid docs/diagrams/checkout.mmd --out checkout.excalidraw converts the checkout diagram from your repo and saves it as checkout.excalidraw, a file you open in Excalidraw as editable shapes and arrows.

    Read the CLI docs →

How to create Mermaid diagrams with MCP

Connect Excalidraw+ to an LLM through MCP to generate diagrams and source code using natural language. Add custom styling, create any diagram type, and connect local files and code repos for context.

Coding agents can also work with Mermaid in Excalidraw CLI: they run its commands in the terminal to convert Mermaid files, render images, and update scenes in your workspace.

Where to start?

  • ▶️ YouTube tutorial

    Connect Excalidraw+ to your preferred LLM. Plus a few ideas to get you started.

    Watch the tutorial →

  • 📄 MCP documentation

    Set up MCP/API keys, authentication, permissions, and tools. Go beyond the basic integration.

    Read the docs →

  • ⌨️ CLI documentation

    Enable your agent to convert, render, and sync diagrams from the terminal.

    Read the docs →

Wrapping up

Mermaid turns diagrams into code: easy to write, review, and keep up to date. Flowcharts, sequence, class, state, and ER diagrams go from text to visuals in seconds, and popular coding tools render them anywhere.

Want more control over the layout? Paste your code into Excalidraw or convert it with the Excalidraw CLI, adjust the result by hand, and share it with your team.

Business use or teamwork? Try Plus features

💬 Comments: Share feedback on specific parts of a diagram.

🎙️ Voice Hangouts and screen sharing: Walk your team through a flow without leaving the canvas.

📺 Presentations: Turn frames into slides for a design review, ready in seconds.

📂 Work organization: Keep Mermaid diagrams in collections, with team workspaces and access control, managed through the CLI.

💡 More AI iterations: Generate and refine more diagrams with Text to diagram.