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

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

This guide covers the problems Lago webhooks cause in production and how to handle each one. What Lago sends and how it's signed is on the [Lago webhooks](/webhooks/lago) overview, every message type is on [Lago webhook events](/webhooks/lago/events), and signature verification code is on [Verify Lago webhooks](/webhooks/lago/verify).

## At a glance

| Problem | What to do | With Event Gateway |
| --- | --- | --- |
| [Timeouts](#timeouts) | Acknowledge at once; process in a background job | Accepts the message at once, then queues it for your service |
| [Duplicates](#duplicates) | Process each `X-Lago-Unique-Key` once | Can drop repeats before they reach your handler |
| [Ordering](#ordering) | Tolerate reordering; fetch current state from the API | Logs every message and attempt, so you can see arrival order when debugging |
| [Retries](#retries) | Watch the webhook logs; reconcile after an outage | Accepts messages while your service is down and replays them |
| [Choosing events](#choosing-events) | Set `event_types`; route on `webhook_type`; accept unknown types | Filters and routes messages by type |
| [Testing](#testing) | Forward messages to localhost with a CLI | Forwards to localhost and replays past messages |
| [Other limits](#other-limits) | Plan for the JWT default and 10 endpoints | Verifies HMAC-signed endpoints and fans one endpoint out to many services |

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

Acknowledge a message straight away, then do the work in a background job.

**Why it happens:** Lago doesn't publish the request timeout for Lago Cloud. Self-hosted instances default to 30 seconds, set with `LAGO_WEBHOOK_TIMEOUT_SECONDS`. A handler that writes to a database or calls other services before responding risks using up one of Lago's three attempts. Lago's webhooks guide lists `200`, `201`, `202` and `204` as acknowledgements, and marks any other response as failed. Its format page says any 2xx, so stick to those four.

**Fix it in your handler:**

1. Verify the signature, store the message and return `200`.
2. Process the message from a queue. See [why to process webhooks asynchronously](/webhooks/guides/why-implement-asynchronous-processing-webhooks).
3. Keep calls to other services out of the request path.

**With Event Gateway:** Hookdeck accepts each Lago message immediately, then queues it and delivers it to your service at a rate you set, with its own retries. Your handler can take as long as it needs without failing a Lago attempt.

## Duplicate Lago webhooks {#duplicates}

Lago can send the same message more than once. Record each `X-Lago-Unique-Key` and skip repeats.

**Why it happens:** Lago sends a message again after an error response or a network issue, even if your handler finished the work before the failure. Without a check, a customer can receive two "payment failed" emails, or a wallet top-up can be applied twice.

**Fix it in your handler:**

1. Store each `X-Lago-Unique-Key` with a unique constraint. Lago tells you to "use it to process a message only once".
2. Make every handler [idempotent](/webhooks/guides/implement-webhook-idempotency): the same result however many times it runs.
3. Write the side effect and the key in one transaction where you can.

**With Event Gateway:** Hookdeck's [deduplication](/docs/deduplication) can drop repeated deliveries based on the `X-Lago-Unique-Key` header before they reach your handler.

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

Lago tells integrations to tolerate reordering. Fetch current state from the API when your next step depends on it.

**Why it happens:** Lago's own integration advice is to "tolerate retries and reordering", and a retried message can arrive after newer ones. A `subscription.terminated` can reach you before the `subscription.started` for the same customer. For more on the general problem, see [Webhook Ordering: Why It's Hard and How to Handle It](/webhooks/guides/webhook-ordering-why-its-hard-and-how-to-handle-it).

**Fix it in your handler:**

1. Before changing access or sending a customer email, fetch the current subscription or invoice from the Lago API.
2. Treat the API as the source of truth when a message and your stored state disagree, as Lago advises.
3. Write state transitions that tolerate messages in any order.

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

## Lago webhook retries and failed messages {#retries}

Lago makes up to 3 attempts per message. After that it's on you to notice the failure, so watch the webhook logs and reconcile after any outage.

**Why it happens:** The 3 attempts are the first call plus 2 retries, waiting longer between each. Lago doesn't publish the intervals. After the last attempt, the message shows an error status in the webhook logs. A short outage on your side can leave invoices and subscription changes that never reached your system. Lago's docs don't describe disabling an endpoint after failures.

**Fix it in your handler:**

1. Open the endpoint's **Deliveries** tab under **Developers > Webhooks**, filter by status, and retry failed messages from there.
2. On self-hosted Lago, raise the total with `LAGO_WEBHOOK_ATTEMPTS` (default 3).
3. Reconcile important objects, such as invoices and subscriptions, against the Lago API on a schedule.

**With Event Gateway:** Hookdeck accepts each message on Lago's first attempt and stores it. Delivery to your service is then [retried](/docs/retries) on a schedule you set, failures raise [issues](/docs/issues), and you can replay messages once your service is back.

## Choosing which Lago messages to listen to {#choosing-events}

Set each endpoint's `event_types` to the messages your integration processes, and route on `webhook_type`.

**Why it matters:** `event_types` set to `null` or `["*"]` sends every type, and an empty array stops all delivery. Lago [lists 83 types](/webhooks/lago/events) and says new ones are added over time. Some names don't mean what they suggest: `invoice.created` is sent when an invoice is finalized, and `wallet.updated` isn't sent for changes to a wallet's ongoing balance. `event.error` is deprecated in favor of `events.errors`, though Lago still sends it.

**Fix it in your handler:**

1. Switch on `webhook_type`. Every message about an invoice has `object_type` set to `invoice`, so `object_type` can't tell you what happened.
2. Acknowledge types you don't handle, so a new type never counts as a failure.
3. Watch for `wallet.depleted_ongoing_balance` rather than `wallet.updated` if you act on a wallet running out.

**With Event Gateway:** Hookdeck's [filters](/docs/filters) on `webhook_type` send each connection only the messages it needs.

## Testing Lago webhooks locally {#testing}

Forward messages to your machine with the Hookdeck CLI, then make a change in Lago to trigger one.

**Why it matters:** Lago needs a public URL, and your development server doesn't have one.

**Set it up:**

1. Run `hookdeck listen 3000 lago --path /webhooks/lago` with the [Hookdeck CLI](/docs/cli), which gives you a public HTTPS URL that forwards to your local server.
2. Use that URL as the endpoint URL in a non-production Lago organization, or on a self-hosted instance.
3. Make a change in Lago, such as creating a customer to send `customer.created`.

**With Event Gateway:** Hookdeck keeps past messages, so you can replay one against your handler while you work on it.

## Other Lago limits {#other-limits}

Lago signs with JWT unless you choose HMAC, and an organization can have only 10 endpoints.

**Why it happens:** JWT puts the payload in the signature header, so the header can grow large and some servers reject it. Lago's docs give that as the difference between the two schemes. An HMAC check of the body also can't match a JWT, so a new endpoint fails every check if you haven't chosen HMAC. Each endpoint has its own URL and message types, so finance, product access and customer messaging can use up the 10 endpoints quickly. Lago's webhook pages publish no source IP list, and neither scheme asks you to check a timestamp.

**Fix it in your handler:**

1. Check `X-Lago-Signature-Algorithm` on a failing message. If it says `jwt`, verify the token as shown in Lago's docs, or change the endpoint with `PUT /api/v1/webhook_endpoints/{lago_id}` and `"signature_algo": "hmac"`.
2. Send `"signature_algo": "hmac"` when you create endpoints through the API, because the API also defaults to `jwt`.
3. If 10 endpoints isn't enough, point one endpoint at a router you run and fan messages out from there.

**With Event Gateway:** Hookdeck verifies HMAC-signed Lago endpoints, so choose HMAC for an endpoint that points at it. One Lago endpoint can then feed a Hookdeck source, with a connection for each service that needs its messages.