Getting Started with Mermaid Diagrams: A Developer's Quick Guide
Last week I was documenting a payment flow for our team. The usual drill - fire up a design tool, drag boxes around, spend 20 minutes getting arrows to align. Then someone suggested Mermaid. Five minutes later, I had the same diagram done with just text.
Here's what that looks like:
graph TD
A[User] --> B[Checkout]
B --> C{Payment Valid?}
C -->|Yes| D[Process Order]
C -->|No| E[Show Error]
That's it. No mouse needed.
What Makes Mermaid Different
Mermaid is a JavaScript library that turns text into diagrams. You write simple syntax, and it renders flowcharts, sequence diagrams, class diagrams, and more. The big win? Your diagrams live in your codebase as plain text.
This means:
- Version control with Git (finally, meaningful diffs for diagrams)
- No separate design files to manage
- Updates are as easy as editing text
- Anyone can contribute without design tools
GitHub, GitLab, Notion, and most documentation platforms render Mermaid natively. Write it once, see it everywhere.
Your First Flowchart
Let's build something. Start with graph TD (top-down direction) or graph LR (left-right):
graph LR
A[Start] --> B[Do Something]
B --> C[End]
Nodes are defined with brackets. Square brackets [text] give you rectangles. Curly braces {text} create diamonds for decisions. Parentheses (text) make rounded rectangles.
graph TD
A[Process Data]
B{Is Valid?}
C(Success)
A --> B
B -->|Yes| C
Sequence Diagrams for API Flows
When documenting how services talk to each other, sequence diagrams are gold:
sequenceDiagram
participant Browser
participant API
participant Database
Browser->>API: POST /login
API->>Database: Query user
Database-->>API: User data
API-->>Browser: JWT token
Solid arrows (->>) show requests. Dashed arrows (-->>) show responses. It reads almost like code comments.
Why Developers Actually Use This
I asked a few teams why they switched from traditional diagramming tools:
Speed: WritingA --> B --> C is faster than clicking through menus. Once you know the syntax, diagrams flow as fast as you can type.
Maintenance: When the code changes, updating a text diagram takes seconds. No hunting for the source file of that PNG someone made two years ago.
Collaboration: Pull request reviews can include diagram changes. "Hey, should this arrow point to the cache instead?" becomes a normal code review comment.
Consistency: Mermaid applies uniform styling automatically. No more debates about box colors or font sizes.
Common Gotchas
A few things that tripped me up initially:
- The word
endis reserved. If you need it as node text, capitalize it:EndorEND - Spaces in node IDs cause issues. Use underscores or camelCase
- Complex diagrams may need the
elkrenderer (available in newer versions) for better layouts
Getting Started Today
The fastest way to try Mermaid is an online editor with live preview. Write your syntax on the left, see the diagram on the right. No setup, no installation.
Once you're comfortable, you can embed Mermaid in:
- Markdown files (GitHub/GitLab render these automatically)
- Documentation sites like Docusaurus or VuePress
- Any web page with the Mermaid.js library
The syntax takes maybe an hour to learn for basic diagrams. For most documentation needs, that's all you'll ever need.
What's Next
Start simple. Next time you need a diagram, try writing it in Mermaid instead of reaching for a drawing tool. The initial learning curve pays off quickly when you realize how much time you save on updates and maintenance.
Your diagrams become part of your documentation, living alongside your code, versioned and reviewable. That's the real value here - not just faster diagram creation, but better documentation practices overall.