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
| Getter | Description |
|---|---|
text | Text body |
mediaId | Media ID for image, audio, video, document or sticker messages |
mediaMimeType | MIME type of the media |
mediaCaption / mediaFilename | Caption and document file name |
mediaSha256 | SHA-256 hash from the webhook |
replyId / replyTitle | Tapped button or list row |
reactionEmoji / reactionMessageId | Reactions |
location | Location payload |
contacts | Shared contact cards |
flowResponse | Parsed Flow submission JSON |
contextMessageId | Message being replied to |
sentAt | Parsed timestamp |
Status helper
| Getter | Description |
|---|---|
status | sent, delivered, read or failed |
recipientId | Recipient phone number |
conversation | Conversation information |
pricing | Pricing information |
errors | Typed webhook errors |
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.
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.