Loading
Help

Code (Javascript) Examples

Last updated:

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

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 (inputData.name === 'Larry') {
  return []; // we don't work for Larry!
}
return {id: 1234, hello: 'world!', name: inputData.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: inputData.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.

A more complex 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:

fetch('http://example.com/')
  .then(function(res) {
    return res.text();
  })
  .then(function(body) {
    var output = {id: 1234, rawHTML: body};
    callback(null, output);
  })
  .catch(callback);

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

Weather JSON API Call

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

const res = await fetch('http://api.openweathermap.org/data/2.5/weather?zip=' + inputData.zipCode + ',us');
const json = await res.json();
return json;

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

fetch('http://api.openweathermap.org/data/2.5/weather?zip=' + inputData.zipCode + ',us')
  .then(function(res) {
    return res.json();
  })
  .then(function(json) {
    callback(null, json);
  })
  .catch(callback);

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

Read more about StoreClient here.


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