Unsaved
Ready
Loading Monaco Editor...
Preview
Styling
Sequence Diagram
All diagram types

Mermaid Sequence Diagram Syntax & Examples

Sequence diagrams show who talks to whom, and in what order. Participants are columns. Messages run down the page in time order.

Examples

Copy an example, or open it in the live editor with this diagram already loaded.

Sign-in with an identity provider

A browser, the app, and an identity provider. autonumber prints a step number on each message so a design doc can refer to step 4.

sequenceDiagram
    autonumber
    actor User
    participant App
    participant IdP as Identity provider
    User->>App: Click Sign in
    App->>IdP: Authorization request
    IdP-->>User: Login page
    User->>IdP: Submit credentials
    IdP-->>App: Authorization code
    App->>IdP: Exchange the code
    IdP-->>App: Access token
    App-->>User: Signed-in session

Conditional payment result

alt/else models two outcomes of the same request. The diagram shows both the success path and the decline path.

sequenceDiagram
    actor Shopper
    participant Checkout
    participant Payments
    Shopper->>Checkout: Place order
    Checkout->>Payments: Charge card
    alt Charge succeeds
        Payments-->>Checkout: Approved
        Checkout-->>Shopper: Order confirmed
    else Charge declined
        Payments-->>Checkout: Declined
        Checkout-->>Shopper: Ask for another card
    end

Retry loop around a queue

loop repeats a message until the worker acknowledges it. The note records why the retry exists.

sequenceDiagram
    participant API
    participant Queue
    participant Worker
    API->>Queue: Enqueue job
    loop Until acknowledged
        Queue->>Worker: Deliver job
        Worker-->>Queue: Ack or retry
    end
    Note over API,Queue: The API does not wait for the worker

What is a Sequence Diagram?

A sequence diagram is an interaction timeline. Each participant — a person, a service, or a database — gets a vertical lifeline. Arrows between lifelines are messages, read from top to bottom. A solid arrow (->>) is a call. A dashed arrow (-->>) is a reply. Blocks such as alt, opt, loop, and par wrap messages that are conditional, optional, repeated, or parallel. Use one when the question is how an API, login, or job handshake actually proceeds.

When to use it

Use a sequence diagram for request and response flows: login, webhooks, payment capture, and worker handoffs. Use a flowchart when there is a single actor and the diagram is mostly decisions. Use a class diagram when you need fields and methods instead of messages.

Edit this sequence diagram in the live editor, read the sequence diagram guide, or compare it with Flowchart, Class Diagram and State Diagram.

Common Use Cases

  • •API documentation - Show request/response flows between services
  • •Authentication flows - Visualize login, OAuth, and session handling
  • •Microservices communication - Document service-to-service interactions
  • •User interactions - Map out how users interact with system components
  • •Debugging - Trace message flow to identify issues

Syntax Quick Reference

ElementSyntaxDescription
Participantparticipant NameDeclare an actor or component
Solid arrow->>Synchronous message
Dashed arrow-->>Response or async message
NoteNote over A: TextAdd annotation
Looploop DescriptionRepeating sequence
Altalt DescriptionConditional branch

Syntax tips

  • •Declare participants in the order you want the columns, left to right. participant API as Billing API sets a short id and a longer label.
  • •->> is a solid request. -->> is a dashed reply. The arrow direction is the direction of the message, not the direction of time.
  • •Close every alt, opt, loop, and par with end. else belongs inside alt, not on its own.
  • •activate API and deactivate API draw the execution bar. They are optional; use them when a call stays open across several messages.
  • •autonumber adds step numbers. Turn it off with autonumber off if you only want numbers on part of the diagram.

Common mistakes

Writing a participant name with spaces and no alias.

Use an id, then an alias: participant Pay as Payment service. Refer to Pay on the arrows.

Leaving an alt or loop unclosed.

Every combined fragment needs its own end. Nested blocks need nested end lines, one per block.

Using a flowchart arrow (-->) between participants.

Sequence messages use ->> for a call and -->> for a reply. --> does not create a sequence message.

Export Options

Sequence diagrams get wide as you add participants. Export SVG if the figure will sit in a docs site, and PNG if it will be pasted into a ticket. Free PNG exports include a watermark.

Got Mermaid code from ChatGPT? Paste it here to render and export instantly.

Get the Free Mermaid Syntax Cheat Sheet

Free Mermaid Syntax Cheat Sheet

All 21 diagram types in one PDF reference.

Frequently Asked Questions

Ready to create your own sequence diagram?

Open the editor with this example pre-loaded and start customizing.

Sequence Diagram templates

Related Diagram Types

All diagram types