Today I was working on Flare, fixing a bug where a webhook payload didn't work. Webhooks allow applications to communicate with each other without constant polling for updates. When a change happens, a request is sent from one service to another, indicating what happens.
The web is an open place with many bad actors, so a signature is sent with this request as a protection measure. This signature is constructed from a secret key that both sender and receiver know and the contents of the payload.
In PHP, the sender would create a request and then a signature as follows:
$signature = hash_hmac(
This signature is then appended as a header to the request.
The receiver on the other side would then again construct the signature like above and then check if it corresponds with the signature sent:
If the two correspond, we know no one tampered with the message, and the request was sent by the service we trust (not some other bad actor who found out what the endpoint was).
Local signature mess
That's all great. Until you start debugging errors why something is not working, Postman is one of the best tools to send API requests from your computer when you're debugging.
But since the signature depends on the contents of the request payload, it needs to be recalculated each time the payload changes.
To add a signature header, we use this piece of code:
const secret ='some_secret_key';
var signature = CryptoJS.HmacSHA256(request.data, secret).toString(CryptoJS.digest);
console.log("Signature :: " + signature)
This code will add a signature header with an HMAC SHA256 hashed version of the secret and payload content, neat!