Skip to content
d2langPublic

About

D2 is a modern diagram scripting language that turns text to diagrams.

Topics

Resources

Code of conduct

Contributing

Stars

26.0k stars

Watchers

78 watching

Forks

Latest commit

 

History

6,197 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

D2

A modern diagram scripting language that turns text to diagrams.

Docs | Cheat sheet | Comparisons | Playground

ci weekly race tests release changelog npm version discord license

D2 Playground button
cli.mp4

D2 render example

Source code
vars: {
  d2-config: {
    layout-engine: elk
    theme-id: Terminal
  }
}
network: {
  cell tower: {
    satellites: {
      shape: stored_data
      style.multiple: true
    }

    transmitter

    satellites -> transmitter: send
    satellites -> transmitter: send
    satellites -> transmitter: send
  }

  online portal: {
    ui: {shape: hexagon}
  }

  data processor: {
    storage: {
      shape: cylinder
      style.multiple: true
    }
  }

  cell tower.transmitter -> data processor.storage: phone logs
}

user: {
  shape: person
  width: 130
}

user -> network.cell tower: make call
user -> network.online portal.ui: access {
  style.stroke-dash: 3
}

api server -> network.online portal.ui: display
api server -> logs: persist
logs: {shape: page; style.multiple: true}

network.data processor -> api server

Open in Playground · Browse more examples

Trusted by

Organizations around the world use D2 to power blog posts and docs both internally and externally. D2 is also integrated into many popular tools and services.

NVIDIA Google Amazon GitLab Samsung Stripe Alibaba Group LangChain UK Ministry of Justice

Quickstart

On macOS or Linux, run the install script.

curl -fsSL https://d2lang.com/install.sh | sh -s --

On x64 Windows, download the MSI installer from the latest release. The installation guide covers other platforms, package managers, and uninstalling.

Save x -> y -> z in a file named hello.d2, then run the following command to create hello.svg.

d2 hello.d2

With uv, run the native CLI directly with uvx d2lang hello.d2.

For a live preview in your browser, run d2 --watch hello.d2 and edit the file.

D2 as a library

You can render diagrams from Go with d2lib or from JavaScript and TypeScript with @d2lang/d2.

Go examples · D2.js usage guide

Themes

D2 includes a variety of official themes to style your diagrams beautifully right out of the box. See ./d2themes to browse the available themes and make or contribute your own creation.

Fonts

D2 ships with "Source Sans Pro" as the font in renders. If you wish to use a different one, please see ./d2renderers/d2fonts.

Export file types

D2 renders SVG, PNG, GIF, MP4, WebM, PDF, PowerPoint (.pptx), and text diagrams (.txt) without a browser. For example, d2 hello.d2 hello.txt draws the diagram with Unicode characters. Use --ascii-mode=standard for plain ASCII. Export guide

For animated SVG, GIF, MP4, and WebM, set animate-interval and animate-duration in vars.d2-config. With TALA, newly appended SQL rows can drop into place while existing rows and connections stay anchored. Appending a sequence message can extend the activation bar and draw the arrow from sender to recipient. Animated TALA example · D2 source

Animated SVG can reuse a shape, its label, and its artwork across steps and scenarios. When their geometry stays compatible, the browser interpolates movement and supported paint changes at display refresh. TALA preserves fractional positions between authored pins. MP4 and WebM sample motion at 60 fps. For changes that need separate pictures, D2 targets 60 samples per second, up to 288 per transition. GIF fits tween samples to its centisecond clock, using delays of at least 20 ms wherever the authored timing allows.

Scenarios let you compare independent failures on the same architecture. In this example, the Orders API writes each order and its outbox event in one PostgreSQL transaction. During a one-minute Kafka outage, all 120 orders are accepted and their events wait in the outbox. During a PostgreSQL primary outage, all 120 requests receive HTTP 503, while the fulfillment worker processes the 30 events already waiting in Kafka. Animated order-processing architecture · D2 source

Nodes moving between pinned positions, shapes changing size, and grid cells shifting into place use cubic ease-in-out. Adding a grid column moves the neighboring cells aside while the new cells grow and fade into view. Removing a column reverses the gesture. Individual nodes can also grow in or shrink away when the surrounding diagram stays in place. Fading a node or connection to or from zero opacity also eases. Numeric labels and continuous flows along connections keep their own timing. Compare linear and eased CI matrix transitions · D2 source

A positive animate-interval cycles through all exported boards, including linked layers. Link a shape with gateway.link: layers.gateway and set animate-duration to zoom through that shape when playback enters the layer and pull back when it returns to the overview. Moving between linked siblings zooms out to their parent and then into the next layer. Across branches, the camera follows the links through the nearest common ancestor. The whole move uses one transition duration. If a link is missing along the route, playback cuts to the destination. Clicking a board link in a standalone SVG pauses the tour for manual navigation. Order fulfillment example · D2 source

Give a connection a stream of icons with icon.style.animated. For example, gateway -> canary: {icon.style.animated: 0.5/s} sends one dot every two seconds. Higher rates put more dots on the wire at the same travel speed, up to 64 icons in flight for a steady stream. On a wire that takes one second to cross, rates above 64/s display as 64/s. Set the rate to 0/s in a later step to let the last dots arrive, or use true for one continuously looping icon. Set icon.shape to circle (the default), square, triangle, or diamond. Each takes the connection's stroke color. Supply an image with icon to send your own artwork. Labels beside a connection leave room for the icon as it passes. Checkout rollout · Transactional outbox · Replica maintenance

To loop one icon with a fixed trip time, give the duration in whole milliseconds. The icon departs again as soon as it arrives. Each trip keeps its duration across layout engines, even when the wire changes length.

gateway -> cache: {icon.style.animated: 200ms}
gateway -> database: {icon.style.animated: 1500ms}

The cache icon makes five trips per second. The database icon takes a second and a half per trip. On a rate-based connection such as 2/s, D2 launches icons at the requested rate and the wire length determines their trip time. Cache latency · Encoder loop · Worker drain

A single-board GIF with icon.style.animated: 0.25/s automatically loops every four seconds. An explicit loop must last at least the time between departures on each active stream. For a diagram with steps, this requirement applies to the full loop across the exported boards.

To export a VP9 WebM, run d2 tour.d2 tour.webm. For H.264 MP4, run d2 tour.d2 tour.mp4. Both play the selected boards once, sample motion at 60 fps, and store still poses with timestamps. Install FFmpeg 5.1 or newer on your PATH with the encoder for your format (libvpx-vp9 for WebM or libx264 for MP4).

d2 --video-size=1920x1080 --video-quality=medium tour.d2 tour.mp4
d2 --video-duration=10s flow.d2 flow.webm
d2 --stdout-format=webm tour.d2 - > tour.webm

--video-size fits and centers the diagram in an exact pixel canvas, including 3840x2160 for UHD or 1080x1920 for portrait. D2 draws text and shapes at the requested resolution. By default, the canvas follows the diagram dimensions and --scale. The quality options are low, medium, high (default), and best. Lower settings produce smaller files. Use --video-duration to record a continuously animated board or to trim or repeat a tour. Durations round up to the next 1/60 second.

A positive animate-interval shows a progress line along the bottom of an animated SVG, GIF, MP4, or WebM. Hide it with vars.d2-config.animate-progress: false or --animate-progress=false. GIF and video exports that use their built-in interval show the line only if you pass --animate-progress or set animate-progress: true in the config.

The line fills from left to right over the playback time, including holds, and stays fixed during camera moves. Its height is 1% of the shorter canvas dimension, rounded to the nearest pixel and limited to 2–6 pixels, or one pixel on a single-row canvas. The export leaves at least 8 pixels between the diagram's existing padding and the line. MP4 and WebM exports with --video-size fit both within the requested dimensions. The line inverts the RGB color of its background. If the inverse has less than 3:1 contrast, D2 uses whichever of black or white has higher contrast. To choose your own color, set vars.d2-config.animate-progress-color: "#E35B42" or pass --animate-progress-color salmon. Enabling it can increase raster export time and file size during long holds.

