Home / Examples / Sequence Diagram

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 dashboard

Payment 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
  end

Retry 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 queue

Parallel 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
  end

Sequence Diagram syntax cheat sheet

SyntaxMeaning
participant A as AliasDeclare a participant with a display name
actor UserStick-figure participant
A->>B: textSolid arrow (request)
A-->>B: textDotted arrow (response)
A-xB / A--xBArrow ending in a cross (failure)
A->>+B / B-->>-AStart / end an activation bar
alt ... else ... endAlternative branches
loop label ... endRepeated messages
par ... and ... endParallel messages
Note over A,B: textNote spanning participants
autonumberNumber every message

Common sequence diagram mistakes

  • Every alt, loop, par, and opt block needs a closing end.
  • 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?
Add autonumber on the line after sequenceDiagram. Mermaid then numbers every message in order.
How do I group participants into a box?
Wrap their declarations in box Name ... end. You can also give the box a color, for example box Aqua Backend.

Keep going

More Mermaid examples

Last updated 2026-11-28.