Drawing a diagram in your program
A diagram’s drawing data (GET /v1/diagrams/{diagramId}/drawing) holds everything the diagram shows. Farkitect’s own drawing code turns it into the picture, exactly as the app draws it, wherever your program runs:
| You’re writing | Use | You get |
|---|---|---|
| A web page (any framework, or none) | The drawing package, @farkitect/draw |
The diagram on the page, or a PNG or SVG |
| A program in any language (Python, Rust, C#, a shell script…) | The command-line tool, farkitect-render |
PNG or SVG files |
Nothing is drawn on Farkitect’s servers: the picture is made where your program runs, from data it has just read, so it is always current.
The command-line tool
Section titled “The command-line tool”farkitect-render needs Node.js 22 or later and Chrome or Chromium on the machine (it finds Chrome in the usual places; --browser <path> gives another).
npm install -g @farkitect/renderDraw a diagram straight from the API with your key:
export FARKITECT_API_KEY=fk_live_…farkitect-render --diagram jh74365kw4gsnrxjk3d5h8pymn8gnt8j -o stakeholders.png --theme darkOr from drawing data your program has already read and saved:
farkitect-render landscape.json -o landscape.pngfarkitect-render *.json --out-dir pictures --format svgcat landscape.json | farkitect-render - -o landscape.png| Option | Does |
|---|---|
-o, --output <file> |
The picture’s file (one diagram). Its extension sets the format |
--out-dir <dir> |
Where pictures go when there are several (default: beside each drawing file) |
--theme light|dark |
Default light |
--format png|svg |
Default png |
--scale <n> |
Pixels per diagram unit, default 2 (as Share › Export) |
--background theme|transparent |
The theme’s canvas colour (default), or none |
--locale <tag> |
How numbers and dates in values are written, default en-US |
--diagram <id> |
Fetch this diagram’s drawing with FARKITECT_API_KEY (repeatable) |
--api-url <url> |
Default https://api.farkitect.com |
--browser <path> |
The Chrome or Chromium to draw with |
--json |
One line of JSON per picture on standard output: { ok, input, output, width, height, ms } |
Several diagrams in one run share one browser, so a batch is much quicker than one run each. The tool exits with 0 when every picture was made, 1 when any failed (each failure is on standard error), and 2 for a usage error.
Image shapes are drawn with their pictures when the tool has your key (FARKITECT_API_KEY): it fetches each from the shape’s attachment.href. Without a key they’re drawn as their frame.
From Python
Section titled “From Python”import json, os, subprocess, urllib.request
API = "https://api.farkitect.com"KEY = os.environ["FARKITECT_API_KEY"]
def get(path): request = urllib.request.Request(API + path, headers={"Authorization": f"Bearer {KEY}"}) with urllib.request.urlopen(request) as response: return json.load(response)
# Every diagram in a project, drawn in one run.views = get("/v1/projects/PROJECT_ID/views?kind=diagram")["data"]ids = [arg for view in views for arg in ("--diagram", view["id"])]subprocess.run(["farkitect-render", *ids, "--out-dir", "pictures", "--json"], check=True, env={**os.environ, "FARKITECT_API_KEY": KEY})To redraw only what changed, keep each drawing’s version and send it back in If-None-Match: an unchanged diagram answers 304, and there’s nothing to redraw.
The drawing package
Section titled “The drawing package”For web pages: one self-contained module, with nothing for your page to provide (it brings its own React and styles, and works with any framework or none). It draws inside a shadow root, so your page’s styles never change the diagram and its styles never reach your page.
npm install @farkitect/drawimport { drawDiagram, renderDiagram } from "@farkitect/draw";
// Your server reads the drawing data with your key (the API refuses calls from browsers)// and hands it to the page.const drawing = await (await fetch("/my-server/diagrams/landscape")).json();
// On the page: fills the element, which needs a size.const view = drawDiagram(document.getElementById("diagram"), drawing, { theme: "dark", interactive: true, // pan and zoom onSelect: (item) => console.log(item), // a shape, connector or view link clicked});await view.ready;
// Or a picture, without showing anything.const png = await renderDiagram(drawing, { format: "png", scale: 2 });drawDiagram option |
Does |
|---|---|
theme |
light (default) or dark |
locale |
How values are written, default en-US |
interactive |
Pan and zoom with the mouse (default off: the diagram fits its box) |
onSelect(item) |
A shape (shapeId, elementId), connector (connectorId, relationshipId) or view link (linkId, target) was clicked |
loadImage(href) |
Fetches an image shape’s attachment (/v1/elements/{id}/attachment) and returns it as a Blob. Without one, image shapes draw their frame |
The view it returns has update(drawing), setTheme(theme), fit(), toBlob({ format, scale, background }), ready and destroy(). renderDiagram(drawing, options) takes the same options plus format, scale and background, and gives a Blob.
Pages without a build step can use the web component:
<script type="module" src="https://your-site/farkitect-draw.js"></script><farkitect-diagram id="d" theme="dark" interactive style="height: 600px"></farkitect-diagram><script type="module"> document.getElementById("d").drawing = await (await fetch("/my-server/diagrams/landscape")).json();</script>It sends drawn when the diagram is drawn, select (with the item as detail) on a click, and error.
Good to know
Section titled “Good to know”- Read-only. Nothing here changes a model.
- The same as the app. Shapes, colours in both themes, icons, compartments, labels and connectors are drawn by the code the app draws with, so a picture matches Share › Export.
- Fonts. Text is drawn in the machine’s sans-serif font, as the app’s is, so a picture made on another machine may differ slightly in its lettering.
- Formats. This version draws
farkitect-drawing/1and says so when given another format. - Licence. Both are open source under the MIT licence, © Urgnt Limited, so you can use them in your own products, commercial ones included.