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:
sequenceDiagram
Browser->>Server: GET /profile
Server->>Database: Find user 42
Database-->>Server: User record
Server-->>Browser: 200 OK, profile pageEach 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:
sequenceDiagram
actor U as User
participant App as Mobile app
participant API as Payments API
U->>App: Tap Pay
App->>API: Create paymentparticipantdraws a box;actordraws a stick figure, useful for people.assets 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
| Arrow | Line | Typical meaning |
|---|---|---|
->> | solid, arrowhead | request, call |
-->> | dotted, arrowhead | response, return |
-> | solid, no arrowhead | plain message |
--> | dotted, no arrowhead | plain reply |
-x | solid, cross at the end | failed or lost message |
--x | dotted, cross at the end | failed reply |
-) | solid, open arrowhead | asynchronous message |
--) | dotted, open arrowhead | asynchronous 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:
sequenceDiagram
Client->>+Server: Upload file
Server->>+Scanner: Check for malware
Scanner-->>-Server: Clean
Server-->>-Client: Upload completeThe long form, activate Server and deactivate Server on their own lines, does the same.
Notes
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 connectionNotes 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:
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| Block | Use |
|---|---|
loop | repeated messages |
alt … else … end | either/or cases |
opt | an optional step |
par … and … end | steps that happen at the same time |
critical … option … end | an action that must happen, with fallback cases |
break | a 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,rectandboxneeds one. A missingendusually 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 Hellois invalid; the message text needs a colon:A->>B: Hello. - A typo in a participant ID.
Sever->>Clientsilently 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.