JavaScript code examples in Zaps

This page provides 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.

For example, if you add these three input data fields:

  • body
  • receivedDate
  • subject

You can reference them in your code as inputData.body, inputData.receivedDate, and inputData.subject. Make sure you review the code examples and provide the right inputs, otherwise nothing will work as expected.

Javascript-input-field-example.png
miscEye icon Note

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

Tip

You can add public npm packages to your Code step for additional functionality beyond what's shown in these examples.

Introductory examples

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

Here's a synchronous example:

return {id: 1234, hello: 'world!', name: inputData.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: inputData.name};

Here's a synchronous example with an early empty return:

if (inputData.name === 'Larry') {
    return []; // skip this input
}
return {id: 1234, hello: 'world!', name: inputData.name};
miscEye icon Note

If Code by Zapier is the Zap's trigger and you return an empty array [], no actions will trigger downstream. This does not apply when Code by Zapier is used as an action, only when it is the trigger.

Here's an asynchronous example:

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

Introductory HTTP example

Code steps support async // await, which simplifies asynchronous JavaScript code. Learn more about await.

Here's an asynchronous example (no "Input Data" needed):

const res = await fetch('http://example.com/');
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:

return {id: 1234, hello: 'world!', name: inputData.name};
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 (inputData.name) {
  console.log('got name!', inputData.name);
}
return {id: 1234, hello: 'world!', name: inputData.name};
Tip

Test your action and check the data to find 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 emails.map(function(email) {
  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.

Store state

Use the StoreClient utility to store data between runs. For example, this counter tracks 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;
store
 .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});
  })
  .catch(callback);

Learn more about StoreClient.

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 the Zapier Developer Platform.

Was this article helpful?
6 out of 19 found this helpful