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

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

This guide covers the problems Prefect's Custom Webhook notifications cause in production and how to handle each one. It's about the requests an automation sends to your endpoint, not [Prefect Cloud webhooks](https://www.prefect.io/docs/v3/concepts/webhooks), which receive requests into Prefect. What the block sends and why Prefect has no event list is on the [Prefect webhooks](/webhooks/prefect) overview, and checking the secret header is on [Verify Prefect webhooks](/webhooks/prefect/verify).

## At a glance

| Problem | What to do | With Event Gateway |
| --- | --- | --- |
| [Timeouts](#timeouts) | Return a 2xx within 10 seconds; process in a background job | Accepts each request at once, then queues it for your service |
| [Duplicates](#duplicates) | Deduplicate on the automation and event you put in the message | Drops repeats you define before they reach your handler |
| [Ordering](#ordering) | Send a state timestamp and ignore anything older | Logs every request and attempt, so you can see arrival order |
| [Retries](#retries) | Plan for lost notifications; reconcile with the Prefect API | Stores each request, retries on your schedule and replays after a fix |
| [Choosing events](#choosing-events) | Put IDs and state in the automation's message | Filters and routes on anything your template includes |
| [Testing](#testing) | Call `notify()` on the block, then fail a real flow run | Forwards requests to localhost |
| [Other limits](#other-limits) | Use a long random secret over HTTPS; check every method you use | Checks the header you name before delivery |

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

Return a 2xx within 10 seconds and do the work in a background job, because the block treats a timeout as an error.

**Why it happens:** The block's `timeout` field defaults to 10 seconds. If your handler doesn't answer in time the request fails, even if your handler goes on to finish the work.

**Fix it in your handler:**

1. Check the secret header, store the request and return `200`.
2. Do the work from a queue. See [why to process webhooks asynchronously](/webhooks/guides/why-implement-asynchronous-processing-webhooks).
3. If a call has to happen inside the request, raise the block's `timeout` to match it.

**With Event Gateway:** Hookdeck accepts each Prefect request immediately, then queues it and delivers it at a rate your service can handle, so your handler can take as long as it needs.

## Duplicate Prefect webhooks {#duplicates}

Prefect's docs don't describe duplicate delivery, and nothing in the request identifies a delivery. Put your own key in the message, and skip a key you've already processed.

**Why it happens:** Each automation that matches an event sends its own request, so two automations on the same flow run state produce two requests. Since retries aren't documented, you also can't rule out a repeat after a request that failed from Prefect's side but that your handler had already processed.

**Fix it in your handler:**

1. Add `{{ automation.name }}` and `{{ event.id }}` to the automation's message. `event` is the event that fired the trigger.
2. A proactive trigger fires when an event doesn't happen, so check that the event ID isn't empty. For flow run automations you can use `{{ flow_run.id }}` and `{{ flow_run.state.name }}` as well.
3. Record each key and skip one you've seen. See our [guide to webhook idempotency](/webhooks/guides/implement-webhook-idempotency).

**With Event Gateway:** Hookdeck can [deduplicate](/docs/deduplication) on the fields you choose before requests reach your endpoint, as long as your template includes them.

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

Prefect documents no ordering guarantee, so don't assume notifications arrive in the order runs changed state.

**Why it happens:** Each automation sends its own requests, and the request carries no sequence number or timestamp unless your template adds one.

**Fix it in your handler:**

1. Add `{{ flow_run.state.timestamp }}` to the automation's message.
2. Store the latest timestamp per flow run and ignore anything older.
3. If the current state matters, read the run from the Prefect API rather than trusting the notification.

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

## Prefect webhook retries and failed notifications {#retries}

Prefect's docs don't say whether a failed notification is sent again, so plan for a notification that never arrives and for one that arrives twice.

**Why it happens:** The block raises an error on a timeout or on any response outside 2xx. That includes a redirect, which the block doesn't follow. Prefect's docs give no retry count, schedule or resend option.

**Fix it in your handler:**

1. Keep the endpoint available and respond quickly, and use the final URL in the block so it doesn't get a redirect.
2. Reconcile with the Prefect API, for example by querying recent failed flow runs, so a missed notification is caught.
3. Prefect documents a `prefect.automation.action.failed` event for an action that fails, which you can alert on through a different block. Test it before you rely on it: point a block at an endpoint that returns a `500`, fire the automation, and check the event appears in the event feed.

**With Event Gateway:** Hookdeck accepts each request as soon as it arrives and stores it, so your outage doesn't fail the send. Failures between Hookdeck and your service become [retries](/docs/retries) you control, [issues](/docs/issues) alert you, and you can replay any notification after a fix.

## Telling Prefect notifications apart {#choosing-events}

Prefect has no webhook event list and adds no event name, run ID or state, so you decide what your handler receives.

**Why it happens:** Two templates are involved. The block's `json_data` or `form_data` can only use `{{subject}}`, `{{body}}`, `{{name}}` and keys in its `secrets`, and the block raises an error on any other placeholder when it's created. The automation's subject and body use [Jinja](https://www.prefect.io/docs/v3/concepts/automations#templating-with-jinja), with variables such as `flow_run`, `deployment`, `work_pool`, `automation` and `event`. Prefect renders the action's text first, then the block puts it into your template.

**Fix it in your handler:**

1. Put what you need in the automation's body, and use the variables that match its trigger. `{{ flow_run.id }}`, `{{ flow_run.state.name }}` and `{{ automation.name }}` cover most flow run automations.
2. To send structure, write JSON in the body, or use `{{ flow_run | tojson }}` for the whole run. It arrives as a string inside your template, so parse it on arrival.
3. Add fixed fields to `json_data`, such as a source and environment, so several blocks can share one endpoint.
4. Treat the templates as an API contract. An edit to a block or to an automation's message changes what your handler receives.

```json
{
  "source": "prefect",
  "environment": "production",
  "subject": "{{subject}}",
  "body": "{{body}}"
}

```

`source` and `environment` are your own convention, and Prefect doesn't add them.

**With Event Gateway:** Hookdeck [transformations](/docs/transformations) can parse and reshape the body before it reaches your service, and [filters](/docs/filters) can route on any field your template includes.

## Testing Prefect webhooks {#testing}

Send a message from the block first, then fail a real flow run to test the automation's message.

**Why it's awkward:** Prefect Cloud can't reach `localhost`, and the automation's text only renders when its trigger fires.

**How to test:**

1. Load the block and call `notify("test")`. It sends a request without an automation and raises an error on a failed send. Without a subject argument, `{{subject}}` is sent as the string `null`.
2. Run a flow that fails on purpose to fire an automation on `Failed`, then read the rendered body your endpoint received.
3. Send a request with a wrong secret to confirm your handler rejects it.

**With Event Gateway:** `hookdeck listen 3000 prefect --path /webhooks/prefect` gives you a public URL that forwards to your machine. Use it as the block's URL.

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

Plan for a static credential, no published source IPs and the method you've chosen.

* **A secret header is all you get:** There's no signature or timestamp, so the header doesn't cover the body and a captured request can be replayed. Use a long random value stored in the block's `secrets`, send it over HTTPS only, and confirm anything consequential through the Prefect API. Hookdeck's Prefect source compares the header you name with the exact value you enter, and a request that fails the check is recorded as failed verification and never reaches your service. Enter the secret's real value on the source, not the `{{webhook_secret}}` placeholder.
* **Source IPs:** Prefect publishes no list, and a self-hosted server sends from wherever it runs, so allowlisting isn't an option to count on.
* **Methods:** The block can use `GET`, `POST`, `PUT`, `PATCH` or `DELETE`, with `POST` as the default. Unless the receiver needs another, keep `POST` with `json_data`, because it's the simplest to authenticate, parse and route. Hookdeck's Prefect source pre-selects `POST`, so add any other method your block uses, and `GET` has to be [enabled on the source](/docs/sources#custom-methods).
* **Body formats:** `json_data` and `form_data` can't be combined, and `form_data` is either a dict sent form-encoded or a raw string.