Variables
Save, manage, overwrite and calculate data by assigning variables.
Overview
The Variable block acts as a container capturing and retaining data passed into your APIs, enabling you to access and reference those values at a later use within your routes.
Variables you set with this block are only accessible in the specific API/Workflow you’re setting them in. For globabl variables which are accessible throughout your whole project visit Environment Variable.
Interface
Within the Variable action you can define 1 or more variables. Variables are computed from top to bottom so the 2nd variable can also reference the newly set variable value from the variable above.
Type
From left to right you can first define the type of the variable by clicking the icon in the input field under the label Name
. Defining this type acts as validation and when the variable is computed, the action throws an error if the computed value is not the specified type.
Name
By default the name of the variable will be taken as the the second segment behind the $var.
prefix so that a variable with the name my-var
will be then accessible under {{$var['my-var']}}
throughout the flow.
The name of the variable must then consist only of the letters and numbers with the following special characters also being allowed: -
, _
and +
. All other characters and symbols will be trimmed when building the key under which the variable will be accssible.
It is also possible to overwrite already existing data by starting the name with the $
symbol to specify the full variable notation. As an example, setting $action['action-name']['some-value']
as the name will lookup the existing value under the given key and overwrites it with the new value.
See the examples down below for a few use cases and illustrations of this mechanic.
The data under $user.*
and $env.*
cannot be overwritten using this mechanic
Operator
Clicking the icon within the Value
input field lets you define the operator for the variable assignment. Each variable type has its own set of available operators, with the Operator Set
being the default and being available for all types. Additional operators are:
Type Number
- Plus: Current value of variable plus the specified new value
- Minus: Current value of variable minus the specified new value
Type Text
- Append: Current value of variable plus the specified new value added at the end
- Prepend: Current value of variable minus the specified new value added at the front
Type Array
- Append: Current value of variable plus the specified new value added at the end of the list
- Prepend: Current value of variable minus the specified new value added at the front of the list
Value
The value of variable defines the expression that will be computed and assigned to the variable as defined by the operator.
Checkout our functions documentation to learn about all our powerful build-in functions and the language syntax that drives them.
Example
Create Variable
Situation
This example API is called whenever a new order is submitted. The data received by this endpoint contains customer and shipping information. We are interested in the Zip Code
as we want to trigger automations based on that value for each other.
The API receives the following JSON when an order is submitted:
Creating the variable
We can access the customer’s Zip Code at {{$body.order.shipment.shippingAddress.location[0].zipcode}}
and use the Variable action block to store it:
Deploy
Now, our variable dropdown will show our new Zip Code variable under Flow variables
:
This means, for the rest of the workflow we can use $var['zip-code']
to access the value of the Zip Code.
Overwrite Action Output
There are several use cases for overwriting existing data from the flow.
Here $body.someText
is initially some text but we convert it lowercase, split it by the whitespaces and then overwrite it so that $body.someText
is now an list of words.
Here $body.someArray
is already an array and we prepend its first element to the beginning of the array.
Here we create a new array out of $action['my-action'].Result
and $action['my-action'].Text
and set it to the Result
key of that action.
We can also utilize advanced indexing of arrays through other variables within the name of the variable.
Further Resources
Utilizing variables has many advantages. They are simply shorter in terms of character length which makes it easier to work with them. Furthermore, in case the originial JSON syntax of the customer order changes, we only need to adjust the syntax of the variable block once, instead of changes all action blocks where the Zip Code value is used.
If you want to learn how to access variables from actions, API requests, events etc. visit Variables.