Examples

Copy-paste clients for sending events, reading records and receiving webhooks in curl, Python and Node.js.

Environment#

All examples read the .env block you got when the key was created:

JAKUN_URL=https://jakun.dk
JAKUN_TENANT=your-tenant
JAKUN_TOKEN=jk_svc_…
JAKUN_KEY_ID=key_…
JAKUN_SIGNING_SECRET=jk_whsec_…

A small client#

"""jakun_client.py — events, records, MCP. Python 3.10+, httpx."""
import hashlib, hmac, json, os, time
import httpx

URL, TENANT = os.environ["JAKUN_URL"], os.environ["JAKUN_TENANT"]
H = {"Authorization": f"Bearer {os.environ['JAKUN_TOKEN']}"}
http = httpx.Client(base_url=f"{URL}/api/v1", timeout=15)


def event(type_: str, source: str, data: dict, *, id: str) -> dict:
    body = json.dumps({"id": id, "type": type_, "source": source, "data": data}).encode()
    ts = int(time.time())
    sig = hmac.new(os.environ["JAKUN_SIGNING_SECRET"].encode(), f"{ts}.".encode() + body,
                   hashlib.sha256).hexdigest()
    for pause in (0, 10, 60, 300):
        time.sleep(pause)
        try:
            r = http.post(f"/inbound/{TENANT}", content=body, headers={
                "Content-Type": "application/json", "X-JaKun-Key": os.environ["JAKUN_KEY_ID"],
                "X-JaKun-Signature": f"t={ts},v1={sig}"})
        except httpx.TransportError:
            continue
        if r.status_code == 429 or r.status_code >= 500:
            continue
        return r.json()
    raise RuntimeError(f"JaKun unavailable, keep {id!r} and retry later")


def records(obj: str, **params) -> list[dict]:
    out, cursor = [], None
    while True:
        r = http.get(f"/{obj}", headers=H, params={**params, **({"cursor": cursor} if cursor else {})})
        r.raise_for_status()
        page = r.json()
        out += page["data"]
        if not page["has_more"]:
            return out
        cursor = page["next_cursor"]
// jakun.mjs — Node 18+, no dependencies.
import crypto from "node:crypto";
const { JAKUN_URL, JAKUN_TENANT, JAKUN_TOKEN, JAKUN_KEY_ID, JAKUN_SIGNING_SECRET } = process.env;
const api = `${JAKUN_URL}/api/v1`;
const sleep = (s) => new Promise((r) => setTimeout(r, s * 1000));

export async function event(type, source, data, id) {
  const body = JSON.stringify({ id, type, source, data });
  const ts = Math.floor(Date.now() / 1000);
  const sig = crypto.createHmac("sha256", JAKUN_SIGNING_SECRET).update(`${ts}.${body}`).digest("hex");
  for (const pause of [0, 10, 60, 300]) {
    await sleep(pause);
    try {
      const r = await fetch(`${api}/inbound/${JAKUN_TENANT}`, { method: "POST", body, headers: {
        "Content-Type": "application/json", "X-JaKun-Key": JAKUN_KEY_ID,
        "X-JaKun-Signature": `t=${ts},v1=${sig}` } });
      if (r.status === 429 || r.status >= 500) continue;
      return await r.json();
    } catch { /* network: retry */ }
  }
  throw new Error(`JaKun unavailable, keep ${id} and retry later`);
}

export async function records(obj, params = {}) {
  const out = [];
  let cursor;
  do {
    const q = new URLSearchParams({ ...params, ...(cursor ? { cursor } : {}) });
    const r = await fetch(`${api}/${obj}?${q}`, { headers: { Authorization: `Bearer ${JAKUN_TOKEN}` } });
    const page = await r.json();
    if (!r.ok) throw new Error(page.error.code);
    out.push(...page.data);
    cursor = page.has_more ? page.next_cursor : undefined;
  } while (cursor);
  return out;
}

The signature is computed once: a retry sends the same bytes with the same t, which stays valid for 300 seconds. For longer outages sign again before resending — the event id keeps it idempotent.

Typical events#

// virtual_pbx: a missed call that wants a callback
{"id": "call-8812", "type": "contact_event", "source": "virtual_pbx",
 "data": {"channel": "call", "external_id": "call-8812", "intent": "callback_request",
          "text": "Vil gerne have et tilbud på nyt tag", "contact": {"phone": "+4500000201"}}}

// bkg: a site visit was booked
{"id": "B-77", "type": "booking.created", "source": "bkg",
 "data": {"booking_id": "B-77", "starts_at": "2026-11-04T10:00:00+00:00", "duration_min": 60,
          "title": "Besigtigelse bad", "contact": {"phone": "+4500000202", "name": "Demo Bkg"},
          "address_id": "0a3f50a0-…"}}

// jatid: hours on a site
{"id": "T-1", "type": "time_entry.recorded", "source": "jatid",
 "data": {"entry_id": "T-1", "worker": {"ref": "w1", "name": "Demo Medarbejder"},
          "started_at": "2026-10-27T07:00:00+00:00", "ended_at": "2026-10-27T15:30:00+00:00"}}

Webhook receivers#

See Verify the signature for Flask and Express.