Verify webhook signatures in Node, Python and PHP
Your webhook URL is public. Anyone who finds it can send it a request that looks like "this email bounced" or "this email was delivered". If your app trusts those requests blindly, someone could mark good addresses as bad, or fake events in your logs. Every webhook from Vaartix is signed, so checking the signature first is how you know a request is real.
How the signature works
Vaartix follows the open Standard Webhooks specification. Each request carries three headers: webhook-id, webhook-timestamp and webhook-signature. To check one:
- Join the id, the timestamp and the raw request body with dots:
{id}.{timestamp}.{body}. - Compute an HMAC-SHA256 of that string. The key is your endpoint's secret without its
whsec_prefix, base64-decoded. - Base64-encode the result and compare it, in constant time, with each
v1,entry in the signature header. - Reject requests whose timestamp is more than 5 minutes from your clock, so an intercepted request can't be replayed later.
You rarely need to write that yourself: the SDKs do it in one call.
Node.js (Express)
import express from 'express';
import { verifyWebhook, WebhookVerificationError } from 'vaartix';
const app = express();
app.post('/webhooks/vaartix', express.raw({ type: 'application/json' }), (req, res) => {
let event;
try {
event = verifyWebhook({
payload: req.body, // a Buffer, thanks to express.raw()
headers: req.headers,
secret: process.env.VAARTIX_WEBHOOK_SECRET,
});
} catch (err) {
if (err instanceof WebhookVerificationError) return res.status(400).send('Invalid signature');
throw err;
}
res.sendStatus(200);
// Handle the event after responding, e.g. queue it.
});
Python (Flask)
import os
from flask import Flask, request
from vaartix import WebhookVerificationError, verify_webhook
app = Flask(__name__)
@app.post("/webhooks/vaartix")
def vaartix_webhook():
try:
event = verify_webhook(request.get_data(), request.headers, os.environ["VAARTIX_WEBHOOK_SECRET"])
except WebhookVerificationError:
return "Invalid signature", 400
# handle event["type"] and event["data"]
return "", 200
On Django, the same call works with request.body; see Send email from Django.
PHP
<?php
use Vaartix\Exceptions\WebhookVerificationException;
use Vaartix\Webhook;
try {
$event = Webhook::verify(
file_get_contents('php://input'),
getallheaders(),
getenv('VAARTIX_WEBHOOK_SECRET'),
);
} catch (WebhookVerificationException $e) {
http_response_code(400);
exit;
}
// handle $event['type'] and $event['data']
http_response_code(200);
On Laravel, pass $request->getContent() and $request->headers->all(); see Send email from Laravel.
The mistakes that break verification
- Parsing the body first. The signature covers the exact bytes Vaartix sent. If your framework parses the JSON and you re-serialise it, spacing can change and the check fails. Always verify the raw body:
express.raw(),request.get_data(),php://input. - Using the wrong secret. Each endpoint has its own
whsec_...secret, shown once when you create it. Store it in an environment variable. - A wrong server clock. Requests older than 5 minutes are rejected, so a server whose clock is far off rejects real ones too. Keep your clock synced.
- CSRF protection in Laravel or Django blocking the route. Exempt it: the signature check is what keeps it safe.
- Slow handlers. Answer with a
2xxwithin 10 seconds, and do slow work afterwards or in a background job.
Test your endpoint without real email by sending to [email protected]: it produces a real, signed email.bounced webhook. The full reference is Verify signatures.