Flowchart code

Write a flowchart in Mermaid's flowchart syntax, the text form most tools and wikis draw.

A flowchart's code is Mermaid's flowchart syntax, the text form most tools, wikis and Git hosts draw. This page shows how to write it.

The Mermaid pane beside the canvas shows the code, and you can edit it there. The chart follows when you pause typing. The assistant and a connected AI app write the same code.

The layout is automatic, so the code only says what the shapes are and how they join. To bring a chart in from a file or from PlantUML, see Import and export.

A first chart

Paste this into the Mermaid pane to see it drawn.

Mermaid
---
title: Password reset
---
flowchart TD
  forgot([Forgot password]) --> found{Account found?}
  found -->|yes| email[Email a reset link]
  found -->|no| same[Show the same message]
  email --> done([Check your email])
  same --> done

Reading it from the top:

  • The three lines between --- give the chart its title.
  • flowchart TD starts the chart, reading top to bottom.
  • forgot([Forgot password]) is a shape. First comes its id, forgot, then its label in the brackets that say its shape.
  • --> joins two shapes, and |yes| labels the arrow.
  • A later line names a shape by its id alone.

The rules that matter

  1. The first line is flowchart and a direction. TD (or TB) reads top to bottom, LR left to right, BT bottom to top and RL right to left. graph works too.
  2. A shape is declared once. Give its brackets and label where it first appears, on its own line or in an arrow. Later lines use its id.
  3. Ids are letters, digits and underscores, such as check_stock. Mermaid's keywords, such as end, graph and subgraph, can't be ids.
  4. One statement per line, or several separated by ;. Indenting is the app's style, not a rule.

Shapes

step[A process]
ask{A decision?}
start([Start or end])
event(An event)
db[(A database)]
sub[[A subroutine]]
io[/Input or output/]
prep{{Preparation}}
link((A connector))

Every other Mermaid shape is written by its name: report@{ shape: doc, label: "Monthly report" }. The names include:

NameShape
doc, docsA document, or several
delayDelay
sl-rectManual input
curv-trapDisplay
lin-cylDisk
triExtract
hourglassCollate
boltCommunication link
braceA comment
sm-circUML start
fr-circUML end
forkUML fork or join bar

The UML start, end and fork bar need no label. Mermaid's longer names, such as shape: database or shape: decision, work too. The reference lists them all.

A Mermaid shape the canvas doesn't draw shows as a process. The code keeps it as written.

Labels

A label with brackets, quotes or other symbols goes in double quotes: plan["Plan (draft)"]. Inside quotes, #quot; is a quote.

<br> breaks a line. So does a line break in the canvas's label field.

Arrows

a --> b
a -->|yes| b
a -- yes --> b
a -.-> b
a ==> b
a --- b
a ~~~ b
a --o b
a --x b
a <--> b
a ---> b
  • --> is an arrow. -->|label| or -- label --> labels it.
  • -.-> is dotted, for an optional or later step. ==> is thick, for the main path.
  • --- is a line with no arrowhead.
  • ~~~ is a hidden spacer that only pulls two shapes together.
  • --o ends in a circle and --x in a cross. <--> has a head at both ends.
  • An extra dash, --->, makes an arrow longer. Its ends sit a step further apart.

Arrows chain and fan out. a --> b --> c is two arrows, and a & b --> c joins both to c. An arrow to a frame's id ends on the frame's edge.

Frames

subgraph warehouse [Warehouse]
  direction LR
  pick[Pick the items] --> pack[Pack the box]
end

subgraph, an id and a title in brackets start a frame. end closes it. The shapes declared inside are in it, and frames nest.

A direction line as a frame's first line says how its own shapes read. Mermaid follows it when no arrow crosses the frame's edge.

Colour

style pay fill:#E1F4E9,stroke:#27AE60
classDef warn fill:#FDEDEC,stroke:#E74C3C
class reject,refund warn
reject:::warn
linkStyle 2 stroke:#E74C3C
  • style colours one shape.
  • classDef names a style, and class or ::: gives it to shapes.
  • linkStyle colours arrows by their number, counting from 0 in the order the code writes them.

The canvas draws a shape's colour the way it draws every colour. The outline takes the colour, with a pale wash of it inside, and the text stays as it is. See Colour.

When you pick a colour on the canvas, the code gets the stroke and a pale fill to match. Other style properties, such as a dashed outline, stay in the code as written.

Comments and what the canvas keeps as written

%% Happy path only for now
click pay "https://wiki.example.com/payments"

A line that starts with %% is a comment. The app keeps it in the code when the chart changes.

Lines the canvas has no use for are kept as written too. These include a click link, accessibility lines and an arrow's animation. Properties shows them under Also in the code when nothing is selected.

Ids

A shape's id is its Mermaid id. The code, comments, the assistant and connected AI apps all name it the same way.

An arrow's id is made from its ends, such as e_check_pay. Mermaid 11's check e1@--> pay names it in the code.

When you edit the code, a shape keeps its id. An arrow keeps its id while it joins the same shapes. That way comments stay on them.

When a line has a mistake

If a line can't be read, the pane's header shows the message with its line number. So does the Problems panel. The chart keeps its last working version until you fix it.

MessageWhat to do
Start with flowchart TD (or LR, BT, RL).Make the first line flowchart and a direction.
This is a Mermaid … , not a flowchart.The text is another kind of Mermaid diagram. Open it as that kind, or start with flowchart TD.
Unknown direction "…". Use TD, LR, BT or RL.Fix the direction.
Can't read "…". Write a node (A[Label]) or an arrow (A --> B).Write the line as a shape or an arrow.
Close "…" with …Add the closing bracket the shape's opening bracket needs.
Close the arrow's label with |.Add the second |.
Say where this arrow goes: A --> B.Name the shape after the arrow.
"…" is a Mermaid keyword, so it can't name a node.Use another id.
Give the subgraph a name: subgraph id [Title].Add an id after subgraph.
There's already a subgraph "…".Give the second frame another id.
This end has no subgraph to close.Remove the extra end.
Close the subgraph "…" with end.Add end after the frame's last line.
"…" names both a subgraph and a node.Give one of them another id.
There's no node or subgraph "…" to style.Fix the id in the style or class line.
There's no arrow … to style.Fix the number in linkStyle. Arrows count from 0.
Front matter (---, then title: …, then ---) goes on the first lines …Move the title lines to the top.

Full reference

The flowchart code reference has the complete syntax on one page, exactly as the assistant gets it. It includes the drawing style and the patterns the assistant follows.

On this page