Code steps can be used as both triggers and actions.
In your Code step, you have access to the
inputData variable, where all values will be strings. Since the amount of data that might feed into your script might be large or highly dynamic you'll need to define an
inputData mapping via our GUI by providing a key and value.
inputData is not available in triggers.
Code steps return a single
output variable, which is an object or array of objects that will be the return value of this Code. You can explicitly return early if you like.
Setting the output to an array of objects will run the subsequent steps multiple times, once for each object in the array.
If your Code step is the Zap's trigger and an empty array is returned, nothing happens. You can think of it like a polling trigger that did not get any results in the HTTP response. This functionality is exclusive to triggers—returning an empty array in a Code by Zapier action does not have the same effect.
There are a few utilities you have access to:
callback(err, output) (optional): A callback if you need to do asynchronous work—whatever you set to the output data variable is ignored since you provide it directly here. We inspect your Code and make our best guess if you are using callback or not!
callback(err, output) tells Zapier that your task is done executing. If you have multiple asynchronous calls, each invoking
callback(err, output) with their desired responses, only the first one to execute will count. Subsequent invocations to
callback(err, output) will be picked up by the next execution of your Zap, but will not affect that task's execution, other than side effects like
console.log() calls. Be sure to carefully manage when you use
callback(err, output) to avoid extremely tricky and confusing bugs!
fetch: An easy to use HTTP client. Read the documentation here.
console.log: Helpful if you want to debug your function. You'll need to test your Zap to see the values. The logs are returned to you in a
runtime_metaadded automatically to your
StoreClient : A built-in utility for storing and retrieving data between Zap runs. Learn more here.
Running your Zap via the dashboard is the canonical way to confirm the behavior you expect. Your Zap History will have all relevant details around the Code step's
output and logs. The test step in the editor can be used for a tighter feedback loop.
Some common error messages you might find include: