Sequence code

Write a sequence diagram as text, one message per line, and bring diagrams in from other tools or take them out.

Sequence code is the text form of a sequence diagram. The Sequence code pane beside the canvas shows it, and you can edit it there: the diagram follows when you pause typing. The assistant and a connected AI app write the same code, so knowing it helps you read what they draft. The layout is automatic, so the code only says who takes part and what they send.

A first diagram

Paste this into the Sequence code pane to see it drawn.

Sequence code
title Password reset
actor User
participant App
database Users DB as DB

User->App: Forgot password
App->DB: Find user by email
alt user found
  DB-->App: User
  App->>User: Email reset link
else no such user
  DB-->App: Nothing
end
App-->User: Check your email

Reading it from the top:

  • title Password reset names the diagram.
  • actor, participant and database declare the columns, left to right. as DB gives a long name a short alias for the lines below.
  • Every other line is a message: who sends it, an arrow, who receives it, then a colon and the text.
  • alt and else start the two branches, and end closes the fragment.

The rules that matter

  1. One element per line, top to bottom. The order of the lines is the order of the messages.
  2. Participants are created as lines name them. The first line that mentions a name adds its column, so you don't have to declare anything. Declare participants at the top to fix their order, set their kind, or give them an alias.
  3. A message needs a colon before its text. App->DB: Find user. The text can be left out: App->DB draws an arrow with no label.
  4. Fragments close with end. Indenting the lines inside by two spaces is the app's style and makes the code easier to read, but the app doesn't need it.
  5. Keywords and names ignore case. ALT works like alt, and app finds the participant called App.

Title and numbering

title Checkout payment
autonumber

title names the diagram, the same name you see at the top of the editor. autonumber, on a line of its own, numbers the messages 1, 2, 3.

Participants

participant Shop
actor Shopper
database "Orders DB" as DB
  • participant draws a box, actor a stick figure for a person, and database a cylinder for a data store.
  • as gives the participant an alias: a short name that later lines use instead of its full name. Lines can still use the full name too.
  • Each participant can be declared once. Declaring it after a message has already named it is fine: the line sets its kind or alias, and the column stays where the first mention put it.
  • Name participants as the reader knows them: "Shopper", "Payments API", "Orders DB".

Messages

Shopper->Shop: Pay for order
Shop-->Shopper: Order placed
Shop->>Queue: Order paid
Worker->Worker: Wait before retrying
ArrowKindDrawn as
->CallSolid line, filled head. The sender waits for an answer.
-->ReplyDashed line. It answers an earlier call.
->>AsyncSolid line, open head. Nobody waits.

A message can go from a participant to itself. In message text, \n starts a new line in the label.

Text after the colon is all message text, so it can hold URLs, paths and //: Client->API: GET https://api.example.com/v1.

Activation bars

Client->API ++: GET /orders/42
API->DB: Load order
DB-->API: Order
API-->Client --: 200 OK
  • ++ after the receiver starts the receiver's bar.
  • -- after the receiver ends the sender's bar.
  • Both can go on one message: ++ --.

You can also start or end a bar on a line of its own, with activate App and deactivate App. Ending a bar on a participant that isn't active is a warning in the Problems panel.

Fragments

alt card accepted
  Payments-->Shop: Payment confirmed
else card declined
  Payments-->Shop: Declined
end

par
  Shop->>Email: Send receipt
and
  Shop->>Warehouse: Pick order
end

loop up to 3 times
  Worker->Receiver: POST /webhooks
end

A fragment starts with its kind and an optional condition, and closes with end. The kinds are alt, opt, loop, par, break, critical and group. See Fragments for what each means.

  • In an alt, each branch after the first starts with else and its condition.
  • In a par, each branch after the first starts with and.
  • The other kinds have one branch.
  • Fragments can sit inside other fragments.

Notes

note over App: Token cached for 5 minutes
note over App, Auth server: PKCE checked on every request
note left of User: On a phone
note right of Receiver
  Signed with HMAC-SHA256
  Replays rejected after 5 minutes
