Diagram as code (also written diagrams as code) is the practice of defining a diagram in a text based language that a tool renders into a picture. Because the source is plain text, the diagram can be stored in version control, reviewed in a pull request and updated together with the software it describes.
How diagram as code works
The author writes a short description of the elements and their relationships. A renderer reads the text, chooses positions for the shapes and draws the result. The author controls what is in the diagram and leaves most of the layout to the tool.
A small example in Mermaid syntax:
flowchart LR
Client --> API
API --> Database
API --> Cache
This produces four boxes connected by three arrows, laid out from left to right.
Common diagram as code tools
Mermaid. A widely used syntax that renders in many documentation tools. See Mermaid diagram.
PlantUML. A long established tool with strong support for UML.
Graphviz. Uses the DOT language for graphs and networks.
D2 and Structurizr. Newer tools aimed at software architecture, the latter built around the C4 model.
Benefits
Version history. Every change is a readable difference in text.
Review. Diagrams go through the same review as code.
Consistency. Layout and style are applied by the tool, so diagrams look alike.
Automation. Diagrams can be generated from code, configuration or an AI model.
Limitations
Layout control. Automatic layout rarely matches what a person would draw for a large or dense diagram.
Audience. People outside engineering are unlikely to edit a text syntax.
Exploration. Sketching options in a workshop is faster on a canvas than in a text file.
For these reasons many teams use both: code for reference diagrams that live with the repository, and a visual board for design discussions.
Diagram as code vs text to diagram
Text to diagram is the wider idea of creating a diagram from any written input, including a plain sentence interpreted by AI. Diagram as code is the stricter form, in which the text follows a defined syntax and gives the same result every time.
Example
A platform team keeps a sequence diagram of its login flow as a Mermaid file beside the service code. When a developer adds a token refresh step, the same pull request changes the code and the diagram, and the reviewer sees both.
Diagram as code in MockFlow
MockFlow offers a visual route to the same diagrams. The Mermaid diagram editor artifact brings Mermaid onto a board, and coding tools such as Claude Code, Cursor and VS Code can create editable boards through the MockFlow MCP server, so a diagram described in the terminal becomes one the whole team can open. See whiteboard for Claude Code and whiteboard for developers.
