D2 is a modern diagram scripting language that turns text to diagrams. https://d2lang.com
Find a file
Alexander Wang 93f97201a9
Merge pull request #2674 from ocadaruma/fix-legend-width-on-mono
svg: Text may overflow legend bounds when monospace font is used
2025-10-14 11:57:28 -06:00
.github use newer wasm-opt 2025-08-18 10:44:01 -06:00
ci Text may overflow legend bounds when monospace font is used 2025-10-13 08:46:49 +09:00
cmd/d2plugin-dagre default layouts 2022-12-30 13:36:49 -08:00
d2ast tooltip with near 2025-07-17 14:42:37 -06:00
d2chaos Add MonoFontFamily support for customizing monospace fonts 2025-08-03 09:30:03 -06:00
d2cli default animate-interval 2025-10-01 10:37:21 -06:00
d2compiler legend: set label 2025-08-01 09:23:09 -06:00
d2exporter Add MonoFontFamily support for customizing monospace fonts 2025-08-03 09:30:03 -06:00
d2format d2fmt: preserve order of boards 2025-08-01 09:01:15 -06:00
d2graph Add MonoFontFamily support for customizing monospace fonts 2025-08-03 09:30:03 -06:00
d2ir d2ir: allow labels for scenarios/steps 2025-08-01 08:49:36 -06:00
d2js Update README.md 2025-08-18 10:38:53 -06:00
d2layouts fix test 2025-09-23 19:55:47 -06:00
d2lib Allow custom fonts to override HandDrawn font in sketch mode 2025-08-03 10:11:31 -06:00
d2lsp d2lsp: let getboardatposition parse broken ast 2025-07-09 15:45:19 -06:00
d2oracle d2oracle: error for board adding 2025-08-24 06:47:33 -06:00
d2parser d2parser: impose max key length 2025-03-25 10:07:05 -06:00
d2plugin fix plugin --help 2025-07-29 14:45:34 -06:00
d2renderers Text may overflow legend bounds when monospace font is used 2025-10-13 08:46:49 +09:00
d2target Add MonoFontFamily support for customizing monospace fonts 2025-08-03 09:30:03 -06:00
d2themes c4 theme 2025-03-03 18:33:00 -07:00
docs Add MonoFontFamily support for customizing monospace fonts 2025-08-03 09:30:03 -06:00
e2etests Text may overflow legend bounds when monospace font is used 2025-10-13 08:46:49 +09:00
e2etests-cli fix png image loading 2025-10-01 10:12:51 -06:00
lib fix chromium prompt 2025-10-01 10:22:17 -06:00
testdata d2oracle: error for board adding 2025-08-24 06:47:33 -06:00
.gitattributes .gitattributes: Add testdata 2022-11-28 15:47:31 -08:00
.gitignore add claude- prefix to gitignore for notes 2025-06-23 09:15:23 -06:00
.gitmodules oss 2022-11-03 06:54:49 -07:00
.pre-commit-hooks.yaml feat: add d2 pre-commit hook 2025-06-03 12:39:09 -03:00
.prettierignore minified browser build 2025-01-14 11:39:18 -07:00
go.mod concurrent gif generation 2025-09-30 13:37:24 -06:00
go.sum concurrent gif generation 2025-09-30 13:37:24 -06:00
install.sh consistent version format 2025-08-01 11:24:02 -06:00
LICENSE.txt oss 2022-11-03 06:54:49 -07:00
main.go d2cli: Move main code into d2cli so it can be imported from tests 2023-02-27 15:04:02 -08:00
make.sh cleanup stage 2025-01-13 11:03:30 -07:00
Makefile remove logs 2025-03-24 13:38:37 -06:00
package-lock.json logs 2025-08-16 15:58:21 -06:00
README.md update d2studio backlink 2025-09-13 18:05:37 -06:00

Table of Contents

What does D2 look like?

vars: {
  d2-config: {
    layout-engine: elk
    # Terminal theme code
    theme-id: 300
  }
}
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

D2 render example

Open in playground

For more examples, see ./docs/examples.

Quickstart

The most convenient way to use D2 is to just run it as a CLI executable to produce SVGs from .d2 files.

# First, install D2
curl -fsSL https://d2lang.com/install.sh | sh -s --

echo 'x -> y -> z' > in.d2
d2 --watch in.d2 out.svg

A browser window will open with out.svg and live-reload on changes to in.d2.

Install

The easiest way to install is with our install script:

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

You can run the install script with --dry-run to see the commands that will be used to install without executing them.

Or if you have Go installed you can install from source though you won't get the manpage:

go install oss.terrastruct.com/d2@latest

You can also install a release from source which will include manpages. See ./docs/INSTALL.md#source-release.

To uninstall with the install script:

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

For detailed installation docs, see ./docs/INSTALL.md. We demonstrate alternative methods and examples for each OS.

As well, the functioning of the install script is described in detail to alleviate any concern of its use. We recommend using your OS's package manager directly instead for improved security but the install script is by no means insecure.

D2 as a library

In addition to being a runnable CLI tool, D2 can also be used to produce diagrams from Go programs.

For examples, see ./docs/examples/lib. This blog post also demos a complete, runnable example of using D2 as a library for a real-world use case.

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 currently supports SVG, PNG and PDF exports. More coming soon.

Language tooling

D2 is designed with language tooling in mind. D2's parser can parse multiple errors from a broken program, has an autoformatter, syntax highlighting, and we have plans for LSP's and more. Good language tooling is necessary for creating and maintaining large diagrams.

The extensions for VSCode and Vim can be found in the Related section.

Plugins

D2 is designed to be extensible and composable. The plugin system allows you to change out layout engines and customize the rendering pipeline. Plugins can either be bundled with the build or separately installed as a standalone binary.

Layout engines:

  • dagre (default, bundled): A fast, directed graph layout engine that produces layered/hierarchical layouts. Based on Graphviz's DOT algorithm.
  • ELK (bundled): A directed graph layout engine particularly suited for node-link diagrams with an inherent direction and ports.
  • TALA (binary): Novel layout engine designed specifically for software architecture diagrams. Requires separate install, visit the Github page for more.

D2 intends to integrate with a variety of layout engines, e.g. dot, as well as single-purpose layout types like sequence diagrams. You can choose whichever layout engine you like and works best for the diagram you're making.

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.

License

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

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 plugins

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.

Notable open-source projects documenting with D2

Do you have or see an open-source project with .d2 files? Please submit a PR adding to this selected list of featured projects using D2.