curl https://app.diagramkit.ai/api/v1/diagrams \
-H "Authorization: Bearer $DIAGRAMKIT_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: book-7-figure-3.2" \
-d '{"prompt": "The water cycle for a grade 7 science textbook: evaporation, condensation, precipitation and collection, with the sun driving it", "wait": 180}'
curl "https://app.diagramkit.ai/api/v1/diagrams/DIAGRAM_ID/image.png?quality=print&width_in=3.25" \
-H "Authorization: Bearer $DIAGRAMKIT_KEY" -o figure-3-2.png
Full examples
Make a diagram, wait for it, and save it, in JavaScript:// Node 18 or later. Run on your server: the key must stay secret.
import { writeFile } from 'node:fs/promises'
const BASE = 'https://app.diagramkit.ai/api/v1'
const headers = { Authorization: 'Bearer ' + process.env.DIAGRAMKIT_KEY, 'Content-Type': 'application/json' }
async function call(path, init = {}) {
const res = await fetch(BASE + path, { ...init, headers: { ...headers, ...init.headers }, signal: AbortSignal.timeout(330_000) })
if (!res.ok) throw new Error((await res.json()).error.message)
return res
}
// 1. Make it, waiting up to three minutes.
let job = await (await call('/diagrams', {
method: 'POST',
headers: { 'Idempotency-Key': 'book-7-figure-3.2' },
body: JSON.stringify({ prompt: 'The water cycle for a grade 7 science textbook', wait: 180 }),
})).json()
// 2. Not done yet? Keep waiting, 60 seconds at a time.
while (job.status === 'queued' || job.status === 'running') {
job = await (await call('/diagrams/' + job.id + '?wait=60')).json()
}
if (job.status === 'failed') throw new Error(job.error.message)
console.log(job.title, '-', job.description)
// 3. Download it for print, one column wide.
const png = await call('/diagrams/' + job.id + '/image.png?quality=print&width_in=3.25')
await writeFile('figure-3-2.png', Buffer.from(await png.arrayBuffer()))
# Python 3 with requests. Run on your server: the key must stay secret.
import base64, os, requests
BASE = "https://app.diagramkit.ai/api/v1"
HEADERS = {"Authorization": "Bearer " + os.environ["DIAGRAMKIT_KEY"]}
# Work from a sketch: images are base64 without a data: prefix.
with open("sketch.png", "rb") as f:
sketch = base64.b64encode(f.read()).decode()
job = requests.post(f"{BASE}/diagrams", headers={**HEADERS, "Idempotency-Key": "ch4-fig2"}, timeout=330, json={
"prompt": "Redraw this sketch of the heart's four chambers, labeled, for a nursing course",
"images": [{"name": "sketch.png", "mime": "image/png", "data_base64": sketch}],
"wait": 180,
}).json()
while job["status"] in ("queued", "running"):
job = requests.get(f"{BASE}/diagrams/{job['id']}", headers=HEADERS, params={"wait": 60}, timeout=90).json()
if job["status"] == "failed":
raise RuntimeError(job["error"]["message"])
svg = requests.get(f"{BASE}/diagrams/{job['id']}/image.svg", headers=HEADERS, timeout=120)
svg.raise_for_status()
open("ch4-fig2.svg", "wb").write(svg.content)