Contact Us

If you still have questions or prefer to get help directly from an agent, please submit a request.
We’ll get back to you as soon as possible.

Please fill out the contact form below and we will reply as soon as possible.

  • Contact Us
  • Home
  • Built-in tools
  • Webhooks & Code
  • Code

JavaScript code examples in Zaps

Written by Owner

Updated at August 20th, 2025

Contact Us

If you still have questions or prefer to get help directly from an agent, please submit a request.
We’ll get back to you as soon as possible.

Please fill out the contact form below and we will reply as soon as possible.

  • Tables
    Work with tables Manage tables Create tables Troubleshoot Zapier Tables
  • Product updates
    Product updates: January 2023 Product updates: March 2023 Product updates: February 2023 Product updates: April 2023 Product updates: May 2023 Product updates: June 2023 Product updates: July 2023 Product updates: August 2023 Product updates: September 2023 Product updates: October 2023 Product updates: November 2023 Product updates: December 2023 Product updates: January 2024 Product updates: February 2024 Product updates: March 2024 Product updates: April 2024 Product updates: May 2024 Product updates: June 2024 Product updates: July 2024 Product updates: August 2024 Product updates: September 2024 Product updates: October 2024 Product updates: November 2024 Product updates: December 2024 Product updates: January 2025 Product updates: February 2025 Product updates: March 2025 Product updates: April 2025 Product updates: May 2025 Product updates: June 2025 Product updates: July 2025 Product updates: August 2025
  • Zaps
    Zap basics Zap history Troubleshoot Zaps Manage your Zaps Do more with Zaps
  • Your Zapier account
    Plans Billing Account profile Notifications Data privacy & security Get help User roles and permissions
  • Interfaces
    Troubleshoot Zapier Interfaces Create an interface Use interface components Manage interfaces
  • Canvas
    Create a canvas Collaborate on a canvas
  • Chatbots
    Add logic and knowledge to chatbots Troubleshoot chatbots Create chatbots Manage chatbots
  • Getting started
    Intro to Zapier Set up your Zapier account Use case templates
  • Agents
    Create agents Manage agents Troubleshoot agents
  • MCP
    Set up MCP
  • Built-in tools
    Filter & Paths Formatter Schedule & Delay Email, IMAP, SMTP, & Email Parser Webhooks & Code Storage, Digest, & RSS Sub-Zap & Looping Other built-in tools Custom Actions & API Requests Functions AI by Zapier & AI actions Copilot Human in the Loop
  • Lead Router
    Create routers
  • Apps
    Connect and manage your app connections AI apps on Zapier Apps on Zapier
+ More

Table of Contents

Introductory Examples Introductory HTTP Example Introductory Logging Example Simple Math: Divide by Two Simple Email Extraction Complex Multiple Email Extraction Weather JSON API Call Store State

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

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

if (inputData.name === 'Larry') {
    return []; // we don't work for Larry!
}
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 [], 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: 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 more complex asynchronous example (no "Input Data" needed) is:

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);
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};
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 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('https://api.openweathermap.org/data/2.5/weather?zip=' + 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: https://openweathermap.org/faq

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('http://api.openweathermap.org/data/2.5/weather?zip=' + inputData.zipCode + ',us&APPID={api_key_here}')
.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, 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;
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.

Was this article helpful?

Yes
No
Give feedback about this article

Related Articles

  • Generate a Code step using AI (Beta)
  • Code by Zapier rate limits
  • Python code examples in Zaps
  • Store data from code steps with StoreClient
  • Use JavaScript code in Zaps

Copyright 2025 – OBZ-Zapier.

Knowledge Base Software powered by Helpjuice

Expand