How to send crypto withdrawals safely from your app
Receiving crypto is forgiving: if something goes wrong you can usually credit later. Sending is not. A transaction on the blockchain can't be reversed, so a withdrawal bug pays someone twice or loses money for good. This guide covers the pattern that keeps withdrawals correct, whatever happens on the network or your servers.
The four steps
- Reserve the funds. In one database transaction, check the customer's balance, subtract the amount plus any fee, and create a withdrawal row with status
processing. - Send with an idempotency key. Create the payout using the withdrawal row's id as the key. If the call times out, retry with the same key within 24 hours, one attempt at a time, and you get the original payout back instead of a second one.
- Mark it complete when it's sent. Save the transaction hash so the customer can see it on a block explorer.
- Refund on failure. If the payout fails, give the amount back and tell the customer.
Step 1: reserve first
BEGIN;
UPDATE balances SET amount = amount - $amount
WHERE user_id = $user AND asset = 'USDT' AND amount >= $amount;
-- if no row was updated: insufficient balance, ROLLBACK
INSERT INTO withdrawals (id, user_id, chain, asset, to_address, amount, status)
VALUES ($id, $user, 'tron', 'USDT', $to, $amount, 'processing');
COMMIT;Reserving before you send means two simultaneous requests can't both spend the same balance.
Step 2: create the payout
curl https://api.blocksreader.com/v1/payouts \
-H "Authorization: Bearer $BLOCKSREADER_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: withdrawal-55102" \
-d '{ "chain": "tron", "asset": "USDT", "from": "TQ4mZ8rWkX2bJ3yHn5cPq1Vd7GfS9aV7pn", "to": "TXyz8rWkX2bJ3yHn5cPq1Vd7GfS9aV7abc", "amount": "125.50" }'from is the address of yours that holds the funds. It's required in live mode and optional in test mode. The response has an id starting po_; save it on the withdrawal row straight away. The payout starts as pending.
Know what the key covers, because this is where double payments come from:
- It lasts 24 hours. After that, the same key creates a new payout.
- Send one attempt at a time. Lock the withdrawal row while you call the API, so two workers never send the same withdrawal at once.
- After a 5xx error or a timeout, look before you retry. Check
GET /v1/payouts?chain=tronfor a payout with the same address and amount. If it's there, save its id instead of sending again. - Never retry an old withdrawal automatically. If it's still
processingwith no payout id after a day, have a person check it.
With BlocksReader, a payout never moves on our say-so. Your own BlocksReader Signer, running on your server, picks it up within about 30 seconds, signs it with your keys and broadcasts it. In test mode a simulated signer does this for you, and can also simulate failures so you can test the refund path.
Step 3 and 4: listen for the result
if (event.type === 'payout.sent') {
// data.payout_id, data.tx_hash. If no row has this payout_id yet, reply 500 so the event is retried.
await db.withdrawals.update({ payout_id: event.data.payout_id, status: 'processing' }, { status: 'completed', tx_hash: event.data.tx_hash });
} else if (event.type === 'payout.failed') {
// data.failure_reason. No funds left the wallet.
await refundWithdrawal(event.data.payout_id); // only if still 'processing', in one transaction
}payout.sent means your signer signed and broadcast the transaction. For large withdrawals, also check on a block explorer that it confirmed successfully before you treat it as final.
Make both handlers safe to run twice: only move a withdrawal out of processing once. As a safety net, a job can check old processing withdrawals with GET /v1/payouts/{id} in case a webhook was missed.
Validate before you send
- Address format. Check the address is valid for the chain the customer chose. A Tron address on an EVM chain, or the reverse, is the classic mistake.
- Destination tags and memos. On XRP and TON, exchanges need a tag or memo. Ask for it, and warn when it's missing.
- Limits. Set your own per-withdrawal and per-day limits, and hold large withdrawals for manual review. Put these checks before the API call: once your signer is running, it sends every pending payout it receives.
- Network fees. The sending address needs the amount plus the network fee. For tokens such as USDT that means some of the chain's native coin (TRX on Tron, BNB on BNB Chain) as well.
Keep the hot wallet small
Hold only what you need for a day or two of withdrawals in the sending wallet, and keep the rest in cold storage. Sweeps can move incoming deposits to the right wallets automatically, and you top up the hot wallet when it runs low.
A checklist
- Balance reserved in the same transaction that creates the withdrawal.
- Idempotency key = your withdrawal id, on every attempt, one attempt at a time, within 24 hours.
payout.sentmarks complete;payout.failedrefunds; both safe to repeat.- Address, tag and memo validated; limits enforced before sending.
- Sending wallet funded with the asset plus gas.
You can build and test the full flow, including failed payouts, for free in BlocksReader's test mode.