A safe SMS webhook receiver does four things: it reads the raw body, verifies the signature, skips events it has seen and answers fast. Everything else happens after you've replied. This post shows that receiver in Flask and in Express, using only each language's standard crypto.
We make Ready, so the headers below are Ready's. The shape carries over to any provider that signs with an HMAC. Our handlers are event-agnostic on purpose: they verify and queue whatever arrives, and your worker decides what each event means. For the full list of events and fields, use the webhooks reference.
What Ready sends
Every webhook is a POST with a JSON body shaped {event, timestamp, data}. These headers matter:
| Header | What it carries |
|---|---|
X-Ready-Event-Id | A stable id. It's the same on every retry and resend of an event, so dedupe on it. |
X-Ready-Timestamp | Unix seconds of this delivery try. |
X-Ready-Signature-V2 | v2= plus the hex HMAC-SHA256 of ${timestamp}.${body}, keyed with your signing secret. |
X-Signature | sha256= plus the hex HMAC-SHA256 of the raw body alone. The older form. |
The signature headers appear when the webhook has a signing secret. Use V2, because the timestamp in it lets you refuse a replayed request. Refuse any timestamp more than 5 minutes from your server's clock.
The four rules
- Sign over the raw body. Parsing JSON and re-serializing it changes bytes, and the signature won't match. Read the bytes first.
- Compare in constant time. Use the language's constant-time compare, not
==. - Dedupe on the event id. Delivery is at least once, so the same event can arrive twice.
- Ack fast, work later. Return a 2xx quickly and push the work to a queue. A slow handler gets retried, and a retry is a duplicate you have to catch.
Our post on receiving inbound SMS through webhooks covers the Ready event shapes, and webhook deduplication goes deeper on rule 3.
Flask
Flask gives you the raw bytes with request.get_data(). Call it before anything touches request.json.
import hmac, hashlib, os, time
from flask import Flask, request, abort
app = Flask(__name__)
SECRET = os.environ["READY_WEBHOOK_SECRET"].encode()
TOLERANCE = 300 # 5 minutes, in seconds
def seen_before(event_id):
# Replace with an insert into a table that has a unique
# constraint on event_id. Return True if the insert collides.
raise NotImplementedError
def enqueue(raw_body):
# Push the raw body to your queue or job runner.
raise NotImplementedError
@app.post("/webhooks/ready")
def ready_webhook():
raw = request.get_data()
ts = request.headers.get("X-Ready-Timestamp", "")
sig = request.headers.get("X-Ready-Signature-V2", "")
if not ts.isdigit() or abs(time.time() - int(ts)) > TOLERANCE:
abort(400)
signed = ts.encode() + b"." + raw
expected = "v2=" + hmac.new(SECRET, signed, hashlib.sha256).hexdigest()
if not hmac.compare_digest(expected, sig):
abort(401)
event_id = request.headers.get("X-Ready-Event-Id", "")
if event_id and seen_before(event_id):
return "", 200 # already handled, ack so it stops retrying
enqueue(raw)
return "", 200 Two details are easy to miss. compare_digest stops a timing attack, and returning 200 on a duplicate tells the sender to stop retrying.
Express
In Express, the default JSON parser consumes the body before you can sign it. Use express.raw on this one route so req.body stays a Buffer.
const crypto = require("node:crypto");
const express = require("express");
const app = express();
const SECRET = process.env.READY_WEBHOOK_SECRET;
const TOLERANCE_SECONDS = 300; // 5 minutes
async function seenBefore(eventId) {
// Insert into a table with a unique constraint on event_id.
// Return true if the insert collides.
throw new Error("not implemented");
}
async function enqueue(rawBody) {
// Push the raw body to your queue or job runner.
throw new Error("not implemented");
}
app.post(
"/webhooks/ready",
express.raw({ type: "application/json" }),
async (req, res) => {
const raw = req.body; // a Buffer
const ts = req.get("X-Ready-Timestamp") || "";
const sig = req.get("X-Ready-Signature-V2") || "";
if (!/^\d+$/.test(ts)) return res.sendStatus(400);
if (Math.abs(Date.now() / 1000 - Number(ts)) > TOLERANCE_SECONDS) {
return res.sendStatus(400);
}
const signed = Buffer.concat([Buffer.from(ts + "."), raw]);
const expected =
"v2=" + crypto.createHmac("sha256", SECRET).update(signed).digest("hex");
const a = Buffer.from(expected);
const b = Buffer.from(sig);
if (a.length !== b.length || !crypto.timingSafeEqual(a, b)) {
return res.sendStatus(401);
}
const eventId = req.get("X-Ready-Event-Id");
if (eventId && (await seenBefore(eventId))) return res.sendStatus(200);
res.sendStatus(200); // ack first
await enqueue(raw);
}
); Note that timingSafeEqual throws when the two buffers differ in length, so the length check comes first.
Process async, not in the request
Acknowledging fast means your handler does almost nothing: verify, dedupe, queue. The queue worker then parses the JSON, reads event and data, and does the real work. Because your handler doesn't depend on event names, a new event type won't break it.
If your queue is down, return a 5xx so the sender retries. If the work itself fails later, that's a worker problem, and a dead letter queue with replay is the usual answer.
Test it before you trust it
- Send a request with a bad signature and confirm a 401.
- Send a good request with a timestamp ten minutes old and confirm a 400.
- Send the same event id twice and confirm only one job lands in the queue.
- Change one byte of the body after signing and confirm a 401.
If you work locally, a tunnel to your machine lets the real service reach your handler. The webhook testing post on testing SMS webhooks locally covers that.
Frequently asked questions
How do I verify a Ready webhook signature in Flask?
Read the raw bytes with request.get_data(), build timestamp + "." + body, compute an HMAC-SHA256 with your signing secret and compare v2= plus the hex digest to the X-Ready-Signature-V2 header with hmac.compare_digest. Reject any timestamp more than 5 minutes old.
Why does my Express signature check fail?
Almost always because the JSON parser ran first and the body was re-serialized. Use express.raw on the webhook route so you sign the exact bytes that arrived.
How do I stop processing the same webhook twice?
Store the X-Ready-Event-Id header with a unique constraint and skip any event whose id is already there. The id is the same on every retry, so it's safe to dedupe on.
How fast does my endpoint need to respond?
As fast as you can: verify, record the id, queue the work and return a 2xx. Do the slow parts after you've replied, so a retry never has to fire.
The takeaway
A good receiver is short: raw body, signature, timestamp window, event id, queue. Keep it event-agnostic and put the thinking in your worker. When you're ready to wire it to a live account, sign up and save your endpoint under Settings, then Developers.