Actions

Develop API calls and pushes to other systems in-browser

Once your data is stored in Middle, you can use automated workflows to construct logic and handle how data flows out of Middle, to other integrations or external sources. This process is handled primarily by Actions.

Actions are Middle's outputs: they can do anything from sending data, triggering events, or fetching records. In terms of HTTP syntax, many actions are comparable to “PUT” or “POST” requests, as they can be responsible for creating or updating; however, they may also be used to fetch or filter data as well.

An action’s main purpose is to take an input object and provide an output object as a response, if necessary. Unlike record types, information returned by an action is not stored in Middle. Instead, they are often used within chains in workflows to retrieve or pass on information to perform automations.

Actions can be triggered based on new or updated records, or if certain conditions are met. Let’s look at some examples:

  • Let’s say that the account using CoolNewApp also has an integration that handles leads (ex: Lead+). If we wanted to create a new user in CoolNewApp’s system, we could take any Lead records from Lead+ and trigger an action called “Create A New User” in CoolNewApp. We can define what data we want to retrieve from the Lead record in our Input Object, and that information could be used when performing the API “POST” call to create a new user. We can also return information as a result of this action. For example, if our API call resp object contains information such as the user ID, we could return that user ID in the Output object. In the context of workflows, this information could be used to trigger additional actions or conditionals.

  • Actions can also be used to retrieve information that can then be used in the output object to trigger the next part of a workflow. For example, you may want to reward a customer for purchasing 5 or more smoothies during a 30 day period. You could create an action that makes an API request to grab all of a customer’s orders within a 30 day period, and either filter or only output the sale(s) if it contained a smoothie line item. The count of the smoothies purchased could be returned in the output objects to determine if any rewards should be triggered.

Actions are especially flexible and can even be used to perform functions internal to the workflow, such as data manipulation.

The three components of an action are the input object, the output object, and the Python script used to perform the action and yield the response.

To create an action, click + Create next to Actions and provide a name and description.

Input object

In most cases, you will need to determine an input object for any information or parameters that you want to feed into your action’s script. Typically, this might represent aspects of the payload or body you need to pass in when making your API request. When a user sets up a workflow for an Account using this action, these are the parameters that they will need to fulfill in order for the action to run.

Constructing an input object is very similar to the way that you construct attributes for a Record Type. To add an attribute, click + Add attribute and use the insert arrows on the side to add additional attributes.

An attribute’s title and description can provide any important information that the user configuring a workflow may need. For example, an action in theory does not have to have all parameters filled out in order to run. Therefore, if there is anything that is explicitly needed for your API request or code, you can indicate it here.

Output object

An output object represents the information that can be returned as a result of your action. It is optional to construct.

It is important to note that, unlike record types, information returned in the output object is not stored in Middle. Instead, this information exists temporarily and can be used in subsequent steps of a workflow, as needed.

Depending on what is contained in the Output Object, a workflow can determine whether further actions or decision nodes should be triggered to complete other automations.

For example, when Creating A New User, it’s possible that the API may return a response containing a user ID or additional user information. That User ID could be passed onto an additional action to create a referral.

You can define these attributes in a similar fashion to the Input Object.

Action Script

Similar to Poll Syncs, the script is the Python code that you write to make the API call needed to complete your action.

Information found for your input object is stored within the Action Payload dictionary and can be accessible when scripting.

Here’s a simple code to Create A New User:

"""
payload = {
    "email": action_payload.get("email"),
    "first_name": action_payload.get("first_name"),
    "last_name": action_payload.get("last_name"),
    "birthday": action_payload.get("birthday"),
    "external_ids": action_payload.get("external_ids", [])
}

resp = requests.post(
    url='https://api.coolnewapp.com/v1/users',
    headers={'Authorization': f'Bearer {auth["access_token"]}'},
    json=payload)
    
r_json = resp.json()

return {
    "succes": True if r_jason else False,
    "user_id": r_json.get("user_ird") if r_jason else None
}

Remember to hit Save when you finish writing your script.

Last updated