Recover
...
API Reference
Resources

Webhook Signature Verification

3min
overview code examples that can be used to verify a webhook signature using a signing key python \# python import json import hashlib import hmac from time import time def verify webhook signature(body dict, headers dict, signing key str) > bool &#x9;now = int(time()) &#x9;payload = json dumps(body, separators=(",", " ")) &#x9;incoming signature = headers get("x butter webhook signature") &#x9;expires at = headers get("x butter webhook expires") &#x9;message = f"{payload}+{expires at}" encode("utf 8") &#x9;signature = hmac new(signing key encode("utf 8"), message, hashlib sha256) hexdigest() &#x9;if signature != incoming signature &#x9; return false &#x9;if expires at < now &#x9; return false &#x9;return true javascript // javascript const crypto = require('crypto'); function verifywebhooksignature(body, headers, signingkey) { &#x9;const now = math floor(new date() gettime()/1000); &#x9;const payload = json stringify(body); &#x9;const expiresat = headers\["x butter webhook expires"]; &#x9;const incomingsignature = headers\["x butter webhook signature"]; &#x9;const hmac = crypto createhmac('sha256', signingkey); &#x9;const data = hmac update(buffer from(`${json stringify(body)}+${expiresat}`)); &#x9;const signature = data digest('hex'); &#x9;if(signature != incomingsignature) return false; &#x9;if(expiresat < now) return false; &#x9;return true; }