<@ULVA73B9P> In a webhook automation, is it possib...
# ask-marvin
g
@Marvin In a webhook automation, is it possible to add the flow run parameters in the body as a json blob?
m
thought for 120.4 seconds
Short answer: not directly. Assuming Prefect 3.x and you mean an Automation with a “Call Webhook” action triggered by a flow-run event, the action’s templating context does not include the flow run’s parameters. You can template in event metadata (IDs, names, timestamps, state info), but parameters aren’t exposed in the event payload for security/size reasons. What you can include (examples):
Copy code
{
  "flow_run_id": "{{ event.resource.id }}",
  "flow_name": "{{ event.resource.name }}",
  "state": "{{ match.state.type }}",
  "occurred": "{{ event.occurred }}"
}
Workarounds: - Send the flow_run_id and have the receiver fetch parameters from the Prefect API: - Flow Runs API: https://docs-3.prefect.io/v3/api-ref/rest-api/ - Emit a custom event from inside your flow that contains the parameters you choose to share, then automate on that event. - Call the external webhook from inside your flow (where you have access to
get_run_context().parameters
) instead of using an Automation action. References: - Automations overview: https://docs-3.prefect.io/v3/concepts/automations/ - Webhooks (incoming, for context): https://docs-3.prefect.io/v3/how-to-guides/cloud/create-a-webhook If you meant an incoming webhook that starts a flow run and you want to pass parameters into that run, that’s a different path—happy to outline it.
g
@Marvin yes I am using Automation with an action to call a webhook. What does this docs page mean then? Can't we access the parameters? https://docs.prefect.io/v3/how-to-guides/automations/access-parameters-in-templates
m
thought for 32.5 seconds
You’re right to question me. I was wrong earlier — thanks for the nudge. Yes: in Prefect 3.x Automations, a “Call Webhook” action can access flow run parameters in templates. The docs you linked (Access parameters in templates) are correct: you can use Jinja like
{{ flow_run.parameters[...] }}
in webhook bodies and headers. Examples you can drop into a webhook action: - JSON body with individual params
Copy code
{
  "flow_run_id": "{{ flow_run.id }}",
  "flow_name": "{{ flow_run.name }}",
  "state": "{{ match.state.type }}",
  "user": "{{ flow_run.parameters['user'] }}",
  "threshold": "{{ flow_run.parameters['config']['threshold'] }}"
}
- Safely include all parameters as a JSON blob (use
tojson
so it renders valid JSON)
Copy code
{
  "flow_run_id": "{{ flow_run.id }}",
  "params": {{ flow_run.parameters | tojson }}
}
- Header templating
Copy code
X-Requester: {{ flow_run.parameters['user'] }}
Caveats: -
flow_run.parameters
is only available when the automation is triggered by a flow-run event (e.g., scheduled, running, completed, failed). If your trigger isn’t tied to a flow run,
flow_run
won’t exist. - Be mindful of sensitive/large parameters; you may not want to dump the whole set into a webhook. Docs: - Access parameters in templates: https://docs.prefect.io/v3/how-to-guides/automations/access-parameters-in-templates - Automations overview: https://docs.prefect.io/v3/concepts/automations If you share the exact parameter shape you care about, I can sketch a minimal body that won’t turn into a JSON horror show.
g
@Marvin how do you specify the destination URL for the webhook
m
thought for 911.4 seconds
Do you mean: - sending an outbound webhook from Prefect to some external service, or - receiving an inbound webhook in Prefect to trigger automations/flows? Here’s how to set the destination URL in both cases (Prefect 3.x): Outbound (send a webhook to your URL) - Automations UI: Create an Automation → add action “Call a webhook” → paste your destination URL in the URL field, choose Method, and optionally set Headers/Body template. - Using a Notifications block (generic webhook): Create a Custom Webhook notification block and put your destination URL in its url field, then use “Send a notification” in an Automation and select that block. - In code:
Copy code
from prefect.blocks.notifications import CustomWebhookNotificationBlock

hook = CustomWebhookNotificationBlock(
    name="my-alert",
    url="<https://example.com/webhook>",
    method="POST",
    json_data={"text": "Hello from Prefect"}
)
hook.save("my-alert")  # select this block in an Automation action
- Or use the generic Webhook block directly from a flow:
Copy code
from prefect.blocks.webhook import Webhook

wh = Webhook(url="<https://example.com/webhook>", method="POST")
resp = wh.call({"message": "hello"})
print(resp.status_code, resp.text)
Inbound (receive webhooks into Prefect) - Prefect generates the URL for you; you don’t choose it. In Cloud: Events → Webhooks → New Webhook. Copy the generated URL into your external system so it can POST to Prefect. Then wire that webhook to an Automation that takes action (e.g., run a deployment). - Docs: Create a webhook (inbound) If you can share which direction you’re implementing (outbound vs inbound) and whether you’re using the UI or code, I can give exact, step-by-step instructions.
g
@Marvin In a webhook automation, is it possible to add the flow run parameters in the body as a json blob? This is for sending an outbound webhook to some external service
m
thought for 150.3 seconds
Yes. In a Prefect 3.x Automation with the “Call a webhook” action, you can include the triggering flow run’s parameters in the request body as JSON using Jinja templating. What to do - In the action, add a header:
Content-Type: application/json
- In the Body field, write a JSON template and insert the parameters. Examples 1) Full JSON body with parameters embedded
Copy code
{
  "flow_run_id": "{{ event.resource.id }}",
  "deployment": "{{ event.resource.name }}",
  "parameters": {{ parameters | tojson }}
}
2) Safer fallback if parameters might be missing (e.g., if the trigger isn’t a flow-run event)
Copy code
{
  "flow_run_id": "{{ event.resource.id }}",
  "parameters": {{ (parameters | default({})) | tojson }}
}
3) Build the entire body via Jinja and serialize once
Copy code
{{ {
  "flow_run_id": event.resource.id,
  "deployment": event.resource.name,
  "parameters": parameters | default({})
} | tojson }}
Notes - The automation template context includes the triggering
event
and, for flow-run events, a
parameters
dict you can reference directly. - Use
| tojson
to ensure the parameters dict is rendered as valid JSON. - Make sure your Automation trigger is a flow-run/deployment event type that actually has parameters; otherwise, use the
default({})
pattern shown. Docs - Accessing parameters in templates: Access parameters in templates - Webhooks concept: Webhooks If you share the exact event you’re triggering on and the payload shape you need, I can tailor the exact template.