Skip to main content

[resources] Node.js Libraries supported in JavaScript Code Steps

 

Contribution by Troy Tessalone

Premier Certified Zapier Expert at Automation Ace.

 

Overview

When writing JavaScript code in the Code by Zapier app, you have access to Node.js standard libraries and the built-in fetch package.
These libraries empower you to perform a variety of tasks, from working with files to making HTTP requests.
This guide lists the top useful libraries you can use, explains their purposes, and provides examples to help make you aware of the available options.

 

Help links for using the Code by Zapier Zap app: https://zapier.com/apps/code/integrations#help

  • You cannot require external libraries, or install or import libraries commonly referred to as "npm modules".
    Only the standard node.js library and the fetch package are available in the Code by Zapier app. fetch is already included in the namespace.

 

Top Node.js Libraries

Here's a curated list of the top most useful Node.js libraries for Zapier users, focusing on practical applications for automation tasks in Zap code steps:

  1. fetch (Built-In Package)

    • Purpose: Simplifies making HTTP requests directly in your code.
    • Example Use Case: Retrieve data from APIs or external services.
    const response = await fetch('https://api.example.com/data');
    const data = await response.json();
    output = { data };
  2. crypto

    • Purpose: Perform cryptographic operations like hashing or HMAC generation.
    • Example Use Case: Securely hash data for validation or encryption.
    const crypto = require('crypto');
    const hash = crypto.createHash('sha256').update('Zapier').digest('hex');
    output = { hash };
  3. url

    • Purpose: Parse, manipulate, and format URLs.
    • Example Use Case: Add or update query parameters in a URL.
    const { URL } = require('url');
    const myUrl = new URL('https://example.com');
    myUrl.searchParams.set('key', 'value');
    output = { updatedUrl: myUrl.toString() };
  4. console

    • Purpose: Debug and log output directly in the code execution environment.
    • Example Use Case: Print values for troubleshooting your Zap’s code step.
    console.log('Debugging Zapier code');
    output = { message: 'Logged to console' };
  5. path

    • Purpose: Handle and manipulate file paths efficiently.
    • Example Use Case: Normalize paths for consistent directory handling.
    const path = require('path');
    const resolvedPath = path.join('/folder', 'file.txt');
    output = { resolvedPath };
  6. fs (File System)

    • Purpose: Work with file-like structures or JSON parsing.
    • Example Use Case: Parse a JSON string to extract dynamic inputs.
    const data = JSON.parse(`{"key": "value"}`);
    output = { key: data.key };
  7. util

    • Purpose: Convert callback-based functions into Promises and access other utility features.
    • Example Use Case: Promisify a setTimeout function for sequential workflows.
    const util = require('util');
    const wait = util.promisify(setTimeout);
    await wait(1000); // Wait for 1 second
    output = { message: 'Waited 1 second' };
  8. zlib

    • Purpose: Compress and decompress data using utilities like Gzip.
    • Example Use Case: Compress data before sending it to an API.
    const zlib = require('zlib');
    const compressed = zlib.gzipSync('Zapier').toString('base64');
    output = { compressed };
  9. timers

    • Purpose: Introduce delays or execute code after a set time.
    • Example Use Case: Add a delay to sequential processing steps.
    await new Promise(resolve => setTimeout(resolve, 1000));
    output = { message: 'Waited 1 second' };
  10. buffer

    • Purpose: Work with binary data like encoding and decoding Base64.
    • Example Use Case: Convert strings to Base64 for API compatibility.
    const buffer = Buffer.from('Zapier');
    const base64 = buffer.toString('base64');
    output = { base64 };

 

* NOTE: Other Node.js libraries are supported, but less relevant for use in Zap Code steps with JavaScript.

Thank you so much for sharing this, ​@Troy Tessalone. It’s such a great resource for folks using Javascript Code actions in their Zaps. 🙌


Reply