Webhooks for Crypto Deposits: How They Work
How a webhook detects an incoming crypto deposit: webhook versus polling, the payload fields, verifying the HMAC signature and handling missed notifications.
Reviewed by Maurice Sobiera
A webhook is an HTTP request that a service sends to your server when an event happens. For crypto deposits, the event is a transaction that matches an address you watch. Chaingateway posts the transaction details to your endpoint, and your code credits the customer. This replaces repeated polling of a blockchain API for new transactions.
What is a webhook?
A webhook is a callback over HTTP. You give the sender a URL, and the sender makes a POST request to that URL when a predefined event occurs. The body of the request is the payload. Think of the URL as a phone number that the sender calls whenever something happens.
Webhooks and APIs are two ways to exchange data. With an API you pull: your code asks for data on a schedule and checks whether anything changed. With a webhook the provider pushes: it contacts you at the moment of the event. Push saves requests and shortens the delay. Pull still has a role, because a webhook only tells you that something happened. If you need the full record or want to check it, you ask the API.
Webhook or polling: which should I use for deposits?
Use webhooks to learn about a deposit quickly, and the API to verify and reconcile it. The two work well together.
| Approach | Strength | Weakness |
|---|---|---|
| Polling the API | You control timing and can resume after downtime | Wastes requests, and the delay is as long as your interval |
| Webhook | Notification soon after the transaction is seen | A notification can be missed if your endpoint is down |
On Chaingateway, webhooks are available for Bitcoin, Ethereum, BNB Smart Chain, Tron, Polygon and Arbitrum. Solana has no webhooks, so you query transactions there.
How do I set up a deposit webhook?
Create a receiver endpoint, then register a webhook for the address you want to watch. The endpoint must be reachable from the public internet. You do not need to allow Chaingateway IP addresses, because the service calls your public URL like any other client.
A webhook needs a url and at least one of from or to. The optional filters are contractaddress, token_id and type, which is ETH, ERC20 or ERC721 on Ethereum. You can also set a reference of up to 255 characters, for example your order number. This request watches deposits of one ERC-20 token (here the USDT contract) to one address.
curl -X POST https://app.chaingateway.io/api/v2/ethereum/webhooks \ -H "Authorization: Bearer $CHAINGATEWAY_TOKEN" \ -H "Content-Type: application/json" \ -d '{ "to": "0x922815dFbEa128eAeaAAa185508fe078bECeFBBE", "url": "https://example.com/hooks/deposits", "type": "ERC20", "contractaddress": "0xdAC17F958D2ee523a2206206994597C13D831ec7", "reference": "order-1042" }'A successful call returns status 201 and the webhook ID in data.webhook. Create the webhook before the customer pays, so that the payment cannot happen before the watch exists.
What does the notification contain?
Each POST contains the fields you need to match the payment to an order. This example is from the Chaingateway documentation for a TRC20 transfer.
{ "id": "9d18457f-c71c-4be4-ae7c-ce04e48359ed", "webhook_id": "9ced86a9-b8d9-4bc2-bbd8-5ffe5b456bc8", "from": "TTd9qHyjqiUkfTxe3gotbuTMpjU8LEbpkN", "to": "DScWVXehzWdV2YS8fA2ryNsiHpKiLgAxM", "blocknumber": "65542464", "datetime": "2024-09-25 14:48:27", "tokenid": null, "type": "TRC20", "contractaddress": "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t", "txid": "18f52f2a55b94a6e3705de4abb18e95dfce79970f27b29450705c561e12b6988", "amount": 108935}The txid identifies the transaction, and webhook_id tells you which watch fired. Store the txid with a unique index. If the same notification arrives twice, the second insert fails, and you skip it.
How do I verify that a notification is genuine?
Check the X-Signature header before you trust the payload. Chaingateway computes an HMAC with SHA-256 over the txid, using your personal secret as the key, and sends the result Base64-encoded. HMAC is defined in RFC 2104. Your server repeats the calculation and compares the two values with a constant-time comparison.
You set the personal secret in your profile settings. Once a secret exists, the header is added to all outgoing webhooks. The signature documentation has verification code for Python, Java, PHP and JavaScript.
What does a minimal receiver look like?
A receiver verifies the signature, records the event once and answers quickly. This Express example keeps seen transaction IDs in memory to stay short. In production, use a database table with a unique index on txid.
const crypto = require('crypto');const express = require('express');
const app = express();const seen = new Set();
app.post('/hooks/deposits', express.json(), (req, res) => { const { txid, to, amount } = req.body; const expected = crypto .createHmac('sha256', process.env.PERSONAL_SECRET) .update(txid) .digest('base64'); const received = req.get('X-Signature') || '';
const valid = expected.length === received.length && crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(received)); if (!valid) return res.sendStatus(401);
if (!seen.has(txid)) { seen.add(txid); // credit the order that belongs to the address `to` for `amount` } res.sendStatus(200);});
app.listen(3000);Test the receiver on a testnet first. Send the X-Network: testnet header when you create the webhook, then send a small test transfer to the watched address. The notification should arrive with the txid of that transfer. The documentation advises creating the webhook right before the payment is made, so create it when you hand out the address.
What happens if my server is down?
The notification is not sent again automatically. If your endpoint does not accept a notification, it appears in the list of failed notifications at GET /api/v2/{chain}/webhooks/notifications/failed. You trigger a new delivery for each entry with POST /api/v2/{chain}/webhooks/notifications/{id}/retry. If the retry fails, the entry returns to the list.
Return a success status as soon as you have stored the event, and do slow work afterwards. RFC 9110 defines the 2xx class of status codes as meaning that the request was received and accepted. Check the failed list regularly, and reconcile your records against the API so that a gap does not stay unnoticed. The webhook delivery guide covers duplicates and ordering in more detail.
Ready to build this yourself? Get your API key — 7-day trial, no card required — or see the Blockchain API for the full endpoint reference.