Beispiele
SSE-Beispiele
Node.js und cURL für Server-Sent Events
Zuletzt aktualisiert am
cURL
curl -N -H "x-api-key: tkn_xxx" \
"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,
"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-Nachricht
Eine SSE-Nachricht (text/event-stream) sieht so aus:
event: message
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}
Die SSE-event-Zeile lautet message. Parsen Sie die data-Zeile als JSON: Darin stehen das Tillor-Event (event), die Payload (data) und timestamp (ms). Dasselbe JSON-Objekt erhalten Sie über Webhooks.
War diese Seite hilfreich?