Skip to main content

Webhook Signature

HMAC Secret

How HMAC Works

HMAC involves using a cryptographic hash function (in this case, SHA-256) and a secret key to produce a unique signature for the payload. This signature is then sent along with the request. The receiver can use the same secret key to generate a signature for the received payload and compare it with the signature provided. If both signatures match, the payload is considered authentic.

Where you can find the secret

To retrieve the secret, navigate to the developer page in the webhook section. Here, you will find the secret key for verifying signatures. If necessary, you can also rotate the secret to ensure continued security.

warning

Rotating the secret will invalidate the old key and generate a new one, which should then be updated in your application to maintain seamless webhook functionality.

Webhook secret placement

Implementation Examples

Generating the Signature

const crypto = require('crypto');

const generateSignature = (secret, payload) => {
return crypto.createHmac('sha256', secret).update(payload).digest('hex');
};

Verifying the Signature

const verifySignature = (secret, payload, receivedSignature) => {
const expectedSignature = generateSignature(secret, payload);
try {
return crypto.timingSafeEqual(Buffer.from(expectedSignature), Buffer.from(receivedSignature));
} catch (e) {
return false;
}
};

Handling the Webhook Request

const express = require('express');
const app = express();
app.use(express.json());

app.post('/webhook', function(req, res) {
const secret = process.env.SECRET;
const signature = req.headers['x-signature'];

if (signature == null) {
res.status(403).send({ message: 'Signature not found' });
} else if (verifySignature(secret, JSON.stringify(req.body), signature)) {
res.status(200).send({ message: 'Webhook signature is valid' });
} else {
res.status(403).send({ message: 'Webhook signature is invalid' });
}
});

app.listen(3000, () => {
console.log('Server is running on port 3000');
});