@diagrams-so/sdk is a fully typed client that covers every /api/v2 operation with one method per endpoint. It has zero dependencies (it uses the platform fetch), works in Node 18+, browsers, and edge runtimes, and includes built-in retries, idempotency keys, streaming, and a re-layout helper.

Install

Connect

Run this once per machine. A browser opens, you press Approve, and the credential is stored for you at ~/.diagrams-so/credentials.json, shared with the MCP server and the Python SDK:

CI and servers

Where no browser exists, set an environment variable. It always takes priority over a stored login:
Or pass it explicitly. The order of resolution is an explicit apiKey, then DIAGRAMS_API_KEY, then a stored login. Use logout() to remove the stored credential.

Quickstart

Use the SDK server-side. Never ship an API key in browser or client-side code.

Authentication and billing

Pass your key from Settings → AI Provider. Reads, enhancePrompt, and clarifyPrompt are free. generate, edit, fix, relayout, and fork cost credits.
dgz_test_ keys run in test mode. They bill the same credits as a live key (drawing your real balance) but apply lower rate limits, so use them to exercise your integration, not to avoid spend. Check your balance with client.usage().

Create and change diagrams

Streaming generation

generateStream is an async generator that yields { event, data }. The XML arrives only in the terminal complete event, after the charge.

Warnings, re-layout, versions, export

Usage and account

Errors

Every non-2xx response throws DiagramsAPIError with .code, .status, and .requestId.

Idempotency

Pass idempotencyKey on billable create calls (generate, generateStream, edit, fix) so a retry replays the stored result for 24 hours instead of charging twice.

Pagination

list, searchGallery, and versions return Page<T> = { items, next_cursor, has_more }.

Configuration

Full method list

generate, generateStream, list, get, update, delete, edit, fix, warnings, startRelayout, relayoutStatus, relayoutAndWait, export, versions, getVersion, revert, import, searchGallery, fork, enhancePrompt, clarifyPrompt, usage, usageHistory, me, meta. Each maps one-to-one to an endpoint in the API reference.

Running out of credits

A billable call with no credits left throws DiagramsAPIError with code QUOTA_EXCEEDED. The error carries the top-up link, so you can show it to the user: