Loading
Help

Use JavaScript code in Zaps

Last updated:

Code steps allow Zaps to run small snippets of Python or JavaScript. This tutorial is for JavaScript code steps, but you can also learn how to use Python code in your Zaps.

Code steps can be used as both triggers and actions.

Note

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

For tips and inspiration, check out JavaScript code examples.


1. Add a code trigger

  • In the Zap editor, click the Trigger step.
  • Search for and select Code by Zapier.
  • Click the Trigger Event dropdown menu and select Run Javascript.
  • Click Continue.

2. Set up your code step

  • In the Code field, enter your JavaScript code.
  • Click Continue.

3. Test your code trigger

  • Click Test trigger.

If your code is valid, the step will show it was successful and display the data. Once the trigger is set up, you can continue to add your action step.


4. Add a code action

The Code action step allows you to write code that will interact with data coming from the trigger, or a previous action step.

  • In the Zap editor, click the + icon to add a new step.
  • Search for and select Code by Zapier.
  • Click the Action Event dropdown menu and select Run Javascript.
  • Click Continue.

5. Set up your code action

  • In the Code field, enter your JavaScript code.
    • You can also define data fields to be provided to the code as strings with the Input Data fields. Provide a key and a value for each field.
  • Click Continue.

6. Test your code action

  • Click Test & Review.
  • If your code is valid, the step will show it was successful and display the data sent.

The JavaScript environment

The environment running JavaScript is vanilla Node v10.x.x. Your script is sandboxed and can only run for a limited amount of time and within a limited amount of memory—the exact limits depend on the plan you are using.

Output data from Code steps

Code steps return a single output variable, which is an object or array of objects that will be the result of this step. You can explicitly return early.

If the output is an array of objects, subsequent steps will run multiple times, once for each object in the array.

[
    {
        "product_id": 123,
        "name": "world",
        "price": 5.50
    },
    {
        "product_id": 234,
        "name": "planet",
        "price": 11.00
    }
]
Example

In this example, each object in the array is returned separately. Subsequent actions will run once for each object.
Example of first object returned

Example of second object returned

To return the whole array as line items instead, you can add line_items as the parent key for the output.

{
    "first_name": "Zap",
    "last_name": "Zaplar",
    "invoice_id": 123456,
    "total_price": 55.50,
    "line_items" : [
        {
            "product_id": 123,
            "name": "world",
            "price": 5.50
        },
        {
                "product_id": 234,
                "name": "planet",
                "price": 11.00
        }
    ]
};
Example

In this example, the array is returned as a set of line items. Any subsequent actions will only run once.
Example of line items returned

If you use Code by Zapier as the Zap's trigger and an empty array is returned, nothing will happen. The behavior will be similar to a polling trigger that did not get any results in the HTTP response. This functionality is exclusive to triggers.

Utilities in Code steps

There are a few utilities you have access to in Code steps:

  • (Optional) callback(err, output): a callback if you need to do asynchronous work—whatever you set to the output data variable is ignored since it's provided directly here. The Zap will inspect the code and make a best guess as to whether it's using a callback or not.
Note

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.

  • fetch: an easy to use HTTP client.
  • console.log: this utility allows you to debug your function. You'll need to test your Zap to see the values. The logs are returned in a runtime_meta added automatically to the output.
  • StoreClient : a built-in utility for storing and retrieving data between Zap runs.

Testing and debugging Code steps

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 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 6 MB. The total size of the code and the data processed by the Code step cannot exceed that. If you're hitting this error, try to limit the amount of data returned from your function. For instance, don't return an entire JSON structure, just the keys you need.
  • Free users can run scripts of up to 1 second and 128 MB of RAM. Paid users can run scripts of up to 10 seconds and 256 MB of RAM. Your Zap will hit an error if it exceeds these limits.
  • 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 by Zapier app. fetch is already included in the namespace.

Troubleshooting errors

Some common error messages you might encounter include:

  • Scripting payload too large: this means the total amount of data returned from your function is too large. See Limitations with code steps.
  • NoneType object does not support item assignment: this error can happen when you redefine some of the important variables in the function, such as callback. Lambda expects callback to be there to complete an async function, so it'll produce errors if it's redefined.
  • Process exited before completing request: this typically happens if your script completes without calling callback. The most common case is if your fetch().then() doesn't have a .catch() for errors. Adding a .catch(callback) will solve this, as the error will be passed as the first argument.
  • 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 warnings about unused variables for inputData, output, fetch or callback. Those are provided for your convenience, but you don't need to use them. If you see something else listed, you may need to debug your code.


Need More Help?

Contact Support

Tell us about your problem, and we’ll find you a solution or you can email support.
Get Help

Hire an Expert

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

Zapier Community

Connect with other Zapier users and industry professionals to get help crafting the perfect workflow.
Check out the community