collab/mermaid/docs/syntax/sequenceDiagram.mdblame
View source
6dd74de1> **Warning**
6dd74de2>
6dd74de3> ## THIS IS AN AUTOGENERATED FILE. DO NOT EDIT.
6dd74de4>
6dd74de5> ## Please edit the corresponding file in [/packages/mermaid/src/docs/syntax/sequenceDiagram.md](../../packages/mermaid/src/docs/syntax/sequenceDiagram.md).
6dd74de6
6dd74de7# Sequence diagrams
6dd74de8
6dd74de9> A Sequence diagram is an interaction diagram that shows how processes operate with one another and in what order.
6dd74de10
6dd74de11Mermaid can render sequence diagrams.
6dd74de12
6dd74de13```mermaid-example
6dd74de14sequenceDiagram
6dd74de15 Alice->>John: Hello John, how are you?
6dd74de16 John-->>Alice: Great!
6dd74de17 Alice-)John: See you later!
6dd74de18```
6dd74de19
6dd74de20```mermaid
6dd74de21sequenceDiagram
6dd74de22 Alice->>John: Hello John, how are you?
6dd74de23 John-->>Alice: Great!
6dd74de24 Alice-)John: See you later!
6dd74de25```
6dd74de26
6dd74de27> **Note**
6dd74de28> A note on nodes, the word "end" could potentially break the diagram, due to the way that the mermaid language is scripted.
6dd74de29>
6dd74de30> If unavoidable, one must use parentheses(), quotation marks "", or brackets {},\[], to enclose the word "end". i.e : (end), \[end], {end}.
6dd74de31
6dd74de32## Syntax
6dd74de33
6dd74de34### Participants
6dd74de35
6dd74de36The participants can be defined implicitly as in the first example on this page. The participants or actors are
6dd74de37rendered in order of appearance in the diagram source text. Sometimes you might want to show the participants in a
6dd74de38different order than how they appear in the first message. It is possible to specify the actor's order of
6dd74de39appearance by doing the following:
6dd74de40
6dd74de41```mermaid-example
6dd74de42sequenceDiagram
6dd74de43 participant Alice
6dd74de44 participant Bob
6dd74de45 Bob->>Alice: Hi Alice
6dd74de46 Alice->>Bob: Hi Bob
6dd74de47```
6dd74de48
6dd74de49```mermaid
6dd74de50sequenceDiagram
6dd74de51 participant Alice
6dd74de52 participant Bob
6dd74de53 Bob->>Alice: Hi Alice
6dd74de54 Alice->>Bob: Hi Bob
6dd74de55```
6dd74de56
6dd74de57### Actors
6dd74de58
6dd74de59If you specifically want to use the actor symbol instead of a rectangle with text you can do so by using actor statements as per below.
6dd74de60
6dd74de61```mermaid-example
6dd74de62sequenceDiagram
6dd74de63 actor Alice
6dd74de64 actor Bob
6dd74de65 Alice->>Bob: Hi Bob
6dd74de66 Bob->>Alice: Hi Alice
6dd74de67```
6dd74de68
6dd74de69```mermaid
6dd74de70sequenceDiagram
6dd74de71 actor Alice
6dd74de72 actor Bob
6dd74de73 Alice->>Bob: Hi Bob
6dd74de74 Bob->>Alice: Hi Alice
6dd74de75```
6dd74de76
6dd74de77### Boundary
6dd74de78
6dd74de79If you want to use the boundary symbol for a participant, use the JSON configuration syntax as shown below.
6dd74de80
6dd74de81```mermaid-example
6dd74de82sequenceDiagram
6dd74de83 participant Alice@{ "type" : "boundary" }
6dd74de84 participant Bob
6dd74de85 Alice->>Bob: Request from boundary
6dd74de86 Bob->>Alice: Response to boundary
6dd74de87```
6dd74de88
6dd74de89```mermaid
6dd74de90sequenceDiagram
6dd74de91 participant Alice@{ "type" : "boundary" }
6dd74de92 participant Bob
6dd74de93 Alice->>Bob: Request from boundary
6dd74de94 Bob->>Alice: Response to boundary
6dd74de95```
6dd74de96
6dd74de97### Control
6dd74de98
6dd74de99If you want to use the control symbol for a participant, use the JSON configuration syntax as shown below.
6dd74de100
6dd74de101```mermaid-example
6dd74de102sequenceDiagram
6dd74de103 participant Alice@{ "type" : "control" }
6dd74de104 participant Bob
6dd74de105 Alice->>Bob: Control request
6dd74de106 Bob->>Alice: Control response
6dd74de107```
6dd74de108
6dd74de109```mermaid
6dd74de110sequenceDiagram
6dd74de111 participant Alice@{ "type" : "control" }
6dd74de112 participant Bob
6dd74de113 Alice->>Bob: Control request
6dd74de114 Bob->>Alice: Control response
6dd74de115```
6dd74de116
6dd74de117### Entity
6dd74de118
6dd74de119If you want to use the entity symbol for a participant, use the JSON configuration syntax as shown below.
6dd74de120
6dd74de121```mermaid-example
6dd74de122sequenceDiagram
6dd74de123 participant Alice@{ "type" : "entity" }
6dd74de124 participant Bob
6dd74de125 Alice->>Bob: Entity request
6dd74de126 Bob->>Alice: Entity response
6dd74de127```
6dd74de128
6dd74de129```mermaid
6dd74de130sequenceDiagram
6dd74de131 participant Alice@{ "type" : "entity" }
6dd74de132 participant Bob
6dd74de133 Alice->>Bob: Entity request
6dd74de134 Bob->>Alice: Entity response
6dd74de135```
6dd74de136
6dd74de137### Database
6dd74de138
6dd74de139If you want to use the database symbol for a participant, use the JSON configuration syntax as shown below.
6dd74de140
6dd74de141```mermaid-example
6dd74de142sequenceDiagram
6dd74de143 participant Alice@{ "type" : "database" }
6dd74de144 participant Bob
6dd74de145 Alice->>Bob: DB query
6dd74de146 Bob->>Alice: DB result
6dd74de147```
6dd74de148
6dd74de149```mermaid
6dd74de150sequenceDiagram
6dd74de151 participant Alice@{ "type" : "database" }
6dd74de152 participant Bob
6dd74de153 Alice->>Bob: DB query
6dd74de154 Bob->>Alice: DB result
6dd74de155```
6dd74de156
6dd74de157### Collections
6dd74de158
6dd74de159If you want to use the collections symbol for a participant, use the JSON configuration syntax as shown below.
6dd74de160
6dd74de161```mermaid-example
6dd74de162sequenceDiagram
6dd74de163 participant Alice@{ "type" : "collections" }
6dd74de164 participant Bob
6dd74de165 Alice->>Bob: Collections request
6dd74de166 Bob->>Alice: Collections response
6dd74de167```
6dd74de168
6dd74de169```mermaid
6dd74de170sequenceDiagram
6dd74de171 participant Alice@{ "type" : "collections" }
6dd74de172 participant Bob
6dd74de173 Alice->>Bob: Collections request
6dd74de174 Bob->>Alice: Collections response
6dd74de175```
6dd74de176
6dd74de177### Queue
6dd74de178
6dd74de179If you want to use the queue symbol for a participant, use the JSON configuration syntax as shown below.
6dd74de180
6dd74de181```mermaid-example
6dd74de182sequenceDiagram
6dd74de183 participant Alice@{ "type" : "queue" }
6dd74de184 participant Bob
6dd74de185 Alice->>Bob: Queue message
6dd74de186 Bob->>Alice: Queue response
6dd74de187```
6dd74de188
6dd74de189```mermaid
6dd74de190sequenceDiagram
6dd74de191 participant Alice@{ "type" : "queue" }
6dd74de192 participant Bob
6dd74de193 Alice->>Bob: Queue message
6dd74de194 Bob->>Alice: Queue response
6dd74de195```
6dd74de196
6dd74de197### Aliases
6dd74de198
6dd74de199The actor can have a convenient identifier and a descriptive label. Aliases can be defined in two ways: using external syntax with the `as` keyword, or inline within the configuration object.
6dd74de200
6dd74de201#### External Alias Syntax
6dd74de202
6dd74de203You can define an alias using the `as` keyword after the participant declaration:
6dd74de204
6dd74de205```mermaid-example
6dd74de206sequenceDiagram
6dd74de207 participant A as Alice
6dd74de208 participant J as John
6dd74de209 A->>J: Hello John, how are you?
6dd74de210 J->>A: Great!
6dd74de211```
6dd74de212
6dd74de213```mermaid
6dd74de214sequenceDiagram
6dd74de215 participant A as Alice
6dd74de216 participant J as John
6dd74de217 A->>J: Hello John, how are you?
6dd74de218 J->>A: Great!
6dd74de219```
6dd74de220
6dd74de221The external alias syntax also works with participant stereotype configurations, allowing you to combine type specification with aliases:
6dd74de222
6dd74de223```mermaid-example
6dd74de224sequenceDiagram
6dd74de225 participant API@{ "type": "boundary" } as Public API
6dd74de226 actor DB@{ "type": "database" } as User Database
6dd74de227 participant Svc@{ "type": "control" } as Auth Service
6dd74de228 API->>Svc: Authenticate
6dd74de229 Svc->>DB: Query user
6dd74de230 DB-->>Svc: User data
6dd74de231 Svc-->>API: Token
6dd74de232```
6dd74de233
6dd74de234```mermaid
6dd74de235sequenceDiagram
6dd74de236 participant API@{ "type": "boundary" } as Public API
6dd74de237 actor DB@{ "type": "database" } as User Database
6dd74de238 participant Svc@{ "type": "control" } as Auth Service
6dd74de239 API->>Svc: Authenticate
6dd74de240 Svc->>DB: Query user
6dd74de241 DB-->>Svc: User data
6dd74de242 Svc-->>API: Token
6dd74de243```
6dd74de244
6dd74de245#### Inline Alias Syntax
6dd74de246
6dd74de247Alternatively, you can define an alias directly inside the configuration object using the `"alias"` field. This works with both `participant` and `actor` keywords:
6dd74de248
6dd74de249```mermaid-example
6dd74de250sequenceDiagram
6dd74de251 participant API@{ "type": "boundary", "alias": "Public API" }
6dd74de252 participant Auth@{ "type": "control", "alias": "Auth Service" }
6dd74de253 participant DB@{ "type": "database", "alias": "User Database" }
6dd74de254 API->>Auth: Login request
6dd74de255 Auth->>DB: Query user
6dd74de256 DB-->>Auth: User data
6dd74de257 Auth-->>API: Access token
6dd74de258```
6dd74de259
6dd74de260```mermaid
6dd74de261sequenceDiagram
6dd74de262 participant API@{ "type": "boundary", "alias": "Public API" }
6dd74de263 participant Auth@{ "type": "control", "alias": "Auth Service" }
6dd74de264 participant DB@{ "type": "database", "alias": "User Database" }
6dd74de265 API->>Auth: Login request
6dd74de266 Auth->>DB: Query user
6dd74de267 DB-->>Auth: User data
6dd74de268 Auth-->>API: Access token
6dd74de269```
6dd74de270
6dd74de271#### Alias Precedence
6dd74de272
6dd74de273When both inline alias (in the configuration object) and external alias (using `as` keyword) are provided, the **external alias takes precedence**:
6dd74de274
6dd74de275```mermaid-example
6dd74de276sequenceDiagram
6dd74de277 participant API@{ "type": "boundary", "alias": "Internal Name" } as External Name
6dd74de278 participant DB@{ "type": "database", "alias": "Internal DB" } as External DB
6dd74de279 API->>DB: Query
6dd74de280 DB-->>API: Result
6dd74de281```
6dd74de282
6dd74de283```mermaid
6dd74de284sequenceDiagram
6dd74de285 participant API@{ "type": "boundary", "alias": "Internal Name" } as External Name
6dd74de286 participant DB@{ "type": "database", "alias": "Internal DB" } as External DB
6dd74de287 API->>DB: Query
6dd74de288 DB-->>API: Result
6dd74de289```
6dd74de290
6dd74de291In the example above, "External Name" and "External DB" will be displayed, not "Internal Name" and "Internal DB".
6dd74de292
6dd74de293### Actor Creation and Destruction (v10.3.0+)
6dd74de294
6dd74de295It is possible to create and destroy actors by messages. To do so, add a create or destroy directive before the message.
6dd74de296
6dd74de297```
6dd74de298create participant B
6dd74de299A --> B: Hello
6dd74de300```
6dd74de301
6dd74de302Create directives support actor/participant distinction and aliases. The sender or the recipient of a message can be destroyed but only the recipient can be created.
6dd74de303
6dd74de304```mermaid-example
6dd74de305sequenceDiagram
6dd74de306 Alice->>Bob: Hello Bob, how are you ?
6dd74de307 Bob->>Alice: Fine, thank you. And you?
6dd74de308 create participant Carl
6dd74de309 Alice->>Carl: Hi Carl!
6dd74de310 create actor D as Donald
6dd74de311 Carl->>D: Hi!
6dd74de312 destroy Carl
6dd74de313 Alice-xCarl: We are too many
6dd74de314 destroy Bob
6dd74de315 Bob->>Alice: I agree
6dd74de316```
6dd74de317
6dd74de318```mermaid
6dd74de319sequenceDiagram
6dd74de320 Alice->>Bob: Hello Bob, how are you ?
6dd74de321 Bob->>Alice: Fine, thank you. And you?
6dd74de322 create participant Carl
6dd74de323 Alice->>Carl: Hi Carl!
6dd74de324 create actor D as Donald
6dd74de325 Carl->>D: Hi!
6dd74de326 destroy Carl
6dd74de327 Alice-xCarl: We are too many
6dd74de328 destroy Bob
6dd74de329 Bob->>Alice: I agree
6dd74de330```
6dd74de331
6dd74de332#### Unfixable actor/participant creation/deletion error
6dd74de333
6dd74de334If an error of the following type occurs when creating or deleting an actor/participant:
6dd74de335
6dd74de336> The destroyed participant **participant-name** does not have an associated destroying message after its declaration. Please check the sequence diagram.
6dd74de337
6dd74de338And fixing diagram code does not get rid of this error and rendering of all other diagrams results in the same error, then you need to update the mermaid version to (v10.7.0+).
6dd74de339
6dd74de340### Grouping / Box
6dd74de341
6dd74de342The actor(s) can be grouped in vertical boxes. You can define a color (if not, it will be transparent) and/or a descriptive label using the following notation:
6dd74de343
6dd74de344```
6dd74de345box Aqua Group Description
6dd74de346... actors ...
6dd74de347end
6dd74de348box Group without description
6dd74de349... actors ...
6dd74de350end
6dd74de351box rgb(33,66,99)
6dd74de352... actors ...
6dd74de353end
6dd74de354box rgba(33,66,99,0.5)
6dd74de355... actors ...
6dd74de356end
6dd74de357```
6dd74de358
6dd74de359> **Note**
6dd74de360> If your group name is a color you can force the color to be transparent:
6dd74de361
6dd74de362```
6dd74de363box transparent Aqua
6dd74de364... actors ...
6dd74de365end
6dd74de366```
6dd74de367
6dd74de368```mermaid-example
6dd74de369 sequenceDiagram
6dd74de370 box Purple Alice & John
6dd74de371 participant A
6dd74de372 participant J
6dd74de373 end
6dd74de374 box Another Group
6dd74de375 participant B
6dd74de376 participant C
6dd74de377 end
6dd74de378 A->>J: Hello John, how are you?
6dd74de379 J->>A: Great!
6dd74de380 A->>B: Hello Bob, how is Charley?
6dd74de381 B->>C: Hello Charley, how are you?
6dd74de382```
6dd74de383
6dd74de384```mermaid
6dd74de385 sequenceDiagram
6dd74de386 box Purple Alice & John
6dd74de387 participant A
6dd74de388 participant J
6dd74de389 end
6dd74de390 box Another Group
6dd74de391 participant B
6dd74de392 participant C
6dd74de393 end
6dd74de394 A->>J: Hello John, how are you?
6dd74de395 J->>A: Great!
6dd74de396 A->>B: Hello Bob, how is Charley?
6dd74de397 B->>C: Hello Charley, how are you?
6dd74de398```
6dd74de399
6dd74de400## Messages
6dd74de401
6dd74de402Messages can be of two displayed either solid or with a dotted line.
6dd74de403
6dd74de404```
6dd74de405[Actor][Arrow][Actor]:Message text
6dd74de406```
6dd74de407
6dd74de408Lines can be solid or dotted, and can end with various types of arrowheads, crosses, or open arrows.
6dd74de409
6dd74de410#### Supported Arrow Types
6dd74de411
6dd74de412**Standard Arrow Types**
6dd74de413
6dd74de414| Type | Description |
6dd74de415| -------- | ---------------------------------------------------- |
6dd74de416| `->` | Solid line without arrow |
6dd74de417| `-->` | Dotted line without arrow |
6dd74de418| `->>` | Solid line with arrowhead |
6dd74de419| `-->>` | Dotted line with arrowhead |
6dd74de420| `<<->>` | Solid line with bidirectional arrowheads (v11.0.0+) |
6dd74de421| `<<-->>` | Dotted line with bidirectional arrowheads (v11.0.0+) |
6dd74de422| `-x` | Solid line with a cross at the end |
6dd74de423| `--x` | Dotted line with a cross at the end |
6dd74de424| `-)` | Solid line with an open arrow at the end (async) |
6dd74de425| `--)` | Dotted line with a open arrow at the end (async) |
6dd74de426
6dd74de427**Half-Arrows (v\<MERMAID_RELEASE_VERSION>+)**
6dd74de428
6dd74de429The following half-arrow types are supported for more expressive sequence diagrams. Both solid and dotted variants are available by increasing the number of dashes (`-` → `--`).
6dd74de430
6dd74de431---
6dd74de432
6dd74de433| Type | Description |
6dd74de434| ------- | ---------------------------------------------------- |
6dd74de435| `-\|\` | Solid line with top half arrowhead |
6dd74de436| `--\|\` | Dotted line with top half arrowhead |
6dd74de437| `-\|/` | Solid line with bottom half arrowhead |
6dd74de438| `--\|/` | Dotted line with bottom half arrowhead |
6dd74de439| `/\|-` | Solid line with reverse top half arrowhead |
6dd74de440| `/\|--` | Dotted line with reverse top half arrowhead |
6dd74de441| `\\-` | Solid line with reverse bottom half arrowhead |
6dd74de442| `\\--` | Dotted line with reverse bottom half arrowhead |
6dd74de443| `-\\` | Solid line with top stick half arrowhead |
6dd74de444| `--\\` | Dotted line with top stick half arrowhead |
6dd74de445| `-//` | Solid line with bottom stick half arrowhead |
6dd74de446| `--//` | Dotted line with bottom stick half arrowhead |
6dd74de447| `//-` | Solid line with reverse top stick half arrowhead |
6dd74de448| `//--` | Dotted line with reverse top stick half arrowhead |
6dd74de449| `\\-` | Solid line with reverse bottom stick half arrowhead |
6dd74de450| `\\--` | Dotted line with reverse bottom stick half arrowhead |
6dd74de451
6dd74de452## Central Connections (v\<MERMAID_RELEASE_VERSION>+)
6dd74de453
6dd74de454Mermaid sequence diagrams support **central lifeline connections** using a `()`.
6dd74de455This is useful to represent messages or signals that connect to a central point, rather than from one actor directly to another.
6dd74de456
6dd74de457To indicate a central connection, append `()` to the arrow syntax.
6dd74de458
6dd74de459#### Basic Syntax
6dd74de460
6dd74de461```mermaid-example
6dd74de462sequenceDiagram
6dd74de463 participant Alice
6dd74de464 participant John
6dd74de465 Alice->>()John: Hello John
6dd74de466 Alice()->>John: How are you?
6dd74de467 John()->>()Alice: Great!
6dd74de468```
6dd74de469
6dd74de470```mermaid
6dd74de471sequenceDiagram
6dd74de472 participant Alice
6dd74de473 participant John
6dd74de474 Alice->>()John: Hello John
6dd74de475 Alice()->>John: How are you?
6dd74de476 John()->>()Alice: Great!
6dd74de477```
6dd74de478
6dd74de479## Activations
6dd74de480
6dd74de481It is possible to activate and deactivate an actor. (de)activation can be dedicated declarations:
6dd74de482
6dd74de483```mermaid-example
6dd74de484sequenceDiagram
6dd74de485 Alice->>John: Hello John, how are you?
6dd74de486 activate John
6dd74de487 John-->>Alice: Great!
6dd74de488 deactivate John
6dd74de489```
6dd74de490
6dd74de491```mermaid
6dd74de492sequenceDiagram
6dd74de493 Alice->>John: Hello John, how are you?
6dd74de494 activate John
6dd74de495 John-->>Alice: Great!
6dd74de496 deactivate John
6dd74de497```
6dd74de498
6dd74de499There is also a shortcut notation by appending `+`/`-` suffix to the message arrow:
6dd74de500
6dd74de501```mermaid-example
6dd74de502sequenceDiagram
6dd74de503 Alice->>+John: Hello John, how are you?
6dd74de504 John-->>-Alice: Great!
6dd74de505```
6dd74de506
6dd74de507```mermaid
6dd74de508sequenceDiagram
6dd74de509 Alice->>+John: Hello John, how are you?
6dd74de510 John-->>-Alice: Great!
6dd74de511```
6dd74de512
6dd74de513Activations can be stacked for same actor:
6dd74de514
6dd74de515```mermaid-example
6dd74de516sequenceDiagram
6dd74de517 Alice->>+John: Hello John, how are you?
6dd74de518 Alice->>+John: John, can you hear me?
6dd74de519 John-->>-Alice: Hi Alice, I can hear you!
6dd74de520 John-->>-Alice: I feel great!
6dd74de521```
6dd74de522
6dd74de523```mermaid
6dd74de524sequenceDiagram
6dd74de525 Alice->>+John: Hello John, how are you?
6dd74de526 Alice->>+John: John, can you hear me?
6dd74de527 John-->>-Alice: Hi Alice, I can hear you!
6dd74de528 John-->>-Alice: I feel great!
6dd74de529```
6dd74de530
6dd74de531## Notes
6dd74de532
6dd74de533It is possible to add notes to a sequence diagram. This is done by the notation
6dd74de534Note \[ right of | left of | over ] \[Actor]: Text in note content
6dd74de535
6dd74de536See the example below:
6dd74de537
6dd74de538```mermaid-example
6dd74de539sequenceDiagram
6dd74de540 participant John
6dd74de541 Note right of John: Text in note
6dd74de542```
6dd74de543
6dd74de544```mermaid
6dd74de545sequenceDiagram
6dd74de546 participant John
6dd74de547 Note right of John: Text in note
6dd74de548```
6dd74de549
6dd74de550It is also possible to create notes spanning two participants:
6dd74de551
6dd74de552```mermaid-example
6dd74de553sequenceDiagram
6dd74de554 Alice->John: Hello John, how are you?
6dd74de555 Note over Alice,John: A typical interaction
6dd74de556```
6dd74de557
6dd74de558```mermaid
6dd74de559sequenceDiagram
6dd74de560 Alice->John: Hello John, how are you?
6dd74de561 Note over Alice,John: A typical interaction
6dd74de562```
6dd74de563
6dd74de564## Line breaks
6dd74de565
6dd74de566Line break can be added to Note and Message:
6dd74de567
6dd74de568```mermaid-example
6dd74de569sequenceDiagram
6dd74de570 Alice->John: Hello John,<br/>how are you?
6dd74de571 Note over Alice,John: A typical interaction<br/>But now in two lines
6dd74de572```
6dd74de573
6dd74de574```mermaid
6dd74de575sequenceDiagram
6dd74de576 Alice->John: Hello John,<br/>how are you?
6dd74de577 Note over Alice,John: A typical interaction<br/>But now in two lines
6dd74de578```
6dd74de579
6dd74de580Line breaks in Actor names requires aliases:
6dd74de581
6dd74de582```mermaid-example
6dd74de583sequenceDiagram
6dd74de584 participant Alice as Alice<br/>Johnson
6dd74de585 Alice->John: Hello John,<br/>how are you?
6dd74de586 Note over Alice,John: A typical interaction<br/>But now in two lines
6dd74de587```
6dd74de588
6dd74de589```mermaid
6dd74de590sequenceDiagram
6dd74de591 participant Alice as Alice<br/>Johnson
6dd74de592 Alice->John: Hello John,<br/>how are you?
6dd74de593 Note over Alice,John: A typical interaction<br/>But now in two lines
6dd74de594```
6dd74de595
6dd74de596## Loops
6dd74de597
6dd74de598It is possible to express loops in a sequence diagram. This is done by the notation
6dd74de599
6dd74de600```
6dd74de601loop Loop text
6dd74de602... statements ...
6dd74de603end
6dd74de604```
6dd74de605
6dd74de606See the example below:
6dd74de607
6dd74de608```mermaid-example
6dd74de609sequenceDiagram
6dd74de610 Alice->John: Hello John, how are you?
6dd74de611 loop Every minute
6dd74de612 John-->Alice: Great!
6dd74de613 end
6dd74de614```
6dd74de615
6dd74de616```mermaid
6dd74de617sequenceDiagram
6dd74de618 Alice->John: Hello John, how are you?
6dd74de619 loop Every minute
6dd74de620 John-->Alice: Great!
6dd74de621 end
6dd74de622```
6dd74de623
6dd74de624## Alt
6dd74de625
6dd74de626It is possible to express alternative paths in a sequence diagram. This is done by the notation
6dd74de627
6dd74de628```
6dd74de629alt Describing text
6dd74de630... statements ...
6dd74de631else
6dd74de632... statements ...
6dd74de633end
6dd74de634```
6dd74de635
6dd74de636or if there is sequence that is optional (if without else).
6dd74de637
6dd74de638```
6dd74de639opt Describing text
6dd74de640... statements ...
6dd74de641end
6dd74de642```
6dd74de643
6dd74de644See the example below:
6dd74de645
6dd74de646```mermaid-example
6dd74de647sequenceDiagram
6dd74de648 Alice->>Bob: Hello Bob, how are you?
6dd74de649 alt is sick
6dd74de650 Bob->>Alice: Not so good :(
6dd74de651 else is well
6dd74de652 Bob->>Alice: Feeling fresh like a daisy
6dd74de653 end
6dd74de654 opt Extra response
6dd74de655 Bob->>Alice: Thanks for asking
6dd74de656 end
6dd74de657```
6dd74de658
6dd74de659```mermaid
6dd74de660sequenceDiagram
6dd74de661 Alice->>Bob: Hello Bob, how are you?
6dd74de662 alt is sick
6dd74de663 Bob->>Alice: Not so good :(
6dd74de664 else is well
6dd74de665 Bob->>Alice: Feeling fresh like a daisy
6dd74de666 end
6dd74de667 opt Extra response
6dd74de668 Bob->>Alice: Thanks for asking
6dd74de669 end
6dd74de670```
6dd74de671
6dd74de672## Parallel
6dd74de673
6dd74de674It is possible to show actions that are happening in parallel.
6dd74de675
6dd74de676This is done by the notation
6dd74de677
6dd74de678```
6dd74de679par [Action 1]
6dd74de680... statements ...
6dd74de681and [Action 2]
6dd74de682... statements ...
6dd74de683and [Action N]
6dd74de684... statements ...
6dd74de685end
6dd74de686```
6dd74de687
6dd74de688See the example below:
6dd74de689
6dd74de690```mermaid-example
6dd74de691sequenceDiagram
6dd74de692 par Alice to Bob
6dd74de693 Alice->>Bob: Hello guys!
6dd74de694 and Alice to John
6dd74de695 Alice->>John: Hello guys!
6dd74de696 end
6dd74de697 Bob-->>Alice: Hi Alice!
6dd74de698 John-->>Alice: Hi Alice!
6dd74de699```
6dd74de700
6dd74de701```mermaid
6dd74de702sequenceDiagram
6dd74de703 par Alice to Bob
6dd74de704 Alice->>Bob: Hello guys!
6dd74de705 and Alice to John
6dd74de706 Alice->>John: Hello guys!
6dd74de707 end
6dd74de708 Bob-->>Alice: Hi Alice!
6dd74de709 John-->>Alice: Hi Alice!
6dd74de710```
6dd74de711
6dd74de712It is also possible to nest parallel blocks.
6dd74de713
6dd74de714```mermaid-example
6dd74de715sequenceDiagram
6dd74de716 par Alice to Bob
6dd74de717 Alice->>Bob: Go help John
6dd74de718 and Alice to John
6dd74de719 Alice->>John: I want this done today
6dd74de720 par John to Charlie
6dd74de721 John->>Charlie: Can we do this today?
6dd74de722 and John to Diana
6dd74de723 John->>Diana: Can you help us today?
6dd74de724 end
6dd74de725 end
6dd74de726```
6dd74de727
6dd74de728```mermaid
6dd74de729sequenceDiagram
6dd74de730 par Alice to Bob
6dd74de731 Alice->>Bob: Go help John
6dd74de732 and Alice to John
6dd74de733 Alice->>John: I want this done today
6dd74de734 par John to Charlie
6dd74de735 John->>Charlie: Can we do this today?
6dd74de736 and John to Diana
6dd74de737 John->>Diana: Can you help us today?
6dd74de738 end
6dd74de739 end
6dd74de740```
6dd74de741
6dd74de742## Critical Region
6dd74de743
6dd74de744It is possible to show actions that must happen automatically with conditional handling of circumstances.
6dd74de745
6dd74de746This is done by the notation
6dd74de747
6dd74de748```
6dd74de749critical [Action that must be performed]
6dd74de750... statements ...
6dd74de751option [Circumstance A]
6dd74de752... statements ...
6dd74de753option [Circumstance B]
6dd74de754... statements ...
6dd74de755end
6dd74de756```
6dd74de757
6dd74de758See the example below:
6dd74de759
6dd74de760```mermaid-example
6dd74de761sequenceDiagram
6dd74de762 critical Establish a connection to the DB
6dd74de763 Service-->DB: connect
6dd74de764 option Network timeout
6dd74de765 Service-->Service: Log error
6dd74de766 option Credentials rejected
6dd74de767 Service-->Service: Log different error
6dd74de768 end
6dd74de769```
6dd74de770
6dd74de771```mermaid
6dd74de772sequenceDiagram
6dd74de773 critical Establish a connection to the DB
6dd74de774 Service-->DB: connect
6dd74de775 option Network timeout
6dd74de776 Service-->Service: Log error
6dd74de777 option Credentials rejected
6dd74de778 Service-->Service: Log different error
6dd74de779 end
6dd74de780```
6dd74de781
6dd74de782It is also possible to have no options at all
6dd74de783
6dd74de784```mermaid-example
6dd74de785sequenceDiagram
6dd74de786 critical Establish a connection to the DB
6dd74de787 Service-->DB: connect
6dd74de788 end
6dd74de789```
6dd74de790
6dd74de791```mermaid
6dd74de792sequenceDiagram
6dd74de793 critical Establish a connection to the DB
6dd74de794 Service-->DB: connect
6dd74de795 end
6dd74de796```
6dd74de797
6dd74de798This critical block can also be nested, equivalently to the `par` statement as seen above.
6dd74de799
6dd74de800## Break
6dd74de801
6dd74de802It is possible to indicate a stop of the sequence within the flow (usually used to model exceptions).
6dd74de803
6dd74de804This is done by the notation
6dd74de805
6dd74de806```
6dd74de807break [something happened]
6dd74de808... statements ...
6dd74de809end
6dd74de810```
6dd74de811
6dd74de812See the example below:
6dd74de813
6dd74de814```mermaid-example
6dd74de815sequenceDiagram
6dd74de816 Consumer-->API: Book something
6dd74de817 API-->BookingService: Start booking process
6dd74de818 break when the booking process fails
6dd74de819 API-->Consumer: show failure
6dd74de820 end
6dd74de821 API-->BillingService: Start billing process
6dd74de822```
6dd74de823
6dd74de824```mermaid
6dd74de825sequenceDiagram
6dd74de826 Consumer-->API: Book something
6dd74de827 API-->BookingService: Start booking process
6dd74de828 break when the booking process fails
6dd74de829 API-->Consumer: show failure
6dd74de830 end
6dd74de831 API-->BillingService: Start billing process
6dd74de832```
6dd74de833
6dd74de834## Background Highlighting
6dd74de835
6dd74de836It is possible to highlight flows by providing colored background rects. This is done by the notation
6dd74de837
6dd74de838```
6dd74de839rect COLOR
6dd74de840... content ...
6dd74de841end
6dd74de842```
6dd74de843
6dd74de844The colors are defined using rgb and rgba syntax.
6dd74de845
6dd74de846```
6dd74de847rect rgb(0, 255, 0)
6dd74de848... content ...
6dd74de849end
6dd74de850```
6dd74de851
6dd74de852```
6dd74de853rect rgba(0, 0, 255, .1)
6dd74de854... content ...
6dd74de855end
6dd74de856```
6dd74de857
6dd74de858See the examples below:
6dd74de859
6dd74de860```mermaid-example
6dd74de861sequenceDiagram
6dd74de862 participant Alice
6dd74de863 participant John
6dd74de864
6dd74de865 rect rgb(191, 223, 255)
6dd74de866 note right of Alice: Alice calls John.
6dd74de867 Alice->>+John: Hello John, how are you?
6dd74de868 rect rgb(200, 150, 255)
6dd74de869 Alice->>+John: John, can you hear me?
6dd74de870 John-->>-Alice: Hi Alice, I can hear you!
6dd74de871 end
6dd74de872 John-->>-Alice: I feel great!
6dd74de873 end
6dd74de874 Alice ->>+ John: Did you want to go to the game tonight?
6dd74de875 John -->>- Alice: Yeah! See you there.
6dd74de876
6dd74de877```
6dd74de878
6dd74de879```mermaid
6dd74de880sequenceDiagram
6dd74de881 participant Alice
6dd74de882 participant John
6dd74de883
6dd74de884 rect rgb(191, 223, 255)
6dd74de885 note right of Alice: Alice calls John.
6dd74de886 Alice->>+John: Hello John, how are you?
6dd74de887 rect rgb(200, 150, 255)
6dd74de888 Alice->>+John: John, can you hear me?
6dd74de889 John-->>-Alice: Hi Alice, I can hear you!
6dd74de890 end
6dd74de891 John-->>-Alice: I feel great!
6dd74de892 end
6dd74de893 Alice ->>+ John: Did you want to go to the game tonight?
6dd74de894 John -->>- Alice: Yeah! See you there.
6dd74de895
6dd74de896```
6dd74de897
6dd74de898## Comments
6dd74de899
6dd74de900Comments can be entered within a sequence diagram, which will be ignored by the parser. Comments need to be on their own line, and must be prefaced with `%%` (double percent signs). Any text after the start of the comment to the next newline will be treated as a comment, including any diagram syntax
6dd74de901
6dd74de902```mermaid-example
6dd74de903sequenceDiagram
6dd74de904 Alice->>John: Hello John, how are you?
6dd74de905 %% this is a comment
6dd74de906 John-->>Alice: Great!
6dd74de907```
6dd74de908
6dd74de909```mermaid
6dd74de910sequenceDiagram
6dd74de911 Alice->>John: Hello John, how are you?
6dd74de912 %% this is a comment
6dd74de913 John-->>Alice: Great!
6dd74de914```
6dd74de915
6dd74de916## Entity codes to escape characters
6dd74de917
6dd74de918It is possible to escape characters using the syntax exemplified here.
6dd74de919
6dd74de920```mermaid-example
6dd74de921sequenceDiagram
6dd74de922 A->>B: I #9829; you!
6dd74de923 B->>A: I #9829; you #infin; times more!
6dd74de924```
6dd74de925
6dd74de926```mermaid
6dd74de927sequenceDiagram
6dd74de928 A->>B: I #9829; you!
6dd74de929 B->>A: I #9829; you #infin; times more!
6dd74de930```
6dd74de931
6dd74de932Numbers given are base 10, so `#` can be encoded as `#35;`. It is also supported to use HTML character names.
6dd74de933
6dd74de934Because semicolons can be used instead of line breaks to define the markup, you need to use `#59;` to include a semicolon in message text.
6dd74de935
6dd74de936## sequenceNumbers
6dd74de937
6dd74de938It is possible to get a sequence number attached to each arrow in a sequence diagram. This can be configured when adding mermaid to the website as shown below:
6dd74de939
6dd74de940```html
6dd74de941<script>
6dd74de942 mermaid.initialize({ sequence: { showSequenceNumbers: true } });
6dd74de943</script>
6dd74de944```
6dd74de945
6dd74de946It can also be turned on via the diagram code as in the diagram:
6dd74de947
6dd74de948```mermaid-example
6dd74de949sequenceDiagram
6dd74de950 autonumber
6dd74de951 Alice->>John: Hello John, how are you?
6dd74de952 loop HealthCheck
6dd74de953 John->>John: Fight against hypochondria
6dd74de954 end
6dd74de955 Note right of John: Rational thoughts!
6dd74de956 John-->>Alice: Great!
6dd74de957 John->>Bob: How about you?
6dd74de958 Bob-->>John: Jolly good!
6dd74de959```
6dd74de960
6dd74de961```mermaid
6dd74de962sequenceDiagram
6dd74de963 autonumber
6dd74de964 Alice->>John: Hello John, how are you?
6dd74de965 loop HealthCheck
6dd74de966 John->>John: Fight against hypochondria
6dd74de967 end
6dd74de968 Note right of John: Rational thoughts!
6dd74de969 John-->>Alice: Great!
6dd74de970 John->>Bob: How about you?
6dd74de971 Bob-->>John: Jolly good!
6dd74de972```
6dd74de973
6dd74de974## Actor Menus
6dd74de975
6dd74de976Actors can have popup-menus containing individualized links to external pages. For example, if an actor represented a web service, useful links might include a link to the service health dashboard, repo containing the code for the service, or a wiki page describing the service.
6dd74de977
6dd74de978This can be configured by adding one or more link lines with the format:
6dd74de979
6dd74de980```
6dd74de981link <actor>: <link-label> @ <link-url>
6dd74de982```
6dd74de983
6dd74de984```mermaid-example
6dd74de985sequenceDiagram
6dd74de986 participant Alice
6dd74de987 participant John
6dd74de988 link Alice: Dashboard @ https://dashboard.contoso.com/alice
6dd74de989 link Alice: Wiki @ https://wiki.contoso.com/alice
6dd74de990 link John: Dashboard @ https://dashboard.contoso.com/john
6dd74de991 link John: Wiki @ https://wiki.contoso.com/john
6dd74de992 Alice->>John: Hello John, how are you?
6dd74de993 John-->>Alice: Great!
6dd74de994 Alice-)John: See you later!
6dd74de995```
6dd74de996
6dd74de997```mermaid
6dd74de998sequenceDiagram
6dd74de999 participant Alice
6dd74de1000 participant John
6dd74de1001 link Alice: Dashboard @ https://dashboard.contoso.com/alice
6dd74de1002 link Alice: Wiki @ https://wiki.contoso.com/alice
6dd74de1003 link John: Dashboard @ https://dashboard.contoso.com/john
6dd74de1004 link John: Wiki @ https://wiki.contoso.com/john
6dd74de1005 Alice->>John: Hello John, how are you?
6dd74de1006 John-->>Alice: Great!
6dd74de1007 Alice-)John: See you later!
6dd74de1008```
6dd74de1009
6dd74de1010#### Advanced Menu Syntax
6dd74de1011
6dd74de1012There is an advanced syntax that relies on JSON formatting. If you are comfortable with JSON format, then this exists as well.
6dd74de1013
6dd74de1014This can be configured by adding the links lines with the format:
6dd74de1015
6dd74de1016```
6dd74de1017links <actor>: <json-formatted link-name link-url pairs>
6dd74de1018```
6dd74de1019
6dd74de1020An example is below:
6dd74de1021
6dd74de1022```mermaid-example
6dd74de1023sequenceDiagram
6dd74de1024 participant Alice
6dd74de1025 participant John
6dd74de1026 links Alice: {"Dashboard": "https://dashboard.contoso.com/alice", "Wiki": "https://wiki.contoso.com/alice"}
6dd74de1027 links John: {"Dashboard": "https://dashboard.contoso.com/john", "Wiki": "https://wiki.contoso.com/john"}
6dd74de1028 Alice->>John: Hello John, how are you?
6dd74de1029 John-->>Alice: Great!
6dd74de1030 Alice-)John: See you later!
6dd74de1031```
6dd74de1032
6dd74de1033```mermaid
6dd74de1034sequenceDiagram
6dd74de1035 participant Alice
6dd74de1036 participant John
6dd74de1037 links Alice: {"Dashboard": "https://dashboard.contoso.com/alice", "Wiki": "https://wiki.contoso.com/alice"}
6dd74de1038 links John: {"Dashboard": "https://dashboard.contoso.com/john", "Wiki": "https://wiki.contoso.com/john"}
6dd74de1039 Alice->>John: Hello John, how are you?
6dd74de1040 John-->>Alice: Great!
6dd74de1041 Alice-)John: See you later!
6dd74de1042```
6dd74de1043
6dd74de1044## Styling
6dd74de1045
6dd74de1046Styling of a sequence diagram is done by defining a number of css classes. During rendering these classes are extracted from the file located at src/themes/sequence.scss
6dd74de1047
6dd74de1048### Classes used
6dd74de1049
6dd74de1050| Class | Description |
6dd74de1051| -------------- | -------------------------------------------------------------- |
6dd74de1052| actor | Styles for the actor box. |
6dd74de1053| actor-top | Styles for the actor figure/ box at the top of the diagram. |
6dd74de1054| actor-bottom | Styles for the actor figure/ box at the bottom of the diagram. |
6dd74de1055| text.actor | Styles for text of all of the actors. |
6dd74de1056| text.actor-box | Styles for text of the actor box. |
6dd74de1057| text.actor-man | Styles for text of the actor figure. |
6dd74de1058| actor-line | The vertical line for an actor. |
6dd74de1059| messageLine0 | Styles for the solid message line. |
6dd74de1060| messageLine1 | Styles for the dotted message line. |
6dd74de1061| messageText | Defines styles for the text on the message arrows. |
6dd74de1062| labelBox | Defines styles label to left in a loop. |
6dd74de1063| labelText | Styles for the text in label for loops. |
6dd74de1064| loopText | Styles for the text in the loop box. |
6dd74de1065| loopLine | Defines styles for the lines in the loop box. |
6dd74de1066| note | Styles for the note box. |
6dd74de1067| noteText | Styles for the text on in the note boxes. |
6dd74de1068
6dd74de1069### Sample stylesheet
6dd74de1070
6dd74de1071```css
6dd74de1072body {
6dd74de1073 background: white;
6dd74de1074}
6dd74de1075
6dd74de1076.actor {
6dd74de1077 stroke: #ccccff;
6dd74de1078 fill: #ececff;
6dd74de1079}
6dd74de1080text.actor {
6dd74de1081 fill: black;
6dd74de1082 stroke: none;
6dd74de1083 font-family: Helvetica;
6dd74de1084}
6dd74de1085
6dd74de1086.actor-line {
6dd74de1087 stroke: grey;
6dd74de1088}
6dd74de1089
6dd74de1090.messageLine0 {
6dd74de1091 stroke-width: 1.5;
6dd74de1092 stroke-dasharray: '2 2';
6dd74de1093 marker-end: 'url(#arrowhead)';
6dd74de1094 stroke: black;
6dd74de1095}
6dd74de1096
6dd74de1097.messageLine1 {
6dd74de1098 stroke-width: 1.5;
6dd74de1099 stroke-dasharray: '2 2';
6dd74de1100 stroke: black;
6dd74de1101}
6dd74de1102
6dd74de1103#arrowhead {
6dd74de1104 fill: black;
6dd74de1105}
6dd74de1106
6dd74de1107.messageText {
6dd74de1108 fill: black;
6dd74de1109 stroke: none;
6dd74de1110 font-family: 'trebuchet ms', verdana, arial;
6dd74de1111 font-size: 14px;
6dd74de1112}
6dd74de1113
6dd74de1114.labelBox {
6dd74de1115 stroke: #ccccff;
6dd74de1116 fill: #ececff;
6dd74de1117}
6dd74de1118
6dd74de1119.labelText {
6dd74de1120 fill: black;
6dd74de1121 stroke: none;
6dd74de1122 font-family: 'trebuchet ms', verdana, arial;
6dd74de1123}
6dd74de1124
6dd74de1125.loopText {
6dd74de1126 fill: black;
6dd74de1127 stroke: none;
6dd74de1128 font-family: 'trebuchet ms', verdana, arial;
6dd74de1129}
6dd74de1130
6dd74de1131.loopLine {
6dd74de1132 stroke-width: 2;
6dd74de1133 stroke-dasharray: '2 2';
6dd74de1134 marker-end: 'url(#arrowhead)';
6dd74de1135 stroke: #ccccff;
6dd74de1136}
6dd74de1137
6dd74de1138.note {
6dd74de1139 stroke: #decc93;
6dd74de1140 fill: #fff5ad;
6dd74de1141}
6dd74de1142
6dd74de1143.noteText {
6dd74de1144 fill: black;
6dd74de1145 stroke: none;
6dd74de1146 font-family: 'trebuchet ms', verdana, arial;
6dd74de1147 font-size: 14px;
6dd74de1148}
6dd74de1149```
6dd74de1150
6dd74de1151## Configuration
6dd74de1152
6dd74de1153It is possible to adjust the margins for rendering the sequence diagram.
6dd74de1154
6dd74de1155This is done by defining `mermaid.sequenceConfig` or by the CLI to use a json file with the configuration.
6dd74de1156How to use the CLI is described in the [mermaidCLI](../config/mermaidCLI.md) page.
6dd74de1157`mermaid.sequenceConfig` can be set to a JSON string with config parameters or the corresponding object.
6dd74de1158
6dd74de1159```javascript
6dd74de1160mermaid.sequenceConfig = {
6dd74de1161 diagramMarginX: 50,
6dd74de1162 diagramMarginY: 10,
6dd74de1163 boxTextMargin: 5,
6dd74de1164 noteMargin: 10,
6dd74de1165 messageMargin: 35,
6dd74de1166 mirrorActors: true,
6dd74de1167};
6dd74de1168```
6dd74de1169
6dd74de1170### Possible configuration parameters:
6dd74de1171
6dd74de1172| Parameter | Description | Default value |
6dd74de1173| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------ |
6dd74de1174| mirrorActors | Turns on/off the rendering of actors below the diagram as well as above it | false |
6dd74de1175| bottomMarginAdj | Adjusts how far down the graph ended. Wide borders styles with css could generate unwanted clipping which is why this config param exists. | 1 |
6dd74de1176| actorFontSize | Sets the font size for the actor's description | 14 |
6dd74de1177| actorFontFamily | Sets the font family for the actor's description | "Open Sans", sans-serif |
6dd74de1178| actorFontWeight | Sets the font weight for the actor's description | "Open Sans", sans-serif |
6dd74de1179| noteFontSize | Sets the font size for actor-attached notes | 14 |
6dd74de1180| noteFontFamily | Sets the font family for actor-attached notes | "trebuchet ms", verdana, arial |
6dd74de1181| noteFontWeight | Sets the font weight for actor-attached notes | "trebuchet ms", verdana, arial |
6dd74de1182| noteAlign | Sets the text alignment for text in actor-attached notes | center |
6dd74de1183| messageFontSize | Sets the font size for actor<->actor messages | 16 |
6dd74de1184| messageFontFamily | Sets the font family for actor<->actor messages | "trebuchet ms", verdana, arial |
6dd74de1185| messageFontWeight | Sets the font weight for actor<->actor messages | "trebuchet ms", verdana, arial |