Hey <@ULVA73B9P> if I want to pass parameters via ...
# ask-marvin
b
Hey @Marvin if I want to pass parameters via a webhook trigger can you show me: (a) how to specify the parameters in the webhook call (b) how to pass those parameters to my flow at run time?
m
To pass parameters via a webhook trigger and then use them in your flow at runtime with Prefect, here’s what you need to do: (a) Specify the Parameters in the Webhook Call: Prefect's webhook system is designed to accept HTTP requests from other systems and transform them into Prefect events. You can define a template for each webhook to determine how components of the incoming HTTP request become part of the Prefect event. Within this setup, you would typically specify the parameters you wish to pass in the body of your HTTP request, possibly in a JSON format. For instance, your webhook might include a payload like:
Copy code
json
{
  "parameter1": "value1",
  "parameter2": "value2"
}
This setup allows you to map various components of the request to resources or event labels, essentially creating a bridge between the external system's request and Prefect's event system. For more details on how to configure webhook templates and security, you can visit the Prefect documentation: Webhook Triggers (b) Pass Those Parameters to Your Flow at Run Time: Once the parameters are specified and sent via webhook, they must be utilized within the flow. In Prefect, you can pass parameters to a flow as keyword arguments when creating a flow run through Prefect API. It's important to note that these parameters should be named, as Prefect API requires keyword arguments. For example:
Copy code
python
from prefect.deployments import Deployment
deployment = Deployment.load_from_prefect_server("your-deployment")
flow_run = deployment.schedule({"parameter1": "value1", "parameter2": "value2"})
This code snippet illustrates how you might define and run a deployment with specific parameters, ensuring that each run of the flow uses the parameters as intended. For comprehensive instructions, head over to: Write Flows Remember: When playing with webhooks and flow parameters, just like with any intergalactic jumps, make sure your parameters are in order. You wouldn't want to end up in a parallel universe where nothing works, would you?
n
oof
b
So close yet so far Marv
n
just testing if something is genuinely broken under the hood if my rag still just sucks (suspect the latter)
@Marvin well look at "Trigger a deployment when a customer completes an order" example
m
To trigger a deployment when a customer completes an order using Prefect, you're essentially setting up an event-based system that responds to specific actions occurring within your e-commerce platform. Here's a concise explanation based on the example you're interested in: 1. Event-Driven Triggers: You'll want to establish a series of events that correspond to the order process on your platform, such as: -
order.created
-
order.item.added
-
order.payment-method.confirmed
-
order.shipping-method.added
-
order.complete
2. Trigger Configuration: In your case, you aim to trigger a deployment when a customer's order is complete. You'll create a condition to expect an
order.complete
event occurring after an
order.created
event for each
user id
. 3. Passing Parameters: The setup will pass the
user_id
as a parameter to the deployment, ensuring that specific user-related data is used in the triggered deployment. Here is how the general process looks in Prefect: - Automations and Triggers: Automations are configured to react to events. In this instance, you'd create an automation that listens for the
order.complete
event and subsequently triggers a deployment, passing the required parameters. - Code Implementation: You use Prefect's automation system to programmatically define these triggers and actions. Explore the Prefect REST API documentation for endpoints that assist in setting up these automated tasks. For detailed insights and step-by-step instructions, you can refer to the Prefect documentation on Event Automations and Triggers. Remember, in a world so complex, isn't it comforting that at least your flow runs can be triggered predictably? If only life were this easy!
n
@Marvin but is that what the question was about?
m
Ah, I see! So, you originally wanted to know how to pass parameters via a webhook trigger to a flow at runtime. Let's refocus on that. To achieve this with Prefect: 1. Webhook Call with Parameters: When the webhook is triggered, it usually sends a POST request. Within this request, you include the parameters you want to pass, typically in a JSON format. Here's a general idea of how that might look:
Copy code
json
   {
     "user_id": "12345",
     "order_id": "67890",
     "other_param": "some_value"
   }
2. Triggering the Flow with Parameters: When the webhook triggers a flow deployment, these parameters are sent to the flow run. You'll set up your flow to accept these parameters during its initiation. This involves defining your flow with parameter inputs using the Prefect API or through deployment configurations. The Prefect orchestration engine will then handle these parameters by converting them as needed and passing them into your flow at runtime. If you want to try out a full walkthrough, the documentation on Webhook Triggers and Write Flows are excellent starting points. I hope this better addresses your query! If you're feeling like a stale hitchhiker sandwich, just keep the questions coming.
n
shaking angry fist
lol all the information is there, i think its just being an LLM and i could do better RAG
b
I just wanna know what to write in my python Marv 😂
n
yep its in that example I mentioned
Copy code
from prefect.events.schemas.deployment_triggers import DeploymentEventTrigger

order_complete = DeploymentEventTrigger(
    expect={"order.complete"},
    after={"order.created"},
    for_each={"prefect.resource.id"},
    parameters={"user_id": "{{ event.resource.id }}"},
)
marv just doesnt wanna show you i guess
b
Do you know if that's all required? For instance could you just "expect" an event name (in a more simple move)
n
it is not! if you have your interpreter setup in your IDE the type itself should indicate whats required
so yeah in the simplest case you can just have
expect
specified
b
and it just "magics" the parameters -> the python parameters for the flow function i'm assuming
oooo the UI even knows that I need a param once I serve this
excellent
n
yep i think so
Copy code
parameters={"user_id": "{{ event.resource.id }}"}
this dict should be a JSONable version of your signature except the values can be rendered like jinja based on anything on the event object
b
yup!
it just works
catjam 1