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

Mermaid C4 Diagram Syntax & Context Examples

C4 diagrams describe software at three zooms: the system in its world, the containers inside it, and the components inside a container.

Examples

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

System context

One system, the person who uses it, and the external systems it depends on. This is the diagram to show someone outside the team.

C4Context
    title Checkout context
    Person(shopper, "Shopper", "Pays for an order")
    System(shop, "Shop", "Takes the order and the payment")
    System_Ext(pay, "Card processor", "Authorizes charges")
    System_Ext(mail, "Email", "Sends receipts")
    Rel(shopper, shop, "Places an order")
    Rel(shop, pay, "Charges the card", "HTTPS")
    Rel(shop, mail, "Sends a receipt", "SMTP")

Containers inside the shop

A container is something you deploy. ContainerDb marks the database. The boundary groups the containers you own.

C4Container
    title Shop containers
    Person(shopper, "Shopper", "Uses the shop")
    Container_Boundary(shop, "Shop") {
        Container(web, "Web app", "Next.js", "Renders the cart")
        Container(api, "API", "Node", "Creates orders")
        ContainerDb(db, "Orders", "Postgres", "Stores orders")
    }
    Rel(shopper, web, "Uses")
    Rel(web, api, "Calls", "JSON")
    Rel(api, db, "Reads and writes", "SQL")

Components inside the API

Zoom one level further. Components live inside a single container and are not separately deployed.

C4Component
    title API components
    Container_Boundary(api, "API") {
        Component(auth, "Auth", "TypeScript", "Checks the session")
        Component(orders, "Orders", "TypeScript", "Creates the order")
        ComponentDb(store, "Order store", "SQL", "Persists rows")
    }
    Rel(auth, orders, "Allows the call")
    Rel(orders, store, "Writes")

What is a C4 Diagram?

Mermaid draws C4 diagrams with a different header per zoom. C4Context shows people and systems. C4Container shows the deployable pieces inside a system boundary. C4Component shows the parts inside one container. Person, System, System_Ext, Container, ContainerDb, and Component each take an id, a name, and a short description. Rel draws a labeled link. The diagram is an architecture explanation, so the technology string belongs in the container or component, not in a separate legend.

When to use it

Use C4 when an architecture review needs a consistent zoom level. Use a flowchart for a process. Use the architecture diagram when you want infrastructure icons — servers, disks, databases — more than C4's person/system boxes.

Edit this c4 diagram in the live editor, read the Mermaid cheat sheet, or compare it with Flowchart, Sequence Diagram and Class Diagram.

Common Use Cases

  • •Architecture documentation - Show system design at multiple abstraction levels
  • •System context - Visualize how your system interacts with users and external systems
  • •Container architecture - Document deployment units and their communication
  • •Component design - Show internal structure of services and applications
  • •Technical communication - Share architecture with stakeholders at appropriate detail levels

Syntax Quick Reference

ElementSyntaxDescription
PersonPerson(id, "Name", "Desc")A user of the system
SystemSystem(id, "Name", "Desc")Your software system
External systemSystem_Ext(id, "Name")Third-party system
RelationshipRel(from, to, "Label")Connection between elements
ContainerContainer(id, "Name", "Tech")Deployment unit (in C4Container)
BoundarySystem_Boundary(id, "Label") { }Group related elements

Syntax tips

  • •Pick the header for the zoom: C4Context, C4Container, or C4Component. Do not mix container elements into a context diagram.
  • •Ids are short tokens. The second argument is the visible name, and later arguments are type and description depending on the element.
  • •System_Ext is outside your boundary. System is the software you are describing.
  • •Wrap owned containers in Container_Boundary(id, "Name") { }. Relationships can cross the boundary.
  • •Rel(from, to, "Label", "Tech") draws the arrow. The technology string is optional.

Common mistakes

Putting containers in a C4Context diagram.

Context shows people and whole systems. Move web apps and databases into a C4Container diagram.

Reusing an id for two elements.

Every Person, System, Container, and Component needs its own id. Rel refers to those ids.

Writing a deploy pipeline as a C4 diagram.

C4 describes structure. A pipeline is a flowchart. A server-and-disk picture may fit the architecture diagram better.

Export Options

C4 diagrams go in architecture docs. SVG keeps the descriptions readable. 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 c4 diagram?

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

C4 Diagram templates

Related Diagram Types

All diagram types