Mermaid sequence diagram syntax: messages, loops and notes

Write Mermaid sequence diagrams in Markdown: participants and actors, message arrows, activations, notes, loops and alt blocks, and common errors.

The smallest sequence diagram

A sequence diagram shows who sends what to whom, in time order from top to bottom. In Markdown it is Mermaid code in a fenced block named mermaid:

Mermaid
sequenceDiagram
  Browser->>Server: GET /profile
  Server->>Database: Find user 42
  Database-->>Server: User record
  Server-->>Browser: 200 OK, profile page

Each line is one message: sender, arrow, receiver, a colon and the message text. Participants appear in the order they are first mentioned.

Participants and actors

Declare participants to control their order and give them short IDs with longer names:

Mermaid
sequenceDiagram
  actor U as User
  participant App as Mobile app
  participant API as Payments API
  U->>App: Tap Pay
  App->>API: Create payment
  • participant draws a box; actor draws a stick figure, useful for people.
  • as sets the displayed name, so messages can use the short ID.
  • Declared participants appear in the order you declare them, whatever order they first speak.

To group participants, wrap them in a box: box Internal services, the participant lines, then end.

Message arrows

ArrowLineTypical meaning
->>solid, arrowheadrequest, call
-->>dotted, arrowheadresponse, return
->solid, no arrowheadplain message
-->dotted, no arrowheadplain reply
-xsolid, cross at the endfailed or lost message
--xdotted, cross at the endfailed reply
-)solid, open arrowheadasynchronous message
--)dotted, open arrowheadasynchronous reply

A participant can send a message to itself: Server->>Server: Validate token.

Activations

An activation bar shows that a participant is busy handling a request. Add + after the arrow to start it and - to end it:

Mermaid
sequenceDiagram
  Client->>+Server: Upload file
  Server->>+Scanner: Check for malware
  Scanner-->>-Server: Clean
  Server-->>-Client: Upload complete

The long form, activate Server and deactivate Server on their own lines, does the same.

Notes

Mermaid
sequenceDiagram
  participant A as Client
  participant B as Server
  Note right of A: Token stored in memory
  A->>B: Request with token
  Note over A,B: TLS connection

Notes go left of, right of or over one participant, or over two participants separated by a comma to span both.

Loops, conditions and parallel steps

Blocks wrap a group of messages and end with end:

Mermaid
sequenceDiagram
  participant App
  participant API
  loop Every 30 seconds
    App->>API: Poll for status
  end
  alt Payment approved
    API-->>App: Approved
  else Payment declined
    API-->>App: Declined
  end
  opt Receipt requested
    App->>API: Send receipt
  end
BlockUse
looprepeated messages
alt … else … endeither/or cases
optan optional step
par … and … endsteps that happen at the same time
critical … option … endan action that must happen, with fallback cases
breaka step that stops the flow, such as an error
rect rgb(240, 240, 255)a coloured background behind a group

Blocks can be nested, but indentation is only for people: every block needs its own end.

Numbering and comments

Add autonumber on its own line after sequenceDiagram to number every message, which makes a long diagram easy to discuss ("step 7 fails"). Lines starting with %% are comments and are ignored.

Mistakes that stop a diagram rendering

  • A missing end. Every loop, alt, opt, par, critical, break, rect and box needs one. A missing end usually shows as an error on the last line.
  • The word end in a message. It can be read as the keyword. Rephrase it or wrap the word in brackets or quotes.
  • A semicolon in the text. Semicolons can end a statement. Write #59; for a literal semicolon.
  • A missing colon. A->>B Hello is invalid; the message text needs a colon: A->>B: Hello.
  • A typo in a participant ID. Sever->>Client silently creates a new participant called Sever.

If the diagram shows as code instead of a picture, see Mermaid diagram not rendering. For processes and decisions rather than conversations, use a flowchart. The official sequence diagram documentation has every option.

Frequently asked questions

How do I make a sequence diagram in Markdown?

Write Mermaid code in a fenced code block named mermaid. Start with sequenceDiagram, then one message per line, such as Alice->>Bob: Hello. GitHub, GitLab, Obsidian and many Markdown apps render it as a diagram.

What is the difference between ->> and -->> in Mermaid?

->> draws a solid line with an arrowhead, usually a request or call. -->> draws a dotted line with an arrowhead, usually a response or return value.

How do I show an if/else in a Mermaid sequence diagram?

Use an alt block: alt condition, the messages for that case, else other condition, the other messages, and end. Use opt for a single optional case.

How do I number the messages in a Mermaid sequence diagram?

Add autonumber on its own line after sequenceDiagram. Every message then gets a number in order.

How do I show a person instead of a box?

Declare the participant with actor instead of participant: actor U as User. Mermaid draws a stick figure for actors.