Skip to content

Verify webhook signatures

Every webhook carries a NOGORPAY-SIGNATURE header. Check it to make sure the request came from NogorPay and wasn’t changed.

  • Algorithm: HMAC-SHA256, hex encoded, prefixed with sha256=.
  • Message: the raw request body, exactly as received.
  • Secret: the API key that was used to create the charge.
NOGORPAY-SIGNATURE: sha256=<hex(HMAC_SHA256(api_key, raw_body))>
// Express
const crypto = require('crypto');
const express = require('express');
const app = express();
function isValidSignature(rawBody, header, secret) {
const expected =
'sha256=' + crypto.createHmac('sha256', secret).update(rawBody).digest('hex');
const a = Buffer.from(header || '');
const b = Buffer.from(expected);
return a.length === b.length && crypto.timingSafeEqual(a, b);
}
app.post(
'/api/nogorpay/webhook',
express.raw({ type: 'application/json' }),
(req, res) => {
const signature = req.get('NOGORPAY-SIGNATURE');
if (!isValidSignature(req.body, signature, process.env.NOGORPAY_API_KEY)) {
return res.status(401).send('Invalid signature');
}
const event = JSON.parse(req.body.toString('utf8'));
if (event.event === 'charge.completed') {
// Check event.amount against your order, then mark event.invoice_id as paid.
}
res.sendStatus(200);
},
);
Problem Likely cause
Signature never matches Your framework parsed the body before you hashed it. Use the raw body.
Works for new charges, fails for old ones You rotated API keys. Old charges are signed with the key that created them.
Compare throws an error in Node.js timingSafeEqual needs equal-length buffers. Check the length first, as in the example.