Digital Solutions Studio
How to verify a WhatsApp webhook signature in Python
When you connect WhatsApp to your own server, the provider tells you about each message by calling a public address that you publish. Anyone can call that address, which is why the provider signs every notification: the signature shows it came from them and that nobody changed the content on the way.
Tell us about your project- Custom solutions for each business
- Software, automation and integrations
- Tell us about your project and we’ll look into your case
Verifying it is the first thing worth doing.
In Meta's API the signature arrives in a header on every request. It is an HMAC calculated over the exact body that was sent, using your application's secret as the key. Your server repeats the calculation with the body it received and compares the result with the header. If they differ, it rejects the request without processing it.
The most common mistake is to parse the body as JSON and serialise it again before calculating. Rewriting it changes the spacing or the order of the fields, and the signature stops matching even though everything else is correct. The fix is to calculate the signature on the original bytes, exactly as they arrive, and only then read the JSON. To compare the two values, use a constant-time comparison function.
Twilio uses a different method. Its signature is calculated from the full address it calls plus the form fields sorted by name, and it is checked with your authentication token. Because the address must match exactly the one you configured, behind a proxy or a tunnel it is wise to set the public address in your configuration instead of trusting the one your application sees.
If your application rejects genuine notifications, check three things first: the key, the unmodified body and the address. If you would rather not write this part from scratch, tell us about your case in the form and we will look at how to solve it in your project.
Tell us about your project
Tell us what you need. We’ll find a way to make it happen.
Tell us about your project