v1
Guide

Webhooks

With a webhook, Guria notifies your system the moment something happens: a message arrived, went out, was delivered, read or refused.

How it works

  1. You register an address

    An https address in your system and the list of events it wants to receive. In the panel or through Create a webhook.

  2. Guria stores a secret

    The response to the registration carries the secret that signs every delivery. It is shown in full only once.

  3. Something happens

    Guria makes a POST to your address with the event in JSON and the signature in a header.

  4. You answer 2xx

    Within 10 seconds. Any other response counts as a failure, and the delivery is retried.

See it happen

The simulator below runs in your browser and goes through the whole cycle: your system sends a message through the API, it shows up on the customer's device, and each state comes back as a delivery to your webhook, with the signature actually computed over the body.

  1. 1send
  2. 2accepted
  3. 3sent
  4. 4delivered
  5. 5read
Fulano de Talcustomer's device

Write the message below and send it. Your system talks to Guria, the message appears here, and each of its states comes back as a delivery to your webhook, on the side.

webhook secret in this demo: segredo-de-exemplo-0000000000000

Each step of the cycle shows up here, numbered, with who is talking to whom.

The delivery body

Every delivery has the same envelope. What changes from one event to another is the content of data.

FieldWhat it is
eventThe event name, such as MessageReceived.
business_idThe company the event happened in.
timestampWhen it happened, in Unix seconds.
dataThe event data. Its fields are in snake_case.
{
  "event": "MessageReceived",
  "business_id": "2ea7ad0d-8e13-4008-9fd8-7c92fe8677f2",
  "timestamp": 1764092406,
  "data": {
    "message_id": "9e4b7c2a-6d1f-4a3e-8b5c-0f7d2a9e6c41",
    "conversation_id": "3c8e1a5d-7b2f-4d6a-9e0c-5a1f8b3d7e29",
    "contact_id": "af147888-ebb0-4ec9-92e5-da66cd561461",
    "from": "5511988887777",
    "type": "text",
    "waba_message_id": "wamid.HBgNNTUxMTk4ODg4Nzc3NxUCABEYEjZDMUQ0RjNBOEIyRTdDOTUwAA==",
    "timestamp": 1764092406,
    "content": {
      "text": "Oi, Fulano! Seu pedido saiu para entrega e chega hoje até as 18h."
    },
    "media_url": null
  }
}
  • In a text message, content is { "text": "…" }. In the other received types (image, audio, document, location, button), content is the message in the WhatsApp Business API format.
  • When the message has media, media_url is a signed address to download the file, valid for 7 days.

Events

EventWhen it happens
MessageReceivedA customer sent a message to one of the company's numbers.
MessageSentA message from the company was accepted by WhatsApp and went out.
MessageDeliveredThe message reached the customer's device.
MessageReadThe customer opened the conversation and read the message.
MessageFailedWhatsApp refused the send. The body carries the code and the reason.

Besides messages, a webhook can subscribe to the events of the company's WhatsApp account:

SubjectEventsWhen it happens
TemplatesTemplateApproved, TemplateRejectedMeta approved or rejected a template submitted for review.
NumbersNumberConnected, NumberDisconnectedOne of the company's numbers connected or lost its connection.
WhatsApp accountAccountUpdate, AccountRestricted, AccountBanned, QualityChanged, NameStatusChangedMeta changed the account's status, quality or display name.

The full list of accepted names comes from List the available events.

Delivery headers

HeaderWhat it carries
X-Webhook-SignatureThe signature of the body: sha256= followed by the HMAC SHA-256 in hexadecimal.
X-Webhook-IdThe delivery id. It is the same on every attempt: use it to avoid processing twice.
X-Webhook-AttemptThe attempt number, starting at 1.
User-AgentAlways GuriaWebhooks/1.0, so you can recognize the origin and allow it through your firewall.

Validating the signature

Anyone who finds out your address can send a POST to it. The signature is what proves the delivery came from Guria: compute the HMAC SHA-256 of the body exactly as it arrived, with the webhook secret, and compare it with the X-Webhook-Signature header.

import crypto from "node:crypto";
import express from "express";

const app = express();

// The signature covers the raw body: read the bytes before any conversion.
app.post("/webhooks/guria", express.raw({ type: "application/json" }), (req, res) => {
  const expected = "sha256=" + crypto
    .createHmac("sha256", process.env.GURIA_WEBHOOK_SECRET)
    .update(req.body)
    .digest("hex");

  const received = req.get("X-Webhook-Signature") ?? "";
  const valid = received.length === expected.length
    && crypto.timingSafeEqual(Buffer.from(received), Buffer.from(expected));
  if (!valid) return res.sendStatus(401);

  // Answer right away and process later: the delivery waits at most 10 seconds.
  res.sendStatus(200);
  enqueue(req.get("X-Webhook-Id"), JSON.parse(req.body));
});

Attempts

A delivery is retried when your address answers 408, 429 or 5xx, takes longer than 10 seconds or does not accept the connection. There are up to 6 attempts:

AttemptWhen
1stThe moment the event happens.
2nd30 seconds after the previous attempt.
3rd2 minutes after the previous attempt.
4th10 minutes after the previous attempt.
5th1 hour after the previous attempt.
6th6 hours after the previous attempt.
  • Other 4xx responses end the delivery right away: retrying would give the same result.
  • After the last attempt the delivery is closed. The panel keeps the history of each one and lets you resend it.
  • Arrival order is not guaranteed: a retried delivery can arrive after a newer event. Order by timestamp.

Good practices

  • Answer fast: put the event on a queue and return 200. Slow processing comes later.
  • Do not process twice: store the X-Webhook-Id of the deliveries you already handled and ignore repeats.
  • Subscribe only to what you use: fewer events, less traffic and less code to maintain.
  • Rotate the secret if it leaks, through Rotate the signing secret. The previous one stops working immediately.