How to Get Started with Code (JavaScript) on Zapier#

Looking for Python instead of JavaScript? Visit our Code (Python) documentation.

Introduction#

With Zapier's Code action you can extend our platform to do just about anything using the most ubiquitous programming language on the web - JavaScript! Run code in response to any trigger that Zapier supports.

Some example use cases include:

  • Transform incorrect dates or convert between other data types.
  • Use custom regular expressions to extract extra data like emails or tracking numbers from large text blobs.
  • Make an extra API call to a different service with fetch without building a full dev app.
  • Augment data from a trigger with extra data from some other source (either generated or external API).
  • Anything else you can dream up!

Warning - this is advanced stuff! You probably need to be a programmer to use this - though you are welcome to play with it. Technical support for bugs in your code is not provided!

How does it work?#

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. If you exceed those limits - your script will be killed (you can upgrade to a paid Zapier to increase your limits).

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. This is a really simple step - the screenshot below shows a very basic example:

Scroll to the examples section to see an assortment of starter scripts.

In your code you will have access to a few variables:

Data Variables#

  • inputData The mapping of data you did right above your code snippet (not available in triggers). All values will be strings.
  • output 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 Code by Zapier 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#

  • callback(err, output) (optional) A callback if you need to do async 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!

    • Important: 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, for example, 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_meta added automatically to your output).

Testing/Debugging#

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 ran code's inputData, output and logs. The test step in the editor can be used for a tighter feedback loop.

Try asking for help by tagging questions as Zapier on Stackoverflow!

↑ Was this documentation useful? Yes No (Suggest Edits)
Get Help