What is the variable scope for a workflow when I initiate more than one invocation?

This topic was automatically generated from Slack. You can find the original thread here.

Petar : What is the variable scope In this example Example: Delay a workflow before the next step runs?

I mean if someone submit the form we schedule email in a day. What happens if someone else submit the form in the meantime?

I want to send personalized emails so I need to keep the receipt name as a variable

Dylan Sather (Pipedream) : Hi , data is scoped to a single invocation of a workflow. When your workflow is triggered, it runs with the data you send to it (via HTTP request, for example), and that triggers an invocation with that data. If you trigger the workflow with different data, a separate invocation runs, completely independent of the first. So there’s no collision between the data.

Petar : thanks. What about if I want to update the data?

In the given example let say the initial workflow is triggered with name, email and item ordered.

I schedule email to be send in one day with thanks for purchase and asking for feedback.

Lets assume this same customer orders one more item in the meantime.

Can I update the scheduled task with the new item purchased and say, thanks for ordering this and that?

Petar : I guess I should save somewhere data and use pointer. Then on new order I have to update data. How to do it?

Dylan Sather (Pipedream) : Is there a way for you to pull the orders from an API on your end when the workflow runs? For example:

  1. You schedule the email to run by sending an HTTP request to your task scheduler, with the user ID / email of the user, telling it to schedule the event for 24 hours later.
  2. When the event is scheduled, it triggers your workflow with the user ID / email you passed in step 1. Within the workflow, you could fetch the current order data and format the text of the email.
    This way, the order information isn’t embedded in the scheduled event, and can be fetched only when you’re ready to send the email. Would that work?

Petar : This is great advice! Thanks.

I am new to this and learning. How to call api from workflow?

https://websitebuilder.docs.apiary.io/#reference/orders/single-order/retrieve-order

Petar : I know the order id

Dylan Sather (Pipedream) : When you add a new step to your workflow, you should see an option to add the HTTP / Webhook Request action. That allows you to make any HTTP request in a workflow. It’ll return the data from the API as “step exports” that you can use in future step (you’ll see the return data below the step when you run your workflow). Let me know if that helps

Petar : New order -> webhook get ID and schedule event in 24h.

Scheduled event get all current data via api. :slight_smile:

Petar : How to implement this to access the ecom platform api?

Authentication
The API uses Bearer Token Authorization

You can use your API key to access protected resources in your website only by passing the Authorization header:

Authorization: Bearer <API KEY>

Petar : How to pass my api key and be sure it is stored safe?

Dylan Sather (Pipedream) : Take a look at Settings | Env - Pipedream . Pipedream supports environment variables, which lets you store any API key securely and they access it your workflow using process.env.YOUR_API_KEY. See the docs for more info on how that works.

Then in your workflow, you can reference that environment variable as the value of the Authorization header, like so: Reference an environment variable in an HTTP request - Pipedream