# Mercury webhooks in production: duplicates, ordering, retries and timeouts

Agent skill: `npx skills add hookdeck/webhook-skills --skill mercury-webhooks` ([details](https://hookdeck.com/webhooks/mercury/skill))

This guide covers the problems Mercury webhooks cause in production and how to handle each one. What Mercury sends is on the [Mercury webhooks](/webhooks/mercury) overview, every event type is on [Mercury webhook events](/webhooks/mercury/events), and signature verification code is on [Verify Mercury webhooks](/webhooks/mercury/verify).

## At a glance

| Problem | What to do | With Event Gateway |
| --- | --- | --- |
| [Timeouts](#timeouts) | Return a 2xx within 5 seconds; process in a background job | Accepts the delivery at once, then queues it for your service |
| [Duplicates](#duplicates) | Record processed event `id` values and skip repeats | Can drop repeats before they reach your handler |
| [Ordering](#ordering) | Keep the last `resourceVersion` for each resource; ignore lower ones | Logs every event and attempt, so you can see arrival order when debugging |
| [Retries](#retries) | Return `4xx` only for invalid requests; reconcile from the Events API | Accepts deliveries while your service is down and replays them |
| [Choosing events](#choosing-events) | Filter with `eventTypes` and `filterPaths`; route on `resourceType` and `operationType` | Filters and routes on any field in the body |
| [Testing](#testing) | Send a test event with the verify call; there's no sandbox | Forwards to localhost and replays past events |
| [Other limits](#other-limits) | Save the secret at creation; plan for 100 endpoints | Fans one endpoint out to many destinations |

## Mercury webhook timeouts and slow handlers {#timeouts}

Return a 2xx within 5 seconds, then do the work in a background job.

**Why it happens:** Mercury marks a delivery as failed if it gets no response within 5 seconds, then retries it. A handler that applies a merge patch to your ledger and calls the Mercury API before responding can run past that limit, even though the work succeeds.

**Fix it in your handler:**

1. Verify the signature, store the event and return 200.
2. Apply the patch or fetch the resource in a worker. See [why to process webhooks asynchronously](/webhooks/guides/why-implement-asynchronous-processing-webhooks).
3. Keep the request path free of calls to other services.

**With Event Gateway:** Hookdeck accepts each Mercury delivery immediately, then queues it and delivers it to your service with its own retries. Your handler can take as long as it needs without failing a Mercury delivery.

## Duplicate Mercury webhooks {#duplicates}

Mercury delivers at least once, so the same event can arrive more than once. Record each event's `id` and skip any you've already processed.

**Why it happens:** A delivery that succeeds on your side but times out on Mercury's is retried, and Mercury's docs say you may occasionally receive the same event several times. Tracking `id` also protects you against an attacker re-sending a captured request, which Mercury points out.

**Fix it in your handler:**

1. Store each event `id` before you process the event, with a unique constraint. See our [guide to webhook idempotency](/webhooks/guides/implement-webhook-idempotency).
2. Skip any `id` you've seen, and still return a 2xx so Mercury stops retrying.
3. Keep the records for at least as long as Mercury retries, which is about a day. Keep them for 90 days if you also reconcile from the Events API, since that's how long events stay available.

**With Event Gateway:** [Deduplication](/docs/deduplication) can compare the fields you choose, such as `id`, and drop repeats within a time window before they reach your handler.

## Out-of-order Mercury webhooks {#ordering}

Mercury doesn't document an ordering guarantee, so don't rely on arrival order. Use `resourceVersion` to tell which change is newer.

**Why it happens:** Mercury retries a failed delivery over about a day, so a retried event can arrive after a later change to the same transaction. `resourceVersion` starts at 1 for a new resource and goes up with each update.

**Fix it in your handler:**

1. Keep the last `resourceVersion` you applied for each `resourceId`, and ignore events with a lower one.
2. If the version jumps, fetch the resource from the API rather than applying the patch. A merge patch only holds the fields that changed, so it can't fill the gap. A `filterPaths` filter can cause jumps too, because it holds back changes to other fields.
3. Treat `previousValues` as a check: if they don't match your stored copy, you've missed something.

**With Event Gateway:** Hookdeck logs every event and delivery attempt, so when state looks wrong you can see the order events arrived in and replay them.

## Mercury webhook retries and disabled endpoints {#retries}

Mercury retries a failed delivery up to 10 times over about a day, but never retries a `4xx` other than `429`. After consecutive failures it disables the endpoint.

**Why it happens:** No response or a non-2xx status counts as a failure, and Mercury retries it with exponential backoff. A `4xx` is the exception, so a `400` from a strict validator or a `401` from a misconfigured verifier loses that delivery after one attempt. Mercury sets an endpoint's `status` to `disabled` after consecutive delivery failures. It doesn't publish the threshold, or say what happens to an event once its retries run out.

**Fix it in your handler:**

1. Return a `4xx` only for requests that really are invalid, such as a bad signature. Return a `5xx` if your own dependencies are down, so Mercury tries again.
2. Check `GET /webhooks`, which can filter by status, and alert when an endpoint reports `disabled`.
3. Reactivate a disabled endpoint with `POST /webhooks/{id}`, setting `status` to `active`.
4. Reconcile the gap with `GET /events`, which can filter by `resourceType` and `resourceId`. Events stay available for 90 days after they occur.

**With Event Gateway:** Hookdeck keeps accepting Mercury's deliveries while your service is down, so your outages don't count as Mercury delivery failures. Failed events can be [retried](/docs/retries) on the status codes you choose, automatically or in bulk once you recover.

## Choosing and routing Mercury events {#choosing-events}

Subscribe with `eventTypes` and `filterPaths`, and route on `resourceType` and `operationType`. The names you subscribe to never appear in a delivery.

**Why it happens:** `eventTypes` only decides what gets sent. The body is an Events API event, so a handler that switches on `transaction.created` never matches. The verbs differ too: `create` in the body, `created` in the subscription. Mercury's Events API lists the `resourceType` values as `transaction`, `checkingAccount`, `savingsAccount`, `treasuryAccount`, `investmentAccount` and `creditAccount`, but it doesn't publish a balance event body or state a rule that maps a subscription name to a pair.

**Fix it in your handler:**

1. Route `transaction` and `create` for `transaction.created`, and `transaction` and `update` for `transaction.updated`.
2. Expect a balance subscription such as `checkingAccount.balance.updated` to arrive as `checkingAccount` and `update`, and log your first delivery of each type to confirm it.
3. Log and acknowledge any combination you don't recognise, so new types don't produce errors.
4. Leave out `eventTypes` only if you want all event types. Add `filterPaths` to send an event only when certain fields change. A reconciliation job that cares about posting can subscribe to `transaction.updated` and filter on the status field.

**With Event Gateway:** Hookdeck [filters](/docs/filters) can route on `resourceType` and `operationType`, so transactions and balance changes can go to different destinations.

## Testing Mercury webhooks {#testing}

Webhooks aren't available in Mercury's sandbox, so test against a production endpoint with the verify call.

**Why you need a plan:** Every endpoint belongs to a production account, so triggering a real transaction as a test means moving real money. Mercury's verify call sends a test event instead. It doesn't document how to tell a test event from a real one.

**Test it safely:**

1. Call `POST /webhooks/{id}/verify`. It sends `transaction.created` unless you pass another `eventType` in the body.
2. Log the test event and check it before you let it reach any code that writes to your ledger.
3. Keep fixtures of the deliveries you receive. Mercury's docs have only one body example, a transaction update, which is on [Mercury webhook events](/webhooks/mercury/events).

**With Event Gateway:** The [Hookdeck CLI](/docs/cli) receives webhooks on your machine without a public server, and you can replay past events to your handler as you change it.

## Other Mercury webhook limits {#other-limits}

Save the signing secret when you create the endpoint, and plan for the endpoint limit and the gaps in Mercury's docs.

**The secret is shown once.** Mercury returns it only in the create response, in a field named `secret`, and `GET` and update calls leave it out. Mercury's Webhooks page calls the same value `secretKey`. Write it to your secret manager in the same script that creates the endpoint. If it's lost, create a new endpoint, switch your receiver over and delete the old one.

**100 endpoints per organization.** Contact api@mercury.com if you need more.

**`filterPaths` values.** The Webhooks page shows bare field names such as `status`, while the API schema lists prefixed values such as `transaction.status`. Check the response to your create call to see what Mercury stored.

**No source IP list.** Mercury doesn't publish IP addresses for webhook traffic, so the signature is how you authenticate a delivery.

**With Event Gateway:** Hookdeck sends one Mercury endpoint's events to as many destinations as you need, so the secret only has to live in one place and your own services never need it.