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
fetchwithout 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!
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:
The mapping of data you did right above your code snippet (not available in triggers). All values will be strings.
An object or array of objects that will be the "return value" of this code. You can explicitly return early if you like.
outputto 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.
A callback if you need to do async work - whatever you set to the
outputdata variable is ignored since you provide it directly here. We inspect your code and make our best guess if you are using
- 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!
- Important: Invoking
An easy to use HTTP client - read the documentation here.
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
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!