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
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
endsequenceDiagram
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 centerParticipants 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).
assets 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'”.
| Arrow | Line | Use it for |
|---|---|---|
->> | Solid, arrowhead | Request, call |
-->> | Dotted, arrowhead | Reply, return value |
-> / --> | Solid / dotted, no arrowhead | Neutral exchange |
-) / --) | Open arrowhead | Asynchronous message, event |
-x / --x | Cross at the end | Failed or rejected message |
<<->> | Arrowheads at both ends | Two-way sync |
Blocks: loop, alt, opt, par, critical, break
| Block | Meaning | Syntax |
|---|---|---|
loop | Repeat | loop every 5 s … end |
alt / else | Either/or | alt paid … else unpaid … end |
opt | Only if | opt user opted in … end |
par / and | At the same time | par … and … end |
critical / option | Must happen, with fallbacks | critical charge card … option timeout … end |
break | Stops the flow | break out of stock … end |
rect rgb(…) | Highlight a region | rect 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: callopens it,B-->>-A: replycloses it.activate B/deactivate Bon their own lines do the same. - Notes:
Note right of A: text,Note left of A: text, orNote over A,B: textacross two lifelines. Use<br/>for a line break. - Boxes group participants under a title:
box Our backend…endaround 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 text | Result | Do this |
|---|---|---|
; | Parse error: ; ends the statement | Use a comma or a dash |
| A missing colon | Parse error | A->>B: text |
The word end inside the text | Fine (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 diagramQuestions
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).