To review an animation, run d2 storyboard film.d2 board.png. Open the PNG to see each board and its timestamp in playback order, including intermediate frames from animated transitions. Panels share the animated SVG’s coordinate system, so a shape that moves between boards changes position in the sheet. Run d2 lint --motion film.d2 to find crowded transitions, short-lived labels, hidden caption targets, and possible shape ID replacements. The D2 MCP server exposes the same operations as storyboard and lint_motion, so a model can inspect the image, edit the source, and check the next render.

Layout engines

D2 bundles three layout engines. Select one with --layout=dagre, --layout=elk, or --layout=tala. You can also set D2_LAYOUT or vars.d2-config.layout-engine in D2 source. Run d2 layout to list the engines or d2 layout <name> for their configuration options.

  • Dagro (default, bundled): A native Go port of the Dagre directed graph layout engine that produces layered/hierarchical layouts. Based on Graphviz's DOT algorithm.
  • elk-go (bundled): A native Go port of the ELK directed graph layout engine, particularly suited for node-link diagrams with an inherent direction and ports.
  • TALA (bundled): Native layout and edge-routing engine designed specifically for software architecture diagrams.

D2 also handles specialized layouts such as sequence diagrams and grids. Go applications can use the layout packages directly or provide layout and routing functions through d2lib.

Sketch-mode rendering uses rough-go, a native Go compatibility port of the Rough.js 4.6.6 rendering surface used by D2.

LaTeX labels use mathjax-go, a native Go port of the MathJax 3.2.2 TeX-to-SVG pipeline.

Comparison

For a comparison against other popular text-to-diagram tools, see https://text-to-diagram.com.

Contributing

Contributions are welcome! See ./docs/CONTRIBUTING.md. Parser and editor authors can use the structural grammar, lexical grammar, and conformance cases.

D2 is an independent open-source project fiscally sponsored by Hack Club. Sponsor D2

License

Open sourced under the Mozilla Public License 2.0. See ./LICENSE.txt.

Related

We are constantly working on new plugins, integrations, extensions. Contributions are welcome in any official or community plugins. If you have somewhere in your workflow that you want to use D2, feel free to open a discussion. We have limited bandwidth and usually choose the most high-demand ones to work on. If you make something cool with D2 yourself, let us know and we'll be happy to include it here!

Official plugins

Community integrations

Libraries and publishing

  • Astro D2 — Render D2 code blocks in Astro or remark.
  • MkDocs D2 — Embed D2 diagrams in MkDocs pages.
  • d2-python — Build D2 source from typed Python. Includes a migration guide for py-d2.
  • VitePress D2 — Render D2 code blocks in VitePress.
  • mdBook D2 — Render D2 code blocks in mdBook.

Editors and notes

  • Zed — Add D2 language support in Zed.
  • JetBrains IDEs — Edit, format, and preview D2 diagrams.
  • Emacs Org Babel — Evaluate D2 source blocks in Org documents.
  • Logseq D2 — Render D2 diagrams offline in Logseq using WebAssembly.

Tools with D2 support

Misc

FAQ

  • Does D2 collect telemetry?
    • No, D2 does not use an internet connection after installation, except to check for version updates from Github periodically.
  • Does D2 need a browser to run?
    • No, D2 can run entirely server-side.
  • What's coming in the next release?
  • I have a question or need help.
    • The best way to get help is to ask on D2 Discord
  • I have a feature request, proposal, or bug report.
    • Please open up a Github Issue.
  • I have a private inquiry.

About

D2 is a modern diagram scripting language that turns text to diagrams.

Topics

Resources

Code of conduct

Contributing

Stars

26.0k stars

Watchers

78 watching

Forks

Releases

Packages

Used by

Contributors

Languages