Mermaid Sequence Diagram Examples
Sequence diagrams show who talks to whom, and in what order. They are the best choice for API calls, authentication handshakes, and anything where timing matters.
A sequence diagram starts with sequenceDiagram. Declare participants first to control their order, then write one message per line.
Login with an identity provider
The + and - after an arrow turn activation bars on and off, which makes it clear which service is busy while it waits for a response.
sequenceDiagram
actor User
participant Browser
participant API
participant IdP as Identity Provider
User->>Browser: Submit login form
Browser->>+API: POST /login
API->>+IdP: Validate credentials
IdP-->>-API: ID token
API-->>-Browser: Set session cookie
Browser-->>User: Show dashboardPayment with alt/else branches
alt and else show mutually exclusive outcomes. Use it for approved and declined responses, cache hits and misses, or feature-flag paths.
sequenceDiagram
participant C as Checkout
participant P as Payment API
participant B as Bank
C->>P: Create charge
P->>B: Authorize card
alt Approved
B-->>P: Approved
P-->>C: 200 OK
else Declined
B-->>P: Declined
P-->>C: 402 Payment Required
endRetry loop with a note
loop wraps repeated messages. The --x arrow marks a failed call, and Note over adds context that spans several participants.
sequenceDiagram
participant W as Worker
participant Q as Queue
participant S as Email service
W->>Q: Pull job
Q-->>W: send_welcome_email
loop Up to 3 attempts
W->>S: Send email
S--xW: Timeout
end
Note over W,S: Exponential backoff between attempts
W->>Q: Move job to dead-letter queueParallel requests with par
par and and show calls that happen at the same time, such as a backend-for-frontend fetching stock and price in parallel.
sequenceDiagram
participant API
participant Inventory
participant Pricing
API->>API: Build product page
par Fetch stock
API->>Inventory: GET /stock/42
Inventory-->>API: 12 in stock
and Fetch price
API->>Pricing: GET /price/42
Pricing-->>API: 19.00 USD
endSequence Diagram syntax cheat sheet
| Syntax | Meaning |
|---|---|
participant A as Alias | Declare a participant with a display name |
actor User | Stick-figure participant |
A->>B: text | Solid arrow (request) |
A-->>B: text | Dotted arrow (response) |
A-xB / A--xB | Arrow ending in a cross (failure) |
A->>+B / B-->>-A | Start / end an activation bar |
alt ... else ... end | Alternative branches |
loop label ... end | Repeated messages |
par ... and ... end | Parallel messages |
Note over A,B: text | Note spanning participants |
autonumber | Number every message |
Common sequence diagram mistakes
- Every
alt,loop,par, andoptblock needs a closingend. - Activations must balance: each
+needs a matching-on the same participant, or Mermaid reports an inactive participant error. - Semicolons in message text end the statement. Use
#59;if you need a literal semicolon.
Sequence Diagram FAQ
How do I number the messages?
autonumber on the line after sequenceDiagram. Mermaid then numbers every message in order.How do I group participants into a box?
box Name ... end. You can also give the box a color, for example box Aqua Backend.Keep going
- Mermaid sequence diagrams for API design
- Open the free Mermaid editor to write your own diagram with live preview.
More Mermaid examples
- Mermaid flowchart examples
- Mermaid er diagram examples
- Mermaid class diagram examples
- Mermaid state diagram examples
- Mermaid gantt chart examples
- Mermaid pie chart examples
- Mermaid mindmap examples
Last updated 2026-11-28.