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

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

This guide covers the problems PostHog webhooks cause in production and how to handle each one. It's about the HTTP Webhook destination, which sends your captured events out of PostHog to your own services. What PostHog sends is on the [PostHog webhooks](/webhooks/posthog) overview, and signature verification code is on [Verify PostHog webhooks](/webhooks/posthog/verify). PostHog defines no event list of its own, as [Choosing events](#choosing-events) explains.

## At a glance

| Problem | What to do | With Event Gateway |
| --- | --- | --- |
| [Timeouts](#timeouts) | Return a 2xx quickly; process in a background job | Accepts the delivery at once, then queues it for your service |
| [Duplicates](#duplicates) | Record each `webhook-id` or `event.uuid`; make handlers idempotent | Drops repeats before they reach your handler |
| [Ordering](#ordering) | Treat events as independent; compare `event.timestamp` when order matters | Logs every event and attempt, so you can see arrival order when debugging |
| [Retries](#retries) | Stay available; query missed events from PostHog after an outage | Accepts deliveries while your service is down and replays them |
| [Choosing events](#choosing-events) | Filter at the destination; validate the body you receive | Filters and routes on any field in the body |
| [Testing](#testing) | Send an example event, then read the logs | Forwards to localhost and replays past events |
| [Other limits](#other-limits) | Use a `whsec_` secret, keep `POST`, allow PostHog's IPs | Verifies the signature before events reach your service |

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

Return a 2xx status quickly, then do the work in a background job. PostHog doesn't publish a timeout.

**Why it happens:** PostHog's docs say a destination has to respond with healthy status codes in a timely fashion, without giving a number. Repeatedly slow responses are one of the two things PostHog measures when it decides a destination is performing poorly, and a destination that performs poorly for long enough gets disabled.

**Fix it in your handler:**

1. Verify the signature, store the event and return 200.
2. Do the rest in a worker. See [why to process webhooks asynchronously](/webhooks/guides/why-implement-asynchronous-processing-webhooks).
3. Watch the destination's **metrics** and **logs** tabs, which show usage and errors.

**With Event Gateway:** Hookdeck accepts each PostHog delivery immediately, then queues events and delivers them to your service at a rate it can handle, with its own retries. Your handler can take as long as it needs without slowing the destination.

## Duplicate PostHog webhooks {#duplicates}

PostHog retries failed calls, so your handler can see the same event twice. Record each `webhook-id` you process and skip repeats.

**Why it happens:** Your handler may finish its work and then time out or return an error, and PostHog sends the event again. PostHog's code creates `webhook-id` once for each call and keeps it across that call's retries. The `webhook-timestamp` and `webhook-signature` values change on each attempt, so comparing those won't catch a repeat. PostHog's docs don't discuss duplicates, so this comes from the destination's code.

**Fix it in your handler:**

1. Track processed ids in a table with a unique constraint.
2. On an unsigned destination there's no `webhook-id`. Use `event.uuid` from the default template, or add `{event.uuid}` to your own.
3. Make every handler [idempotent](/webhooks/guides/implement-webhook-idempotency): the same result however many times it runs.

**With Event Gateway:** Hookdeck can [deduplicate](/docs/deduplication) on a field you choose, such as the `webhook-id` header, so repeats are filtered before they reach your handler.

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

PostHog's docs say nothing about delivery order, so don't assume it. Treat each event as independent, and compare `event.timestamp` when order matters.

**Why it happens:** PostHog's code puts a failed call back on a queue with a short backoff, so a later event can arrive before the retried one. The `person` object also won't always match the event: PostHog says it holds the latest profile at the time the event is processed.

**Fix it in your handler:**

1. Keep the last `event.timestamp` you applied for each record, and ignore anything older when you update state.
2. Don't treat `person` as a snapshot from the moment of the event.
3. If you need merged person properties, filter for events that occur on or after the merge, such as `$identify`, as PostHog advises.

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

## PostHog webhook retries and disabled destinations {#retries}

PostHog retries a non-2xx response up to 3 times, depending on the error code. A destination that keeps failing is quarantined and eventually disabled.

**Why it happens:** PostHog says it may change its retry logic as it balances reliability and speed. Its code currently retries `408`, `429`, `500`, `502`, `503` and `504` responses and connection errors, with defaults that use up the retries within seconds, so most outages outlast them. PostHog documents no redelivery after the last attempt. Separately, too many errors or slow responses over a prolonged period get a destination quarantined. PostHog says it doesn't count `4xx` responses as poor performance, and it logs them. It tries to re-enable a disabled destination automatically before stopping it entirely, and from then on you have to change its filters or code and re-enable it in the UI.

**Fix it in your handler:**

1. Return `5xx` only for failures a retry could fix, and `4xx` for requests you'll never accept.
2. After an outage, find what you missed by querying the events in PostHog itself, through its API or a batch export.
3. Check the destination's status if events stop arriving, because a disabled destination sends nothing.
4. PostHog signs each of its attempts with a fresh timestamp, but a queue's later retry or replay keeps the original headers. If you verify behind a queue, use a verifier whose timestamp window you can widen or turn off, as described on [Verify PostHog webhooks](/webhooks/posthog/verify).

**With Event Gateway:** Hookdeck captures every PostHog delivery while your services are down, so the destination keeps succeeding and has nothing to be quarantined for. Failed events can be [retried](/docs/retries) automatically, manually or in bulk once you recover.

## Choosing PostHog events and the body they arrive in {#choosing-events}

Use the destination's filters so it sends only what you act on, and route on `event.event` if you keep the default body.

**Why it happens:** PostHog has no fixed list of webhook events. The events are whatever your project captures, such as `$pageview`, `$pageleave`, `$identify`, `$autocapture` or your own `signed_up`, and without filters high-volume events reach your endpoint too. The body is also a template. Its default is `{ "event": "{event}", "person": "{person}" }`, which renders like this, with placeholder values:

```json
{
  "event": {
    "uuid": "<event uuid>",
    "event": "$pageview",
    "distinct_id": "<distinct id>",
    "properties": { "$current_url": "https://example.com/pricing" },
    "timestamp": "<timestamp>",
    "url": "<link to the event in PostHog>"
  },
  "person": {
    "id": "<person uuid>",
    "name": "<display name>",
    "url": "<link to the person in PostHog>",
    "properties": {}
  }
}

```

PostHog marks `person` as optional. Anyone with access to the destination can edit the template, the method or the headers, and the change reaches your endpoint with no deploy on your side.

**Fix it in your handler:**

1. Add filters and match rules in the destination's **Filters** section.
2. Keep the default template where you can. If you need your own, keep a copy in version control. PostHog's template variables also offer `groups`, `project` and `source`.
3. Validate the shape on receipt, and log and acknowledge bodies you don't recognise rather than failing them.

**With Event Gateway:** Hookdeck [filters](/docs/filters) and routes events on any field in the body, so one PostHog destination can feed several services. It also stores every request, so you can see what PostHog sent before and after a template change.

## Testing and debugging PostHog webhooks {#testing}

Send an example event from the destination, then read what came back.

**Why it's worth doing:** Signing errors and a rejected body only show up once a request is sent. PostHog's test sends one example event, so check a real event too once the destination is live.

**How to test:**

1. In the destination's **Testing** section, click **Start testing** > **Test function**.
2. Turn on **Log responses** while you set up, then check the logs tab for the status and body your endpoint returned.
3. To test on your machine, use a tunnel. `npx hookdeck-cli listen 3000 posthog --path /webhooks/posthog` prints a URL you can paste into **Webhook URL**.

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

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

Plan for the signing secret's format, the HTTP method and your firewall before they reach production.

**PostHog doesn't issue the secret.** Generate one with `echo "whsec_$(openssl rand -base64 32)"`. PostHog base64-decodes the secret with or without the `whsec_` prefix, and refuses to send if it isn't valid base64 or decodes to fewer than 24 bytes. A receiver can treat an unprefixed secret as plain text and get a different key, so keep the prefix and paste the same string on both sides.

**Signing is optional, and PostHog's docs page doesn't mention it.** The Signing secret input and how it works are in PostHog's open-source destination code. A destination without one sends no signature headers. If you can't use signing, put a long random token in **Secret headers**, which PostHog encrypts and sends exactly as entered, and check it on your receiver.

**Keep the method as `POST`.** The destination also offers `PUT`, `PATCH`, `GET` and `DELETE`. With `GET`, PostHog sends no body but still signs the templated one, so the signature can't verify.

**Firewalls.** If a firewall or WAF protects your endpoint, allow PostHog's IPs for your region. EU: `3.75.65.221`, `18.197.246.42`, `3.120.223.253`. US: `44.205.89.55`, `52.4.194.122`, `44.208.188.173`.

**With Event Gateway:** Paste the same `whsec_` string into the PostHog destination and the Hookdeck source's **Webhook Signing Secret** field, and Hookdeck verifies each signature so your services never hold the secret. If you leave the Signing secret empty in PostHog, leave verification off on the source too.