Developers, Payouts, Guides
Payout APIs and webhooks for platforms: idempotency and signatures
How platforms automate payouts with an API: idempotency keys, verifying webhook signatures, handling retries and duplicates, and testing in a sandbox.
By ekash team. , 6 min read
A marketplace paying sellers, a gig platform paying workers, a software company paying affiliates: once the volume grows, nobody wants to prepare payouts by hand. The usual answer is a payout API, with webhooks that report what happened.
Money movement is unforgiving, so the details matter. This article is vendor-neutral. It covers the four ideas that decide whether a payout integration is reliable: idempotency, signed webhooks, retries, and a sandbox.
The shape of a payout integration
Most payout integrations follow the same loop:
- Your system decides that someone is owed money (a sale settles, a shift ends, a commission accrues).
- Your backend calls the payout API to create a payment for a recipient.
- The payment moves through statuses, such as ready, processing, sent, received, or canceled.
- The provider calls a webhook on your server whenever the status changes.
- Your system updates its own records and notifies the recipient.
Everything below is about making steps 2 and 4 safe when networks fail, servers restart and messages arrive twice.
Idempotency: making retries safe
Suppose your server calls the API to create a payout, and the connection drops before you receive the response. Did the payment get created? You do not know. If you retry blindly, you may pay twice.
Idempotency keys solve this. You generate a unique key for each logical payout (for example, a UUID stored with the payout record) and send it in a header on the create call. The provider remembers the key:
- If the same key arrives again with the same request, it returns the original result instead of creating a second payment.
- If the same key arrives with a different request body, it rejects the call with a conflict error, since that is almost certainly a bug.
Practical rules:
- Generate the key once per payout, and persist it before the first call. If you generate a new key on each retry, you defeat the mechanism.
- Use the same key for every retry of the same payout, including retries after a restart.
- Never reuse a key for a different payout.
- Treat a replayed response as success. It means the first attempt worked.
Webhooks: let the provider tell you
Polling for status is wasteful and slow. Webhooks reverse the direction: the provider sends an HTTPS request to an endpoint you register whenever something changes, such as a payout moving to sent or being returned.
Design your endpoint with these principles:
- Acknowledge fast. Return a 2xx response as soon as you have safely stored the event, then process it asynchronously. Long handlers cause timeouts and trigger retries.
- Expect duplicates. Delivery is typically at-least-once, so the same event can arrive more than once.
- Expect out-of-order events. A “sent” event may arrive after “received” in rare cases. Compare against your current state before applying a change.
- Use the event identifier. Store the ID of every processed event and ignore repeats.
Verifying webhook signatures
Your webhook URL is a public address. Anyone who finds it can send it fake events, such as “payout sent” for a payment that never happened. Signature verification is how you tell genuine events from forged ones.
A common scheme works like this:
- The provider shares a signing secret with you when you register the endpoint.
- For each delivery, it computes an HMAC (typically HMAC-SHA256) over the timestamp and the raw request body, and sends the result in a header along with the timestamp.
- Your server recomputes the HMAC with the same secret and compares.
A minimal verification in Python looks like this:
import hmac, hashlib, time
def verify(secret: bytes, timestamp: str, raw_body: bytes, signature_hex: str) -> bool:
if abs(time.time() - int(timestamp)) > 300: # reject stale deliveries
return False
signed = timestamp.encode() + b"." + raw_body
expected = hmac.new(secret, signed, hashlib.sha256).hexdigest()
return hmac.compare_digest(expected, signature_hex)
Points that trip people up:
- Use the raw body, exactly as received. If your framework parses the JSON and you re-serialize it, the bytes change and verification fails.
- Compare in constant time, as shown, not with a plain equality check.
- Check the timestamp and reject deliveries that are too old, to limit replay attacks.
- Keep the secret in a secrets manager, not in code, and rotate it if it leaks.
- Accept only HTTPS endpoints and consider an IP allowlist if the provider publishes one.
ekash signs its webhooks with HMAC-SHA256 over the timestamp and body, and includes the event ID and event type in headers, so the pattern above applies directly.
Retries and failure handling
Both sides retry, and both need rules.
On the provider’s side: a good webhook system retries failed deliveries with increasing delays for a long window, then gives up and alerts someone. A replay or events endpoint lets you fetch what you missed during an outage.
On your side:
- If your endpoint returns an error or times out, expect retries and make sure your handler is safe to run twice.
- After downtime, query the events or payments list for the period you missed, instead of waiting for the retries to arrive.
- Run a periodic reconciliation job that compares your payout records with the provider’s list. Webhooks are a fast path, not the only path.
- Alert on payouts stuck in a non-final status for longer than expected.
When calling the API itself, retry only on network errors and server errors (5xx) and rate-limit responses, with exponential backoff and jitter. Do not retry client errors (4xx) such as validation failures, since the same request will fail the same way.
Authorization and safety on the API side
- Scope keys narrowly. A reporting service that only reads payments should not hold a key that can create them.
- Separate test and live keys, and make it impossible for test keys to reach production code paths.
- Keep an approval step. Many platforms let the API create payouts for later approval by a person, which is a useful brake for large or unusual amounts. Automatic approval can be reserved for small, well-understood payouts.
Test in a sandbox first
A sandbox lets you exercise the integration without moving real money. Use it to test:
- creating a payout, including a retry with the same idempotency key and one with a different body,
- every status you handle, and the unhappy paths such as a returned or canceled payout,
- webhook delivery to a public test URL, including a handler that deliberately fails so you can watch the retries,
- signature verification, including a deliberately bad signature and a stale timestamp,
- what happens when your balance is too low.
On ekash, API keys are issued by the team and come as separate test and live keys, with a sandbox for testing. Ask for sandbox access early so integration work does not wait on a production decision.
A go-live checklist
- Idempotency key stored with each payout before the first API call.
- Webhook endpoint verifies signatures and timestamps, and answers quickly.
- Duplicate and out-of-order events handled.
- Reconciliation job in place.
- Alerts for stuck or failed payouts.
- Keys scoped, stored in a secrets manager, and rotated on a schedule.
Next step
If your platform is planning automated payouts and you want to see the API, the webhook events and the sandbox, talk to the ekash team about getting keys and walking through your use case.