Class: WebhookHandler
Verifies and parses webhook deliveries. Always verify before trusting a payload — without it, anyone can POST a fake payment confirmation to your endpoint.
Example
const webhooks = new WebhookHandler(process.env.VECTAPAY_WEBHOOK_SECRET!);
app.post('/webhooks/vectapay', express.raw({ type: 'application/json' }), async (req, res) => {
const signature = req.headers['x-signature'] as string;
let event;
try {
event = await webhooks.parseEvent(JSON.parse(req.body), signature);
} catch {
return res.status(400).json({ error: 'Invalid signature' });
}
res.status(200).json({ received: true }); // ack fast, process async
});Constructors
Constructor
new WebhookHandler(webhookSecret): WebhookHandler;Parameters
webhookSecret
string
Returns
WebhookHandler
Methods
parseEvent()
parseEvent(body, signature): Promise<WebhookEvent>;Verifies the signature and returns the typed event. Throws if the signature does not match.
Parameters
body
unknown
signature
string
Returns
Promise<WebhookEvent>
verifySignature()
verifySignature(payload, receivedSignature): Promise<boolean>;Verifies the X-Signature: v1=<hex> header against the raw request body string.
Parameters
payload
string
receivedSignature
string
Returns
Promise<boolean>
Last updated on