end note
  • note over takes one participant, or several separated by commas, and spans them.
  • note left of and note right of take one participant.
  • For a note of several lines, leave out the colon, write the lines below, and close them with end note.

Dividers

== Delivery ==

A divider is a label between two ==, across the whole diagram. Use one to split a long interaction into phases.

Quotes

Put a name in double quotes when it has characters the code would read otherwise, such as a colon, a comma, an arrow, a quote or //, or when it starts with a word that begins other lines, such as end, note or database.

"Users DB"->API: Query
"Billing: EU"->Ledger: Post entry
participant "Orders \"main\" DB" as Orders

Inside quotes, write \" for a quote and \\ for a backslash. Spaces alone don't need quotes: Orders DB->API: Query works.

Comments

// Happy path only for now
Client->API: GET /orders

A line that starts with // is a comment. The app ignores it, and keeps it in the text when the diagram changes. A // later on a line isn't a comment, so a message's text can hold it.

Ids

Every participant and element has an id, such as Participant_Shop or Message_CreatePayment. Comments, the assistant and connected AI apps refer to elements by id. When you edit the code, the app keeps each element's id while it still says the same thing: a participant by its alias or name, a message while it has the same sender, receiver and text. So you can reorder lines and add new ones, and comments stay on their elements.

When a line has a mistake

If a line can't be read, the pane's header shows the message with its line number, and so does the Problems panel. The diagram keeps its last working version until you fix it.

MessageWhat to do
Can't read this line. A message looks like "Alice->Bob: Hello".Write the line as a message, or start it with a keyword such as note or alt.
Put the message after a colon: "Alice->Bob: Hello".Add a colon before the text.
Name a participant on both sides of the arrow.Add the missing sender or receiver.
Close the quote around this name.Add the closing ".
"…" is already declared on line …Declare each participant once. Change the first declaration instead.
Give the alias after "as": participant "Users DB" as DB.Write as before the alias.
Nothing goes after the alias.Remove what follows the alias.
"else" belongs inside an "alt" or a "par" block.Move the line inside an alt, or start the fragment with alt. and belongs inside a par.
"end" has no block to close.Remove the extra end.
"…" is never closed. Add "end" after its last line.Add end after the fragment's last line.
A note left of takes one participant. Write "note over A, B" to span several.Use note over for a note across several participants.
Close this note with "end note", or put its text after a colon.Add end note after the note's last line.

One case reads without an error but not as you meant: a message with no colon, such as App->DB Find user, reads DB Find user as the receiver's name. If a participant appears that you didn't expect, check for a missing colon.

Import a file

You can bring in a sequence diagram written for Mermaid or PlantUML. In the Diagrams panel, open New diagram (the plus button) and choose Import Mermaid or PlantUML file…. The app reads .mmd, .mermaid, .puml, .plantuml, .pu and .txt files, A file whose first line is sequenceDiagram is read as Mermaid, and any other file as PlantUML.

The file becomes a new diagram, named after its title or the file. Parts a sequence diagram here can't show, such as styling, participant boxes or shapes other than participant, actor and database, are read as near as they can be. When something is left out, a message after the import names it. A file with no sequence diagram in it isn't imported, and the message says so.

Export a file

Open Export at the top right of the diagram. Pictures are under Image, text files under Code:

Menu itemWhat you get
SVGAn .svg picture of the diagram.
PNGA .png picture of the diagram.
Copy as imageThe picture, copied to your clipboard.
Sequence codeA .seq file with the diagram in sequence code.
Mermaid fileA .mmd file for Mermaid. ⇧ ⌘ E (Shift Ctrl E) downloads it without opening the menu.
PlantUML fileA .puml file for PlantUML.
Copy Sequence codeThe sequence code, copied to your clipboard.

View Mermaid… and View PlantUML… at the bottom of the menu show the text read-only, with a button to copy it. In the Mermaid file, dividers become notes across the diagram and groups become shaded areas.

Full reference

The sequence code reference has the complete syntax on one page, exactly as the assistant gets it.

On this page