Domphy

Sequence Diagrams

Sequence diagrams show how components communicate over time. Each actor is laid out as a vertical lifeline; messages pass between lifelines horizontally.

Basic syntax

<div class="dp-mermaid language-mermaid">sequenceDiagram Alice-&gt;&gt;Bob: Hello! Bob--&gt;&gt;Alice: Hi there Alice-&gt;&gt;Bob: How are you? Bob--&gt;&gt;Alice: All good, thanks.</div>

Participants

Declare participants explicitly to fix their order and give them readable labels:

<div class="dp-mermaid language-mermaid">sequenceDiagram participant B as Browser participant S as API Server participant D as Database B-&gt;&gt;S: GET /users/42 S-&gt;&gt;D: SELECT * FROM users WHERE id=42 D--&gt;&gt;S: user row S--&gt;&gt;B: 200 { id: 42, name: &quot;Alice&quot; }</div>

Use actor instead of participant to render a stick-person icon:

<div class="dp-mermaid language-mermaid">sequenceDiagram actor User participant App User-&gt;&gt;App: Click &quot;Sign in&quot; App--&gt;&gt;User: Show login form</div>

Message arrow types

<div class="dp-mermaid language-mermaid">sequenceDiagram A-&gt;&gt;B: Solid arrowhead (sync request) A--&gt;&gt;B: Dotted arrowhead (sync response) A-&gt;B: Solid, no arrowhead A--&gt;B: Dotted, no arrowhead A-xB: Solid with cross (lost message) A--xB: Dotted with cross A-)B: Async (open arrowhead) A--)B: Async dotted</div>
SyntaxMeaning
->>Synchronous call (solid arrowhead)
-->>Synchronous response (dotted arrowhead)
->Solid, no arrowhead
-->Dotted, no arrowhead
-xLost / dropped message
--xLost / dropped dotted
-)Asynchronous (open arrowhead)
--)Asynchronous dotted

Activation boxes

Activation boxes indicate when a participant is actively processing. Use the +/- shorthand directly on the message:

<div class="dp-mermaid language-mermaid">sequenceDiagram Browser-&gt;&gt;+Server: POST /login Server-&gt;&gt;+DB: SELECT user DB--&gt;&gt;-Server: user row Server--&gt;&gt;-Browser: 200 { token }</div>

Or use explicit activate / deactivate:

<div class="dp-mermaid language-mermaid">sequenceDiagram A-&gt;&gt;B: Request activate B B-&gt;&gt;C: Forward activate C C--&gt;&gt;B: Result deactivate C B--&gt;&gt;A: Response deactivate B</div>

Nested +/- on the same participant creates stacked boxes to show re-entrant calls:

<div class="dp-mermaid language-mermaid">sequenceDiagram A-&gt;&gt;+B: Call 1 A-&gt;&gt;+B: Call 2 B--&gt;&gt;-A: Reply 2 B--&gt;&gt;-A: Reply 1</div>

Notes

Annotate the diagram with free-form notes:

<div class="dp-mermaid language-mermaid">sequenceDiagram participant A participant B Note right of A: Waiting for user input Note left of B: Validates on every request A-&gt;&gt;B: Submit Note over A,B: Both sides log this event</div>
SyntaxPosition
Note right of XRight of X
Note left of XLeft of X
Note over XCentred above X
Note over X,YSpanning X to Y

Loop

Repeat a block of messages:

<div class="dp-mermaid language-mermaid">sequenceDiagram Client-&gt;&gt;Server: Subscribe to events loop Every 30 seconds Server-&gt;&gt;Client: Heartbeat ping Client--&gt;&gt;Server: Pong end Client-&gt;&gt;Server: Unsubscribe</div>

Alt / else

Model branching flows — alt with one or more else clauses:

<div class="dp-mermaid language-mermaid">sequenceDiagram Browser-&gt;&gt;API: GET /resource alt Found API--&gt;&gt;Browser: 200 { data } else Not found API--&gt;&gt;Browser: 404 Not Found else Server error API--&gt;&gt;Browser: 500 Internal Error end</div>

Opt

An optional block (shown only when the condition holds):

<div class="dp-mermaid language-mermaid">sequenceDiagram User-&gt;&gt;App: Submit form App-&gt;&gt;Validator: Validate fields opt Invalid input Validator--&gt;&gt;App: Validation errors App--&gt;&gt;User: Show error messages end App-&gt;&gt;DB: Persist record DB--&gt;&gt;App: OK App--&gt;&gt;User: Success</div>

Par

Show parallel concurrent activity:

<div class="dp-mermaid language-mermaid">sequenceDiagram Server-&gt;&gt;Server: Receive request par Notify user Server-&gt;&gt;Email: Send confirmation and Log event Server-&gt;&gt;Logger: Write audit log and Update metrics Server-&gt;&gt;Metrics: Increment counter end Server--&gt;&gt;Client: 200 OK</div>

Critical

A critical block marks a section that must succeed, with option branches for alternative outcomes:

<div class="dp-mermaid language-mermaid">sequenceDiagram Client-&gt;&gt;Server: Open connection critical Establish TLS Server-&gt;&gt;CA: Verify certificate CA--&gt;&gt;Server: Certificate valid option Timeout Server--&gt;&gt;Client: 503 Unavailable option Invalid cert Server--&gt;&gt;Client: 495 SSL Error end</div>

Break

Model an early exit from a sequence:

<div class="dp-mermaid language-mermaid">sequenceDiagram User-&gt;&gt;App: Upload file break File too large App--&gt;&gt;User: 413 Payload Too Large end App-&gt;&gt;Storage: Store file Storage--&gt;&gt;App: URL App--&gt;&gt;User: 200 { url }</div>

Autonumber

Prefix every message with an incrementing sequence number:

<div class="dp-mermaid language-mermaid">sequenceDiagram autonumber Browser-&gt;&gt;Server: GET /api/data Server-&gt;&gt;DB: Query DB--&gt;&gt;Server: Rows Server--&gt;&gt;Browser: JSON response</div>

Box grouping

Group participants in a colored background box:

<div class="dp-mermaid language-mermaid">sequenceDiagram box &quot;Front-end&quot; #e0f2fe participant UI participant Store end box &quot;Back-end&quot; participant API participant DB end UI-&gt;&gt;API: Fetch API-&gt;&gt;DB: Query DB--&gt;&gt;API: Rows API--&gt;&gt;Store: Update state Store--&gt;&gt;UI: Re-render</div>

Rendering with @domphy/mermaid

Build-time:

import { renderMermaidToSvg } from "@domphy/mermaid"

const svg = await renderMermaidToSvg(`sequenceDiagram
  autonumber
  actor User
  participant App
  participant Auth

  User->>App: Sign in
  App->>+Auth: Verify credentials
  Auth-->>-App: JWT token
  App-->>User: Redirect to /dashboard`, { theme: "default" })

Client-side (for dynamic or user-authored diagrams):

import { mermaidClient } from "@domphy/mermaid"

const SequenceView = {
  pre: [{ code: `sequenceDiagram
  A->>B: Hello
  B-->>A: Hi` }],
  $: [mermaidClient({ theme: "neutral" })],
}