Skip to main content

Python SDK for the Diagrams.so public API — generate, edit, and manage cloud architecture diagrams with AI.

Project description

diagrams-so — Python SDK

A thin, typed, dependency-free client for the Diagrams.so public API (/api/v2). Generate, edit, and manage cloud-architecture diagrams (draw.io / SVG) with AI.

  • Covers all 27 /api/v2 operations · one method per endpoint
  • No hard dependencies — uses requests if installed, else stdlib urllib
  • Typed, ships py.typed · raises a single DiagramsAPIError with code / status / request_id
  • Safe billing: every billable call auto-attaches an Idempotency-Key and retries ambiguous failures (timeout / 5xx / in-progress) with the same key, so a response lost to a gateway timeout is replayed — one charge, never two
  • Built-in 429/503 backoff (honors Retry-After), SSE streaming, async re-layout helper, and an in-process credit tally (session_charges)

Install

pip install diagrams-so

(Uses requests if present, otherwise the stdlib urllib — no hard dependency.)

Quickstart

from diagrams_so import DiagramsClient

client = DiagramsClient(api_key="dgz_live_…")   # or dgz_test_… (test mode — bills the same credits)

d = client.generate("AWS 3-tier web app: ALB, EC2, RDS", cloud_provider="aws")
print(d["id"], d["score"]["score"], len(d["warnings"]))

# fix the first Well-Architected warning
w = client.warnings(d["id"])
if w:
    client.fix(d["id"], w[0]["message"], component=w[0].get("component"), warning_type=w[0]["type"])

open("diagram.drawio", "w").write(client.export(d["id"], "drawio"))

Authentication & billing

Pass your key (from the API Keys page in your account). dgz_live_ keys bill credits for generate/edit/fix/relayout/fork; dgz_test_ keys are test mode — they bill the same credits (drawing your real balance, like a live key), at lower test rate limits. Reads and enhance/clarify are free. Check balance with client.usage().

Errors

Every non-2xx raises DiagramsAPIError:

from diagrams_so import DiagramsAPIError
try:
    client.generate("…")
except DiagramsAPIError as e:
    print(e.code, e.status, e.request_id)   # e.g. QUOTA_EXCEEDED 402 req_abc
    if e.status == 402:
        ...  # out of credits → send the user to upgrade

Idempotency (safe retries on billable ops)

Every billable call (generate, edit, fix, relayout) auto-attaches a fresh Idempotency-Key and retries ambiguous failures with that same key, so you never double-charge on a timeout. Pass your own key to make the safety window explicit or to dedupe across processes:

client.generate("…", idempotency_key="order-42")   # server replays the stored result for 24h

Definite rejections (401/402/404/422) are never retried; a call whose outcome is lost is recorded in session_charges as status="unknown" — reconcile with client.usage_history(). Streamed generations tally a confirmed charge on their terminal event; an applied re-layout tallies unknown (its credits bill asynchronously — the exact amount is in usage_history).

Streaming

for event, data in client.generate_stream("AWS event-driven pipeline"):
    if event == "progress":
        print(data["progress"], data["message"])
    elif event == "complete":
        print(data["id"], data["usage"]["credits_charged"])
    elif event == "error":
        raise RuntimeError(data["error"]["message"])

The diagram XML arrives only in the terminal complete event (after the charge).

Async re-layout

Re-layout is token-billed on every run (no free allowance) and charged only on delivery of the re-laid diagram. The first call returns confirmation_required; re-call with confirm=True to accept the charge:

job = client.relayout_and_wait(d["id"])           # starts + polls to completion
if job.get("status") == "confirmation_required":  # re-layout always needs confirmation
    job = client.relayout_and_wait(d["id"], confirm=True)

Pagination

List / gallery / versions return {items, next_cursor, has_more}:

cursor = None
while True:
    page = client.list(limit=50, cursor=cursor)
    for item in page["items"]:
        ...
    if not page.get("has_more"):
        break
    cursor = page["next_cursor"]

Config, retries & timeouts

DiagramsClient(api_key, base_url="https://api.diagrams.so/api/v2",
               timeout=450.0, max_retries=3, backoff=0.5,
               retry_delays=(5.0, 15.0, 30.0), retry_budget=600.0)

timeout defaults to 450s, above the server-side timeout ladder, so the client never aborts work the server would still deliver. Reads retry 429/503 (honoring Retry-After). Billable calls retry 429 the same way and every ambiguous failure (timeout / 502/503/504 / in-progress) through the same-key idempotent ladder (retry_delays between attempts, capped at retry_budget seconds) — a single retry layer, so a busy server is never poked twice. Point base_url at http://localhost:8000/api/v2 for local development.

Full method list

generate · generate_stream · list · get · update · delete · edit · fix · warnings · relayout · relayout_status · relayout_and_wait · export · versions · get_version · revert · import_diagram · search_gallery · fork · enhance_prompt · clarify_prompt · usage · usage_history · iter_usage_history · me · meta — each maps 1:1 to an endpoint.

License

Apache-2.0 · docs at diagrams.so/developers

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

diagrams_so-1.1.0.tar.gz (19.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

diagrams_so-1.1.0-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file diagrams_so-1.1.0.tar.gz.

File metadata

  • Download URL: diagrams_so-1.1.0.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for diagrams_so-1.1.0.tar.gz
Algorithm Hash digest
SHA256 b2be07ae2011c471f901ca09d8bd1cc14bed60f276c954d2014e2c4cbc75e313
MD5 6fdd4715777442f3cf70025ff2809f96
BLAKE2b-256 afaca8df2b16c65a49f1503b6d9336242ef4534234970dd0772cb546c1f1ab42

See more details on using hashes here.

File details

Details for the file diagrams_so-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: diagrams_so-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for diagrams_so-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5b014c6a519e35de3ad8e90f4df9020878e2d6d5adcde4c6f110690a70dbf30d
MD5 c7843f53eb61b0f7a4beaa1f5896ce91
BLAKE2b-256 0cf749eb44942cf06bc3cdbe974fc56c7e278e49c82b1db4d62660cd91837bb4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page