Input data for Code steps
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.
Output data from Code steps
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.
Utilities in Code steps
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: Super 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.
Testing and debugging Code steps
Running your Zap via the dashboard is the canonical way to confirm the behavior you expect. Your Task 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.
Limitations with Code steps
- The environment in which your Code steps run (AWS Lambda) has an I/O limit of 6MB. The total size of the code and the data processed by the Code cannot exceed that. If you're hitting this error, try to limit the amount of data your return from your function. For instance, don't return an entire JSON structure, just the keys you need.
- Free users are limited to 1 second and 128mb of RAM. Paid users get well over double that at 10 seconds and 256mb of RAM. Your Zap will hit an error if you exceed these limits.
- Unfortunately, you cannot require external libraries or install or import libraries commonly referred to as "npm modules". Only the standard node.js library and the fetch package are available in the Code app.
fetch is already included in the namespace.
Troubleshooting error messages
Some common error messages you might find include:
- Scripting payload too large: This means the total amount of data returned from your function is too large. See Limitations with code steps for more information.
- NoneType object does not support item assignment: This shows up when you redefine some of the important variables in the function, namely callback. Lambda expects callback to be there to complete an async function, so if you've redefined it you'll get some strange errors.
- Process exited before completing request: This typically happens if your script completes without calling the callback. The most common case is if your
fetch().then() doesn't have a
.catch() for errors. A simple
.catch(callback) will suffice because the error will be passed as the first argument. Alternatively, use await with a Node 8 Code step to avoid having to worry about this.
callback. We provide those for your convenience but you don't need to use them. That said, if you see something else listed there it might be a sign that you have a bug!