## Building a Simple Flow

This step-by-step guide will walk you through how to build a simple Flow in Clay.

### Step 1 - Select a Trigger

Create a flow and give it a name. You will be presented with the following screen:

Types of Triggers:

• Webhook: Creates URL enpoint that triggers the workflow
• Schedule: Runs the workflow periodically
• Base: Runs the workflow when a specific change happens in a Clay Base

#### Note

Once you select a trigger you can’t change it. You have to delete the flow and start again.

### Step 2 - Setup Webhook Trigger

For the purposes of this walkthrough we’ll use the webhook trigger.

The webhook has a publicly accessible HTTPS endpoint. It is off by default and does not capture events or run the workflow.

You can turn it on by pressing the green button in the inspection panel or the switch at the top right:

The output area will show the keys of the last object that was sent to the workflow. This will help you see the structure of objects that are being sent to the webhook.

### Step 3 - Add & Write Clay Function

Currently the only action available is a Clay function. Select it:

You can choose an existing function or create a new function. In this case, we’ll create a new function:

This will open up the functions editor:

### Usage

Write the code for your function and test it by clicking run or cmd+enter using valid sample data.

Set up any private parameters you need process.env.parameter_name and any input parameters your function expects event.vars.variable_name.

When used in a workflow the function receives the following special parameter event.vars.previousStep which has all the data passed from the previous step in the workflow.

Once your function is behaving as per your requirements return the correct JSON data using the done() function. Make sure to publish your changes by clicking Publish Changes or cmd+s.

See the Appendix for more detailed information about the editor. For this example, set up a public parameter called emalId.

 if(event.vars.emailId == 'test') {
done({
template: "This is the test template"
})
}
else {
done({
template: null
})
}


Running it in dev mode with emailId = ‘test’ in the right hand panel will show the following result:

It is now published and available at the following url: https://exec.clay.run/felicek/get-template-from-emailid

### Step 4 - Bind Data for Each Step

You have to refresh the page so that any changes that you made to the function’s definition are reflected in the flow page.

The Clay Function will receive the object that was passed to the webhook in event.vars.previousStep.

In order for the functions that you write to be reusable, we let you map the data from any step in the workflow to the input parameters of a function. This is key for the reusability of functions so that you avoid any hard coding of object keys.

The syntax is double curly brackets with the step number and expected variable name.

For example: {{0.emalId}}

This will select the key emalId from the 0th step which is the webhook. In this case we selected the emailId key from the object that gets passed into the webhook url and passed it to our function’s emailId field.

You can see that the output is an object with a template field.

You can trigger the workflow with sample data to test it out, in my case this would be: https://api.base.run/v1/workflows/run/203335926fff553?emailId=test

### FAQs

Aborting the Flow:

If a function returns fail or done({}, 400) with a 400 http status code the rest of the workflow steps after that function will not run.

Authentication:

We are going to be adding authentication out of the box. We are adding it in the next release.

For now the first step of any flow should check for a particular key that you pass in the header or in the body and fail if it is incorrect.