Crypto payment webhooks: retries, duplicates and order
A webhook is how your shop learns that a crypto invoice was paid, and it is the one part of an integration you cannot test by clicking around. Checking the signature is covered in its own guide. This post is about everything after that: when easyway sends an event again, why the same event can reach you twice or in an unexpected order, and a small Node.js receiver that ships each order exactly once.
What counts as delivered
Each event is a JSON body with an id (evt_…), a type such as invoice.paid, the mode (test or live), a created time and the full invoice under data.invoice. It is POSTed to your endpoint, and only one answer counts as success: a 2xx status within 10 seconds. A timeout, a 500, a 404, even a 301 redirect to the same URL with a trailing slash, is a failed attempt. Redirects are never followed.
The retry schedule
A failed delivery is tried again on a fixed back-off, eight attempts in all:
| Attempt | Sent | Time since the first attempt |
|---|---|---|
| 1 | at once | — |
| 2 | 1 minute later | 1 min |
| 3 | 5 minutes later | 6 min |
| 4 | 30 minutes later | 36 min |
| 5 | 2 hours later | 2 h 36 min |
| 6 | 6 hours later | 8 h 36 min |
| 7 | 12 hours later | 20 h 36 min |
| 8 (last) | 24 hours later | 44 h 36 min |
Every attempt is signed again with the current time, so a retry a day later still passes a five-minute timestamp check. After the eighth failure the delivery is marked failed and is not sent again on its own. Separately, an endpoint that fails 25 attempts in a row (across all its deliveries) is switched off, and from then on new events are not queued for it at all. Dashboard → Developers lists every attempt with its status code and error, re-sends a single delivery on request, and switches the endpoint back on.
Why the same event arrives twice
Retries are the reason, and a retry does not mean your server never saw the request. The common cases:
| What happened | What you see |
|---|---|
| Your handler did the work but answered after 10 seconds | The attempt counts as failed; the same event comes again a minute later. |
| You answered 500 after the order was already saved | Same: a retry of an event you have handled. |
| Someone pressed retry on a delivery in the dashboard | The identical body and headers once more. |
| You have an account webhook and moved to a per-key one, or the reverse | Each endpoint gets its own copy with its own X-Webhook-Id; the event id is the same. |
So the key to remember is the event's id from the body. It stays the same on every retry and on every endpoint the event goes to. The X-Webhook-Id header names one delivery to one endpoint, which is useful for logs but not for de-duplication.
Why events arrive out of order
An invoice moves through a few states, and each move sends an event: invoice.confirming when a payment is seen but not yet confirmed, invoice.paid once the full amount has its confirmations, invoice.underpaid for a short payment (again for every further short top-up), invoice.expired when an unpaid invoice runs out of time, and invoice.cancelled. If the confirming delivery fails once, its retry arrives a minute later, after paid. And a customer who pays after expiry still turns the invoice paid, so expired is not always the last word (the underpaid and late payments guide has the details).
The fix is simple: never move an order backwards because of an event's type. Read data.invoice.status, which is the status at the time the event was made, and only ever move an order forward to paid. When in doubt, fetch the current state with GET /v1/invoices/:id.
A receiver that handles each event once
This file needs Node 22.13 or later and nothing from npm (it uses the built-in node:sqlite; with MySQL or Postgres the same idea is a unique key on the event id plus INSERT IGNORE or ON CONFLICT DO NOTHING):
// webhook.mjs: an easyway webhook receiver that survives retries. Node 22.13+, no packages.
import http from "node:http";
import crypto from "node:crypto";
import { DatabaseSync } from "node:sqlite";
const SECRET = process.env.EASYWAY_WEBHOOK_SECRET;
const db = new DatabaseSync(process.env.DB_FILE ?? "shop.db");
db.exec(`CREATE TABLE IF NOT EXISTS webhook_events (id TEXT PRIMARY KEY, type TEXT, received_at INTEGER);
CREATE TABLE IF NOT EXISTS orders (id TEXT PRIMARY KEY, status TEXT NOT NULL DEFAULT 'open')`);
function verify(raw, header = "") {
const parts = Object.fromEntries(header.split(",").map((p) => p.split("=", 2)));
const t = Number(parts.t);
if (!/^[0-9a-f]{64}$/.test(parts.v1 ?? "") || Math.abs(Date.now() / 1000 - t) > 300) return false;
const expected = crypto.createHmac("sha256", SECRET).update(`${t}.`).update(raw).digest("hex");
return crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(parts.v1));
}
function handle(event) {
db.exec("BEGIN");
try {
// 1. Seen this event id before? Then it is a retry: do nothing, but still answer 200.
const { changes } = db.prepare("INSERT OR IGNORE INTO webhook_events VALUES (?, ?, ?)").run(event.id, event.type, Date.now());
// 2. Decide from the invoice's status, not from the event name or arrival order.
const inv = event.data.invoice;
if (changes && inv?.status === "paid" && inv.order_id) {
db.prepare("UPDATE orders SET status = 'paid' WHERE id = ? AND status <> 'paid'").run(inv.order_id);
}
db.exec("COMMIT");
return changes ? "ok" : "duplicate";
} catch (err) {
db.exec("ROLLBACK"); // nothing recorded, so the next retry runs it again
throw err;
}
}
http.createServer((req, res) => {
const chunks = [];
let size = 0;
req.on("data", (c) => (size += c.length) > 256 * 1024 ? req.destroy() : chunks.push(c));
req.on("end", () => {
const raw = Buffer.concat(chunks);
if (!verify(raw, req.headers["x-webhook-signature"])) return res.writeHead(400).end();
try {
res.writeHead(200).end(handle(JSON.parse(raw)));
} catch {
res.writeHead(500).end(); // easyway tries again later
}
});
}).listen(3000);Three details matter. The event id and the order update are written in one transaction, so a crash halfway records neither and the next retry does the work. A duplicate still gets a 200, otherwise easyway keeps retrying something you already handled. And the order only changes when the invoice status is paid and the order is not paid yet. We ran it against a local sender: the first invoice.paid marked order 2048 paid, the same event again was answered duplicate, a late invoice.confirming left the order paid, and a forged signature got a 400.
Keep the handler fast. Sending the licence key, an e-mail or a call to another service belongs after the answer, in a queue or a background job that reads the paid orders. If that job is slow, the webhook still answers in milliseconds.
When deliveries were missed
If your endpoint was down for longer than the retry window, or was switched off after 25 failures, re-enable it in the dashboard and then catch up from the API rather than waiting for events: list recent invoices with GET /v1/invoices (or look one up with ?order_id=) and run each paid one through the same "mark paid if not paid yet" code. Because that code is safe to run twice, the catch-up and any late retries cannot ship an order twice. You can rehearse all of this with an sk_test_… key in the sandbox, and the full setup in Node.js is in the Node.js guide. Errors on the calls you make to the API are covered in what to retry and what to fix.