Tillor
OntwikkelaarsVoorbeelden

SSE-voorbeelden

Node.js en cURL voor Server-Sent Events

cURL

curl -N -H "x-api-key: tkn_xxx" \
     -H "X-Tillor-Org-Id: org_abc123" \
     "https://app.tillor.eu/api/orgs/org_abc123/realtime/subscribe"

Node.js

const orgId = "org_abc123";
const apiKey = "tkn_xxx";

const url = `https://app.tillor.eu/api/orgs/${orgId}/realtime/subscribe?events=invoice:created&events=customer:updated`;

const response = await fetch(url, {
  headers: {
    "x-api-key": apiKey,
    "X-Tillor-Org-Id": orgId,
    "Accept": "text/event-stream",
  },
});

const reader = response.body.getReader();
const decoder = new TextDecoder();

while (true) {
  const { done, value } = await reader.read();
  if (done) break;

  const chunk = decoder.decode(value, { stream: true });
  for (const line of chunk.split("\n")) {
    if (line.startsWith("data: ")) {
      const payload = JSON.parse(line.slice(6));
      console.log(payload.event, payload.data);
    }
  }
}

SSE-bericht

Een SSE-bericht (text/event-stream) ziet er zo uit:

event: invoice:paid
data: {"event":"invoice:paid","data":{"invoice":{"id":"inv_def456uvw","displayId":"2025-00123","status":"BOOKED","paymentStatus":"PAID","amountTotal":200,"paidDate":"2025-03-09T12:15:00.000Z","customerId":"cust_abc123xyz"},"payment":{"id":"pay_ghi789rst","amount":200,"status":"PAID","method":"BANCONTACT","provider":"MOLLIE","paidAt":"2025-03-09T12:15:00.000Z"}},"timestamp":1735811700000}

De data-regel bevat het volledige JSON-object (event + data + timestamp).