TEXT FORMAT
D2 logo

D2

D2 (Terrastruct)

D2 (short for "Declarative Diagramming," and referred to in code as d2lang) is a text-to-diagram scripting language created and maintained by Terrastruct, Inc., which open-sourced it in 2022 under the Mozilla Public License 2.0. It is implemented primarily in Go and ships as a self-contained CLI (with a watch mode for live re-rendering), a Go library API, and an online Playground. A D2 program is a plain-text .d2 file that compiles to a rendered diagram, positioning it in the same "diagram-as-code" niche as Mermaid, PlantUML, and the older DOT from Graphviz.

The data model is a graph of shapes (nodes) and connections (edges). Nodes are declared implicitly by naming them; edges use arrow operators (->, --, <->) between node keys, and both nodes and edges can carry labels and a nested block of styling and metadata attributes (shape, style, icon, tooltip, link, and so on). Hierarchy is first-class: containers group child shapes via dotted key paths or nested braces, so a.b.c addresses a node inside nested parents. Beyond generic node-link graphs, D2 supports specialized constructs including UML class and table shapes, sequence diagrams, SQL-table shapes, grids, and Markdown/LaTeX/code text blocks. Reuse features such as variables, classes (reusable style bundles), imports/@-includes, and glob wildcards let large diagrams stay DRY.

A distinguishing design point is that D2 separates the abstract diagram from its geometry and delegates positioning to pluggable layout engines: the bundled dagre (layered, Graphviz-DOT-style), ELK (ports and orthogonal routing), and Terrastruct's proprietary TALA engine tuned for software-architecture layouts. It renders to SVG, PNG, and PDF, and offers themes, adaptive dark mode, a hand-drawn "sketch" mode, and animation across multi-board diagrams.

D2's strengths are expressive containers, strong styling and theming, multiple layout backends, and a parser that reports multiple errors and includes an autoformatter and AST (d2ast). Its limitations follow from being a rendering-oriented DSL rather than a graph-interchange format: it has no standardized schema for round-tripping arbitrary attributed graphs, TALA is closed-source and licensed, and semantics are entangled with visual presentation, so it is generally a diagram authoring and export target rather than a source for lossless graph data exchange with tools like GraphML or GML.

Alternative Names: d2lang, Declarative Diagramming

File Extensions: .d2 v0.7.1
FeatureD2 (Terrastruct)
Nodes supported
Undirected Edges supported
Directed Edges supported
Parallel Edges supported
Self-loops supported
Nested Graphs in Nodes supported
Node Labels supported
Edge Labels supported
Attributes on Nodes supported
Attributes on Edges supported

Tools(Read & Write)

Read-only Tools

Frequently Asked Questions

What is a D2 (Terrastruct) file?

A D2 (Terrastruct) file stores a graph — its nodes, edges and attributes — in the D2 (Terrastruct) format (also: d2lang, Declarative Diagramming). See the feature table above for what it supports.

How do I open a D2 (Terrastruct) file?

Open it in a graph tool that supports D2 (Terrastruct), or convert it to a format your tool reads. With GraphInOut you can convert D2 (Terrastruct) to GraphML, DOT, Connected JSON and more, right in your browser.

How do I convert a D2 (Terrastruct) file to another format?

Use the Convert from D2 (Terrastruct) link above: upload or paste your D2 (Terrastruct) file (input preset to D2 (Terrastruct)), choose a target format and download the result — free, no install.