Skip to main content
Version: 4.2.0

Webhooks

WhatsApp sends incoming messages and delivery statuses to a callback URL. Before trusting a payload you must verify Meta's X-Hub-Signature-256 header. The package includes a dependency-free HMAC-SHA256 implementation and typed payload models.

Verify the callback URL (GET handshake)​

When you save the callback URL in the Meta App Dashboard, Meta sends a GET request with hub.mode, hub.verify_token and hub.challenge. Echo the challenge only when the token matches the verify token you configured.

final challenge = WhatsAppWebhook.verifyHandshake(
mode: query['hub.mode'] ?? '',
token: query['hub.verify_token'] ?? '',
challenge: query['hub.challenge'] ?? '',
verifyToken: 'YOUR_VERIFY_TOKEN',
);

if (challenge != null) {
return Response.ok(challenge, headers: {'content-type': 'text/plain'});
}
return Response(statusCode: 403);

Verify the signature (POST)​

The signature is an HMAC-SHA256 of the raw request body, keyed with your Meta app secret. Read the raw bytes/string before JSON decoding; re-serialising the JSON changes the bytes and invalidates the signature.

final valid = WhatsAppWebhook.isValidSignature(
payload: rawBody,
signature: request.headers['x-hub-signature-256'] ?? '',
appSecret: 'YOUR_APP_SECRET',
);

if (!valid) {
return Response(statusCode: 403);
}

isValidSignatureBytes accepts List<int> when your server exposes raw bytes.

Parse the payload​

import 'dart:convert';
import 'package:whatsapp/whatsapp.dart';

final webhook = WhatsAppWebhook.fromJson(
jsonDecode(rawBody) as Map<String, dynamic>,
);

for (final message in webhook.messages) {
print('From: ${message.from}');
print('Type: ${message.type}');
print('Text: ${message.text}');
print('Media ID: ${message.mediaId}');
print('Media MIME: ${message.mediaMimeType}');
print('Reply ID: ${message.replyId}');
print('Flow response: ${message.flowResponse}');
}

for (final status in webhook.statuses) {
print('${status.id}: ${status.status}');
}

Message helpers​

GetterDescription
textText body
mediaIdMedia ID for image, audio, video, document or sticker messages
mediaMimeTypeMIME type of the media
mediaCaption / mediaFilenameCaption and document file name
mediaSha256SHA-256 hash from the webhook
replyId / replyTitleTapped button or list row
reactionEmoji / reactionMessageIdReactions
locationLocation payload
contactsShared contact cards
flowResponseParsed Flow submission JSON
contextMessageIdMessage being replied to
sentAtParsed timestamp

Status helper​

GetterDescription
statussent, delivered, read or failed
recipientIdRecipient phone number
conversationConversation information
pricingPricing information
errorsTyped webhook errors
Deduplicate deliveries

Meta retries webhooks and can deliver duplicates. Store and de-duplicate by message or status ID. Always answer with HTTP 200 after you have accepted the payload.

Raw body

If you use a framework that parses JSON automatically, configure it to keep the raw body for the webhook route. isValidSignature on a re-encoded map cannot match Meta's signature.