Code (Javascript) Examples

Important! Every example depends on specific inputData - which is provided under the "Input Data" field when setting up your - 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!

In this section we provide some examples - it is important to note that javascript is an advanced programming language - if you get lost it might make sense to ask a programmer friend or learn javascript yourself. There's also a great cheatsheet available if you need a quick refresher.

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:};

You'll notice that when it comes to code - you can solve the same problems in hundreds of unique ways!

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

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

If Code by Zapier is the Zap's trigger and you return an empty array [], we will not trigger any actions downstream it is as if you said "nevermind" 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 as trivial as:

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):

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);

Very important - 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:};

Test your action and look at the data to see the console.log result - great for debugging your code!

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');
const json = await res.json();
return json;

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

fetch('' + inputData.zipCode + ',us')
  .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, our StoreClient can do exactly that. For example - this is a counter that counts how many times it is ran:

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});

Read more about StoreClient here.

Was this article helpful?
0 out of 0 found this helpful