Skip to content

Receive messages by webhook

Receive WhatsApp messages and delivery reports on your own server: events and payloads, checking the signature, retries, duplicates and downloading files.

A webhook is an https address of yours. WizMessage posts to it when a customer writes, when a message was sent, delivered, read or failed, and when somebody reacts.

Switch it on

Open the key in API Management, go to Webhooks, enter the address and tick the events you want. The signing secret (whsec_…) is shown once. Send Test Webhook posts a webhook.test event, so you can try the address before a customer writes.

Messages from customers and their reactions come only while the bot of the number is set to Webhook in Bot Settings. With any other bot, that bot answers and nothing is posted. Reports about your own messages come either way.

Events

EventWhen
message.receivedA customer wrote to you. Sent only while the number forwards its messages to your webhook (Webhook forwarding as its bot). to is empty today. Buttons and lists arrive as text.
message.sentA message was sent from your number. From this API, the app, the inbox or an AI agent; source says which.
message.deliveredA message was delivered.
message.readA message was read.
message.failedA message could not be delivered. After acceptance, message_id is the WhatsApp id. If WhatsApp never accepted the message, it is our uuid.
message.reactionSomebody reacted to a message. direction says who: inbound is the customer. A removed reaction has an empty emoji.

Only ticked events are sent. message.delivered and message.read have their own ticks.

What arrives

Every event has the same frame: version, event, timestamp and data. These are whole examples.

message.received
{
  "version": 1,
  "event": "message.received",
  "timestamp": "2026-11-25T10:00:00.000+00:00",
  "data": {
    "message_id": "wamid.EXAMPLE-INBOUND-1",
    "to": "",
    "message": {
      "type": "text",
      "text": {
        "body": "Where is my order?"
      }
    },
    "from": "447700900123",
    "contact": {
      "name": "Riya",
      "wa_id": "447700900123"
    }
  }
}
message.delivered
{
  "version": 1,
  "event": "message.delivered",
  "timestamp": "2026-11-25T10:00:03.000+00:00",
  "data": {
    "message_id": "wamid.EXAMPLE-SENT-1",
    "provider_message_id": "wamid.EXAMPLE-SENT-1",
    "reference": "order-1042",
    "status": "delivered",
    "recipient": "447700900123",
    "delivered_at": "2026-11-25T10:00:03.000+00:00"
  }
}
message.failed
{
  "version": 1,
  "event": "message.failed",
  "timestamp": "2026-11-25T10:00:04.000+00:00",
  "data": {
    "message_id": "wamid.EXAMPLE-SENT-1",
    "status": "failed",
    "recipient": "447700900123",
    "reference": "order-1042",
    "error": {
      "code": "MESSAGE_SEND_FAILED",
      "message": "The message could not be delivered."
    }
  }
}
  • data.message_id is the WhatsApp id. In reports about your own message it is the whatsapp_message_id of the send, and data.reference is your own id. If WhatsApp never accepted the message, it is our uuid.
  • data.source on message.sent says where the message came from: this API, the app, the inbox or an AI agent.
  • data.to is empty on message.received today. The message went to the number of the key.
  • data.message holds a block named after type. Text, and what a customer taps on buttons and lists, is in text.body. A place or a contact card arrives as a sentence in text.body. Files have a block with a media id.

The manual lists more fields, for example the ad a customer came from.

Check the signature

HeaderValue
X-Webhook-SignatureLowercase hex HMAC-SHA256 of timestamp + "." + raw body, keyed with the webhook secret (whsec_…) as it is.
X-Webhook-TimestampUnix seconds. Refuse deliveries that are older than a few minutes.
X-Webhook-EventName of the event.
X-Webhook-IdId of the event; every retry carries the same one.
  1. Read the raw body, before any JSON parser touches it.
  2. Build timestamp.body from the header and the raw body.
  3. Compute the HMAC-SHA256 with the webhook secret as it is. The requests you send use a hash of the API secret; webhooks do not.
  4. Compare in constant time, and refuse timestamps that are more than a few minutes old.
PartValue
Secretwhsec_EXAMPLE
Timestamp1764064800
Raw body{"version":1,"event":"webhook.test"}
Signature0485b795475d6a75d675f9844162e8152de5039609dcb2cbb45e46e404c091c8

A receiver

It checks the signature, answers, and ignores what it has seen. Put it behind https before you use it.

webhook.mjs
// node --env-file=.env nodejs/webhook.mjs
// Receives webhooks on http://localhost:8080/. Put it behind https before you use it.
import { createHmac, timingSafeEqual } from 'node:crypto'
import { createServer } from 'node:http'

const SECRET = process.env.WIZMESSAGE_WEBHOOK_SECRET
const PORT = process.env.PORT ?? 8080
const MAX_AGE_SECONDS = 300
const MAX_BYTES = 1_000_000

// The same event can arrive twice. Keep this in your database; a Map is enough for an example.
const seen = new Map()

export function verify(rawBody, timestamp, signature, secret = SECRET, now = Date.now() / 1000) {
  if (!secret || !signature || !/^\d+$/.test(timestamp ?? '')) return false
  if (Math.abs(now - Number(timestamp)) > MAX_AGE_SECONDS) return false
  const expected = createHmac('sha256', secret).update(`${timestamp}.`).update(rawBody).digest()
  const given = Buffer.from(signature, 'hex')
  return given.length === expected.length && timingSafeEqual(given, expected)
}

export function isNew(event) {
  const id = `${event.event}:${event.data?.message_id}`
  if (seen.has(id)) return false
  seen.set(id, Date.now())
  if (seen.size > 10_000) seen.delete(seen.keys().next().value)
  return true
}

function handle(event) {
  // Keep this short, or put the work in a queue: the answer is expected within 10 seconds.
  console.log(event.event, event.data?.message_id, event.data?.message?.type ?? event.data?.status ?? '')
}

if (process.argv[1]?.endsWith('webhook.mjs')) {
  createServer(async (request, response) => {
    const chunks = []
    let size = 0
    for await (const chunk of request) {
      size += chunk.length
      if (size > MAX_BYTES) return response.writeHead(413).end()
      chunks.push(chunk)
    }
    const rawBody = Buffer.concat(chunks)
    // The signature is made over the bytes as they arrive, so verify before you parse.
    if (!verify(rawBody, request.headers['x-webhook-timestamp'], request.headers['x-webhook-signature']))
      return response.writeHead(401).end()

    response.writeHead(200).end()
    const event = JSON.parse(rawBody)
    if (isNew(event)) handle(event)
  }).listen(PORT, () => console.log(`Listening on port ${PORT}`))
}

Answer quickly

Answer with 2xx within 10 seconds and do the work afterwards. Redirects are not followed.

Your answerWhat happens
2xxDelivered
5xx, 3xx, 408, 429, a timeout, no connectionSent again after about 1 second, 5 seconds, 30 seconds and 5 minutes; five attempts in all
Any other 4xxFinal. The event is not sent again
410The webhook of the key is switched off

Ignore repeats

An event can arrive twice, and not always in order. Keep event together with data.message_id for a few days, and ignore what you already have. X-Webhook-Id is the same on every retry of one event.

Files from customers

A file does not come with a link. Its block has a media id, and GET /media/{media_id} returns the file itself. The key needs view_messages.

  • The id of a customer's file works for 7 days, the id of your own upload for 30. Save the file, not the id.
  • The answer is the file, with its content type. Errors are JSON, so look at the status first.
  • At most 32 MB. If the connection ends early, compare the length with Content-Length and try again.
  • A document has its name in the webhook, as document.filename.
  • A download counts as a request of the key.

The clients on the authentication page have a method for it.