Mermaid reference

Mermaid sequence diagrams

A sequence diagram shows who talks to whom, in order: a user, an app, an API, a queue. Here is the full syntax with examples you can edit, plus the details that break diagrams: colons, semicolons and the word “end”.

Updated

Polling loop, optional step and parallel work Edit and export
sequenceDiagram
  participant W as Web app
  participant A as API
  participant Q as Queue
  W->>A: GET /report/42
  loop every 5 s until ready
    W->>A: GET /report/42/status
    A-->>W: pending
  end
  opt user ticked "email me"
    A-)Q: send email job
  end
  par
    A->>Q: log download
  and
    A-->>W: report.pdf
  end
Boxes, activation and notes Edit and export
sequenceDiagram
  box Customer side
    actor C as Customer
    participant M as Mobile app
  end
  box Our backend
    participant API
    participant DB as Database
  end
  C->>M: Open order history
  M->>API: GET /orders
  activate API
  API->>DB: select orders
  DB-->>API: 12 rows
  API-->>M: JSON list
  deactivate API
  Note right of C: Shown in under a second
  Note over API,DB: Same data center

Participants and messages

sequenceDiagram
  actor U as User
  participant API
  U->>API: GET /orders
  API-->>U: 200 OK
  • Participants appear left to right in the order you declare them (or first use them).
  • as sets the display name: participant DB as Orders database. The short id keeps message lines readable.
  • The colon is required: A->>B: text. Without it the parser stops with “Expecting 'TXT', got 'NEWLINE'”.
ArrowLineUse it for
->>Solid, arrowheadRequest, call
-->>Dotted, arrowheadReply, return value
-> / -->Solid / dotted, no arrowheadNeutral exchange
-) / --)Open arrowheadAsynchronous message, event
-x / --xCross at the endFailed or rejected message
<<->>Arrowheads at both endsTwo-way sync

Blocks: loop, alt, opt, par, critical, break

BlockMeaningSyntax
loopRepeatloop every 5 s … end
alt / elseEither/oralt paid … else unpaid … end
optOnly ifopt user opted in … end
par / andAt the same timepar … and … end
critical / optionMust happen, with fallbackscritical charge card … option timeout … end
breakStops the flowbreak out of stock … end
rect rgb(…)Highlight a regionrect rgb(230, 242, 251) … end

Every block closes with end. Blocks nest: a loop inside an alt branch is fine, as long as each one has its own end.

Activation, notes, boxes and numbering

  • Activation draws a thin bar while a participant is busy: A->>+B: call opens it, B-->>-A: reply closes it. activate B / deactivate B on their own lines do the same.
  • Notes: Note right of A: text, Note left of A: text, or Note over A,B: text across two lifelines. Use <br/> for a line break.
  • Boxes group participants under a title: box Our backend … end around their declarations. A color can come first: box rgb(230,242,251) Front.
  • autonumber on its own line numbers every message, handy when the diagram is discussed in a review.

Characters that break a message

In the message textResultDo this
;Parse error: ; ends the statementUse a comma or a dash
A missing colonParse errorA->>B: text
The word end inside the textFine (it's only a keyword at the start of a line)Nothing

Describe the exchange in words

Write who calls whom as plain notes. Arrowly draws the sequence diagram, requests and replies in order, with Mermaid code you can tweak. 3 free diagrams a day.

Draw a sequence diagram

Questions

How do I show a database call inside a request?

Activate the API when the request arrives (A->>+API), draw the database call and its reply, then close with the API's reply (API-->>-A). The activation bar shows the nesting.

Can a participant appear later in the diagram?

Yes: put “create participant Name” just before the first message to it. “destroy Name” removes it after its last message.

How do I make the diagram less wide?

Fewer participants is the only real lever: merge the ones that don't matter to the reader (for example 'Backend' instead of three services).