Verifying Sender Requests
Generating the Signature
const hmac = crypto.createHmac('SHA256', webhook.secret);
hmac.update(Buffer.from(JSON.stringify(payload)));
const signature = hmac.digest('hex');Verifying the Signature in Python
import hmac
import hashlib
import json
from fastapi import Request
async def verify_webhook(request: Request, secret: str):
# Extract the signature from the request headers
hmac_header = request.headers.get("x-authorization-hmac-256")
if not hmac_header:
return False # Missing signature header
# Retrieve and format the JSON payload
response_payload = await request.json()
payload_str = json.dumps(response_payload, separators=(',', ':')) # Match JSON.stringify()
# Compute the HMAC-SHA256 hash
hmac_obj = hmac.new(secret.encode(), payload_str.encode(), hashlib.sha256)
sha256_hash = hmac_obj.hexdigest()
# Compare the computed hash with the received signature
return hmac_header == sha256_hashDebugging Tips
Last updated
Was this helpful?

