Esempi
Esempi SSE
Node.js e cURL per i Server-Sent Events
Ultimo aggiornamento il
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);
}
}
}Messaggio SSE
Un messaggio SSE (text/event-stream) ha questo aspetto:
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}
La riga SSE event è message. Analizza la riga data come JSON: contiene l'evento Tillor (event), il payload (data) e timestamp (ms). Lo stesso oggetto JSON lo ricevi tramite i webhook.
Questa pagina è stata utile?