If your WhatsApp Cloud API webhook writes straight into a CRM, anyone who finds the URL can create fake contacts. Verifying the signature Meta sends is the first line of any WhatsApp CRM integration done safely.
We integrate with all major CRMs (for example HubSpot, Zoho, Salesforce, Pipedrive and Freshsales, plus custom CRMs or Google Sheets setups) and work with all major WhatsApp Business providers and the official WhatsApp Business API (for example Meta Cloud API, Twilio, Gupshup, Interakt and WATI).
What Meta sends
Each webhook POST includes an X-Hub-Signature-256 header: sha256= followed by an HMAC of the raw request body, keyed with your app secret.
Verify with the raw body
Parse JSON only after verification, and use the raw bytes, not a re-serialised dict.
import hmac, hashlib, os
from flask import Flask, request, abort
app = Flask(__name__)
APP_SECRET = os.environ["META_APP_SECRET"].encode()
def valid(raw: bytes, header: str) -> bool:
if not header or not header.startswith("sha256="):
return False
expected = hmac.new(APP_SECRET, raw, hashlib.sha256).hexdigest()
return hmac.compare_digest(expected, header.split("=", 1)[1])
@app.post("/webhook")
def webhook():
raw = request.get_data()
if not valid(raw, request.headers.get("X-Hub-Signature-256", "")):
abort(401)
event = request.get_json()
enqueue_for_crm(event) # write asynchronously, reply fast
return "", 200
Other habits
- Reply 200 quickly and process in a queue; slow handlers trigger retries.
- Store the message ID and ignore duplicates.
- Keep the app secret in environment variables, never in the repo.
- Log rejected requests with the source IP, but not the body.
Testing
Compute a signature locally with the same secret and a saved sample payload, then send it with curl. Also test a tampered body and confirm you get 401.
More on choosing custom flows: why we often build custom instead of buying a bot.
Disclosure: I co-founded NxFlowAI, an automation agency.
Top comments (0)