Prefect webhooks in production: duplicates, ordering, retries and timeouts
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, which receive requests into Prefect. What the block sends and why Prefect has no event list is on the Prefect webhooks overview, and checking the secret header is on Verify Prefect webhooks.
At a glance
| Problem | What to do | With Event Gateway |
|---|---|---|
| Timeouts | Return a 2xx within 10 seconds; process in a background job | Accepts each request at once, then queues it for your service |
| Duplicates | Deduplicate on the automation and event you put in the message | Drops repeats you define before they reach your handler |
| Ordering | Send a state timestamp and ignore anything older | Logs every request and attempt, so you can see arrival order |
| Retries | Plan for lost notifications; reconcile with the Prefect API | Stores each request, retries on your schedule and replays after a fix |
| Choosing events | Put IDs and state in the automation's message | Filters and routes on anything your template includes |
| Testing | Call notify() on the block, then fail a real flow run | Forwards requests to localhost |
| 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
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:
- Check the secret header, store the request and return
200. - Do the work from a queue. See why to process webhooks asynchronously.
- If a call has to happen inside the request, raise the block's
timeoutto 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
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:
- Add
{{ automation.name }}and{{ event.id }}to the automation's message.eventis the event that fired the trigger. - 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_as well.run.state.name }} - Record each key and skip one you've seen. See our guide to webhook idempotency.
With Event Gateway: Hookdeck can deduplicate on the fields you choose before requests reach your endpoint, as long as your template includes them.
Out-of-order Prefect webhooks
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:
- Add
{{ flow_to the automation's message.run.state.timestamp }} - Store the latest timestamp per flow run and ignore anything older.
- 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
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:
- Keep the endpoint available and respond quickly, and use the final URL in the block so it doesn't get a redirect.
- Reconcile with the Prefect API, for example by querying recent failed flow runs, so a missed notification is caught.
- Prefect documents a
prefect.automation.action.failedevent 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 a500, 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 you control, issues alert you, and you can replay any notification after a fix.
Telling Prefect notifications apart
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, 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:
- Put what you need in the automation's body, and use the variables that match its trigger.
{{ flow_run.id }},{{ flow_andrun.state.name }} {{ automation.name }}cover most flow run automations. - 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. - Add fixed fields to
json_data, such as a source and environment, so several blocks can share one endpoint. - Treat the templates as an API contract. An edit to a block or to an automation's message changes what your handler receives.
{
"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 can parse and reshape the body before it reaches your service, and filters can route on any field your template includes.
Testing Prefect webhooks
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:
- 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 stringnull. - Run a flow that fails on purpose to fire an automation on
Failed, then read the rendered body your endpoint received. - 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
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,PATCHorDELETE, withPOSTas the default. Unless the receiver needs another, keepPOSTwithjson_data, because it's the simplest to authenticate, parse and route. Hookdeck's Prefect source pre-selectsPOST, so add any other method your block uses, andGEThas to be enabled on the source. - Body formats:
json_dataandform_datacan't be combined, andform_datais either a dict sent form-encoded or a raw string.
Keep an outage on your side from losing Prefect alerts.
Event Gateway checks the secret header, stores each notification as it arrives and delivers it to your service with retries you control, and you can replay any of them after a fix.