Skip to content
SwapWalletDocs
فارسیGet an API key

Receive webhooks

Get notified when an invoice receives money: set the webhook URL, verify the HMAC-SHA256 signature in Node.js, Python or PHP, respond in time and handle retries.

A webhook is a request SwapWallet sends to your server when an invoice receives money, so you don’t have to keep asking. This page covers where they go, what they contain, how to prove they came from SwapWallet, and what happens when your server doesn’t answer.

Choose where they go

Each invoice can carry its own webhookUrl. An invoice without one uses your business’s default URL, set in the shop’s settings on the business panel. Use HTTPS, and a path nobody would guess.

When they’re sent

  • After every confirmed blockchain transfer to an invoice’s address, including one that doesn’t cover the invoice yet (its status is still ACTIVE) and one that arrives after it’s paid.
  • When an invoice is paid from a SwapWallet account, or a Toman receipt is paid.
  • When you call Retry a webhook delivery.

So a webhook means “money moved on this invoice”, not “this order is paid”. Always check event.invoice.status.

What’s in a delivery

A POST with a JSON body of three fields: requestId (new on every attempt), hmac (the signature) and event, which holds the invoice as it was when the payment was processed. Every field is in the event reference.

HTTP
POST /webhooks/swapwallet HTTP/1.1
Content-Type: application/json
User-Agent: SwapWallet

{"requestId":"a7d0cb53-b035-44b4-b48e-4b04a59d5f97","hmac":"e14c8f2a…43bf6","event":{"time":"2026-11-23T09:19:42.107Z","type":"INVOICE","invoice":{"id":"bcbb332e-0bf8-4da2-a42f-a75a0be6c36f","status":"PAID", … }}}

Verify the signature

Anyone who finds your URL can send it a request, so check every delivery before you act on it. hmac is an HMAC-SHA256 of the event object, keyed with your business API key (the whole key, business- included), as lowercase hex.

  1. Read the raw request body. Don’t parse and re-encode it: a different key order or spacing changes the signature.
  2. Take the JSON text of event exactly as sent.
  3. Compute HMAC-SHA256 over it with your key and compare with hmac in constant time.
  4. Reject the request with 401 if they differ.
import crypto from 'node:crypto';
import express from 'express';

const KEY = process.env.SWAPWALLET_BUSINESS_KEY; // the whole key, "business-…" included

// HMAC-SHA256 of the raw "event" JSON, exactly as it was sent.
function isFromSwapWallet(rawBody) {
  const raw = rawBody.toString('utf8');
  const marker = '"event":';
  const start = raw.indexOf(marker);
  if (start < 0) return false;
  const event = raw.slice(start + marker.length, raw.lastIndexOf('}'));
  const expected = crypto.createHmac('sha256', KEY).update(event, 'utf8').digest('hex');
  const { hmac } = JSON.parse(raw);
  return typeof hmac === 'string' && hmac.length === expected.length &&
    crypto.timingSafeEqual(Buffer.from(hmac), Buffer.from(expected));
}

const app = express();

// Keep the raw bytes: re-encoding parsed JSON can change them.
app.post('/webhooks/swapwallet', express.raw({ type: 'application/json' }), (req, res) => {
  if (!isFromSwapWallet(req.body)) return res.sendStatus(401);
  const { event } = JSON.parse(req.body);
  res.sendStatus(200);                 // answer within 10 seconds…
  queue.add('invoice', event.invoice); // …and do the work afterwards
});

Test your code before going live: the example in the event reference carries a real hmac, computed with the example key shown there.

Would rather not verify signatures? Treat the webhook as a nudge only: take event.invoice.id and fetch the invoice with Retrieve an invoice, then act on that response. SwapWallet doesn’t publish a fixed list of IP addresses, so don’t rely on an IP allowlist.

Respond quickly

Answer with a 2xx status within 10 seconds. Do slow work (delivering the product, sending emails) after you’ve answered, from a queue. A 4xx or 5xx status, a timeout or a refused connection counts as a failed delivery.

Retries and duplicates

A failed delivery is retried after 1, 2, 4, 8, 16 and 32 minutes, then every hour, for up to 72 attempts over about three days. Each attempt has a new requestId, event.time and hmac; the invoice inside stays the same. Each attempt is signed with the key your business has at that moment, so after you replace the key, retries carry the new signature.

The same payment can therefore reach you more than once. Make your handler idempotent on event.invoice.id and event.invoice.paidAmount, and never on requestId.

See every attempt for an invoice with Retrieve webhook deliveries, and send a paid invoice’s webhook again with Retry a webhook delivery.

Test locally

SwapWallet must be able to reach your URL over the internet. While developing, point webhookUrl at a request inspector such as webhook.site to see real deliveries, or expose your local server through a tunnel. Pay a small invoice to trigger one.