Use JavaScript code in Zaps

Last updated:

Code steps are a way to extend Zapier to run small snippets of Python or JavaScript as part of your Zaps. This tutorial is for JavaScript Code steps, but you can read about Python Code steps here.

Code steps can be used as both triggers and actions.

Heads Up

JavaScript is an advanced programming language. If you're not familiar with using JavaScript, it's recommended to ask a developer for help. Zapier does not offer email support for code steps. If you run into trouble, we recommend asking questions on StackOverflow and tagging them with "Zapier".

For tips and inspiration, check out JavaScript code examples.

The JavaScript environment

The environment is vanilla Node.js v8.10.0 which runs JavaScript. Your script is sandboxed and can only run for a limited amount of time and within a limited amount of memory—the exact limits are based on plan level. If you exceed those limits, your script will be stopped.

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.

Here we're defining body, receivedDate and subject fields on the inputData object based on our trigger data.


Mapping 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!

Heads Up

Invoking 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 output.

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 inputData, 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.
  • SyntaxErrors or other bugs: JavaScript is a fairly complex language and syntax errors can happen for a variety of reasons. We recommend using a linter like JSHint to automatically check your code and highlight specific issues. Note, if you're using a linter you can safely ignore any warnings about "unused variables" of inputData, output, fetch or 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!

Was this doc helpful?

Need More Help?

Zapier Support

Contact our world class support team and we’ll be happy to help you get up and running!
Contact Us

Hire an Expert

We have a directory of professionals across the globe who are ready to help.
Find a Zapier Expert