JavaScript code examples in Zaps

This page provides some examples for using JavaScript code steps. Every example depends on specific inputData, which is provided under the "Input Data" field when setting up your Zap. This example screenshot shows three demonstration inputs you can use in your code like this: inputData.body, inputData.receivedDate, and inputData.subject. Be sure you read the code examples and provide the right inputs otherwise nothing will work as expected!

miscEye icon Note

JavaScript is an advanced programming language. If you're not familiar with using JavaScript, it's recommended to ask a developer for help.

Introductory Examples

Each of the four examples below expects a name in the "Input Data" field.

A synchronous example might be something as trivial as:

return {id: 1234, hello: 'world!', name:};

You can also bind the result to output—the code above has exactly the same behavior as the code below:

output = {id: 1234, hello: 'world!', name:};

A synchronous example with an early empty return might be something like:

if ( === 'Larry') {
    return []; // we don't work for Larry!
return {id: 1234, hello: 'world!', name:};
miscEye icon Note

If Code by Zapier is the Zap's trigger and you return an empty array [], we will not trigger any actions downstream. It's as if you said "never mind" in code. This does not apply when Code by Zapier is used as an action, only when it is the trigger.

An asynchronous example might be something like:

callback(null, {id: 1234, hello: 'world!', name:});

Introductory HTTP Example

As of June 2018, Node.js version 8 is available in Code steps. That release made async // await available for general use, greatly simplifying asynchronous javascript code. You can read more about awaithere.

A more complex asynchronous example (no "Input Data" needed) is:

const res = await fetch('');
const body = await res.text();
return {id: 1234, rawHTML: body};
// or
// output = {id: 1234, rawHTML: body}

For older Code steps, you can do the same with promises:

  .then(function(res) {
    return res.text();
  .then(function(body) {
   var output = {id: 1234, rawHTML: body};
    callback(null, output);
miscEye icon Note

Be sure to use callback in asynchronous code that uses .then()!

Introductory Logging Example

This example expects a name in the "Input Data" field:

if ( {
  console.log('got name!',;
return {id: 1234, hello: 'world!', name:};
ratingStar icon Tip

Test your action and look at the data to see the console.log result.

Simple Math: Divide by Two

This example expects a rawNumber in the "Input Data" field:

return {
  calculatedNumber: Number(inputData.rawNumber) / 2

Simple Email Extraction

This example expects a rawText in the "Input Data" field:

return {
  firstEmail: (inputData.rawText.match(/([\w._-]+@[\w._-]+\.[\w._-]+)/gi) || [])[0]

Complex Multiple Email Extraction

This example expects a rawText in the "Input Data" field:

var emails = inputData.rawText.match(/([\w._-]+@[\w._-]+\.[\w._-]+)/gi) || [];
return {
  return {email: email};

Because this returns an array like [] instead of a single object like {} this will activate follow up actions multiple times, one for each email found! If no emails are found, nothing happens.

Weather JSON API Call

This example expects a zipCode in the "Input Data" field:

const res = await fetch('' + inputData.zipCode + ',us&APPID={api_key_here}');
const json = await res.json();
return json;

Where {api_key_here} is to be replaced by your API Key you can get in OpenWeather:

miscEye icon Note

Zapier advises against inputting sensitive information such as api_key in Code or Webhook steps as plain text. For a more secure technique, build your own private app on our Zapier Developer Platform

For older Code steps, you can do the same with promises:

fetch('' + inputData.zipCode + ',us&APPID={api_key_here}')
.then(function(res) {
return res.json();
.then(function(json) {
callback(null, json);

Store State

It isn't uncommon to want to stash some data away for the next run, and our StoreClient can do exactly that. For example, this is a counter that keeps track of how many times it is run:

const store = StoreClient('your secret here');
const count = await store.get('some counter')
const newCount = (count || 0) + 1;
await store.set('some counter', count);
return {count: newCount}

For older Code steps, you can do the same with promises:

var store = StoreClient('your secret here');
var outCount;
 .get('some counter')
  .then(function(count) {
    count = (count || 0) + 1;
    outCount = count;
    return store.set('some counter', count);
  .then(function() {
    callback(null, {'the count': outCount});

Learn more about StoreClient.

Was this article helpful?
4 out of 11 found this helpful