Webhooks
Signed events sent to your URL the moment something happens in the account.
Events
| type | When | data.object |
|---|---|---|
lead.created | a contact becomes a lead (phone or e-mail captured) | lead |
conversation.handoff_requested | the customer asks for a human / the agent escalates | conversation (+ reason) |
message.received | every inbound message on Facebook, Instagram, WhatsApp — high volume | message |
credit.low | credits fall under the threshold (at most once a day) | credit_balance |
webhook.test | Send test or POST /webhook_endpoints/{id}/test | webhook_test |
Setup
From the dashboard (Settings → Developers → Webhooks) or through the API. The URL must be a public https:// address. The signing secret (whsec_…) is shown once.
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"]}'What you receive
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" } }
}Verify the signature
The Aichat-Signature header holds t (Unix seconds) and v1 = HMAC-SHA256 with your secret over t + "." + raw body. Compute it on the exact body you received (not re-serialized JSON), compare in constant time and reject requests older than 5 minutes.
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
});After a secret rotation, for 24 hours the header carries two v1 values (new and old secret): accept the request if either matches.
Delivery and retries
- Answer any
2xxwithin 10 seconds. Process after answering (a queue). - Any other answer or a timeout is retried after 1 min, 5 min, 30 min, 2 h, 6 h, 12 h and 24 h (8 attempts in ~2 days).
- Order is not guaranteed and, rarely, an event can arrive twice: dedupe on
id(evt_…). - If a webhook fails continuously for 3 days we turn it off (“Off after errors”). Turn it back on in the dashboard or with
PATCH{"status": "enabled"}. - Recent deliveries (status, HTTP code, attempts) are in the dashboard and at
GET /webhook_endpoints/{id}/deliveries.
No server: polling
GET /events returns the events of the last 30 days, newest first. Use it for polling triggers and to catch up on anything you missed.
curl "https://aichat.md/api/public/v1/events?type=lead.created&created_after=2026-11-27T10:00:00Z" \
-H "Authorization: Bearer $AICHAT_API_KEY"