Webhook-uri
Evenimente semnate, trimise la adresa ta în momentul în care se întâmplă ceva în cont.
Evenimente
| type | Când | data.object |
|---|---|---|
lead.created | un contact devine lead (telefon sau email captat) | lead |
conversation.handoff_requested | clientul cere un operator / agentul escaladează | conversation (+ reason) |
message.received | orice mesaj primit pe Facebook, Instagram, WhatsApp — volum mare | message |
credit.low | creditele scad sub prag (cel mult o dată pe zi) | credit_balance |
webhook.test | butonul Trimite test sau POST /webhook_endpoints/{id}/test | webhook_test |
Configurare
Din panou (Setări → Dezvoltatori → Webhook-uri) sau prin API. Adresa trebuie să fie https:// publică. Secretul de semnare (whsec_…) apare o singură dată.
curl https://aichat.md/api/public/v1/webhook_endpoints \
-H "Authorization: Bearer $AICHAT_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $(uuidgen)" \
-d '{"url": "https://example.com/aichat/webhook", "events": ["lead.created"]}'Ce primești
POST /aichat/webhook HTTP/1.1
Content-Type: application/json
User-Agent: aichat.md-webhooks/1.0
Aichat-Event-Id: evt_7wQ2nT0aR5xK9mBv3LcY8dEf
Aichat-Event-Type: lead.created
Aichat-Signature: t=1764238500,v1=5257a869e7ecebeda32affa62cdca3fa51cad7e77a0e56ff536d0ce8e108d8bd
{
"id": "evt_7wQ2nT0aR5xK9mBv3LcY8dEf",
"object": "event",
"type": "lead.created",
"created": "2026-11-27T10:15:02.000Z",
"api_version": "v1",
"data": { "object": { "id": "lead_MTEy", "object": "lead", "name": "Ana Popescu", "phone": "+37369123456", "status": "new" } }
}Verifică semnătura
Antetul Aichat-Signature conține t (secunde Unix) și v1 = HMAC-SHA256 cu secretul tău peste t + "." + corpul brut. Calculează-l pe corpul exact primit (nu pe JSON-ul reserializat), compară în timp constant și respinge cererile mai vechi de 5 minute.
import crypto from "node:crypto";
import express from "express";
const app = express();
const SECRET = process.env.AICHAT_WEBHOOK_SECRET; // whsec_…
function verify(header, rawBody) {
const parts = Object.groupBy(header.split(","), (p) => p.split("=")[0]);
const t = Number(parts.t?.[0].split("=")[1]);
if (!t || Math.abs(Date.now() / 1000 - t) > 300) return false; // 5 minutes
const expected = crypto.createHmac("sha256", SECRET).update(`${t}.${rawBody}`).digest("hex");
return (parts.v1 ?? []).some((p) => {
const got = Buffer.from(p.split("=")[1] ?? "");
return got.length === expected.length && crypto.timingSafeEqual(got, Buffer.from(expected));
});
}
// Keep the RAW body: the signature covers the exact bytes we sent.
app.post("/aichat/webhook", express.raw({ type: "application/json" }), (req, res) => {
if (!verify(req.get("Aichat-Signature") || "", req.body.toString("utf8"))) return res.sendStatus(400);
const event = JSON.parse(req.body);
res.sendStatus(200); // answer fast, then process
queue.add(event); // dedupe on event.id
});După o rotire a secretului, timp de 24 de ore antetul are două valori v1 (secret nou și vechi): acceptă cererea dacă oricare se potrivește.
Livrare și reîncercări
- Răspunde cu orice
2xxîn cel mult 10 secunde. Procesează după ce ai răspuns (o coadă). - Orice alt răspuns sau timeout se reîncearcă: după 1 min, 5 min, 30 min, 2 h, 6 h, 12 h și 24 h (8 încercări în ~2 zile).
- Ordinea nu e garantată, iar rar un eveniment poate veni de două ori: deduplică după
id(evt_…). - Dacă un webhook eșuează continuu 3 zile, îl oprim (stare „Oprit după erori”). Îl pornești din panou sau cu
PATCH{"status": "enabled"}. - Ultimele livrări (status, cod HTTP, încercări) se văd în panou și la
GET /webhook_endpoints/{id}/deliveries.
Fără server: polling
GET /events întoarce evenimentele din ultimele 30 de zile, cele mai noi primele. E util pentru declanșatoare de tip „polling” și pentru a recupera ce ai pierdut.
curl "https://aichat.md/api/public/v1/events?type=lead.created&created_after=2026-11-27T10:00:00Z" \
-H "Authorization: Bearer $AICHAT_API_KEY"