Code

Write custom JavaScript for complex data transformations

Overview

The Code utility allows you to write custom JavaScript code to transform, filter, or process data in ways that other utilities cannot. This is the most flexible utility in your toolbox.

Your code has access to your input variables and common JavaScript features. The value you return becomes the output of the node.

Input Variables

Important: You must add Input Variables to access data from previous nodes!

Step 1: Add Input Variable

In the Code node configuration, find the "Input Variables" section and click "Add Variable".

Step 2: Name Your Variable

Give it a meaningful name (e.g., message,userData).

Step 3: Map the Value

Use template syntax to reference data from previous nodes:

{{#2.data.message}}

Step 4: Access in Code

Access your variable using input.variableName:

const text = input.message;
// or directly:
const text = message;

Available Variables

VariableDescription
input.varNameYour mapped input variables
iteratorIterator context (if inside an iterator)
itemCurrent iteration item
indexCurrent iteration index

Return Format

Your code must return a value. This becomes the output of the node:

Return an object:

return { processed: true, count: 5 };

Return an array (for multiple bundles):

return [
  { id: 1, name: "First" },
  { id: 2, name: "Second" }
];

Examples

Example 1: Text Transformation

Input Variable: text = {{#1.data.content}}

const text = input.text;

// Clean and transform
const cleaned = text
  .trim()
  .toLowerCase()
  .replace(/\s+/g, ' ');

return {
  original: text,
  cleaned: cleaned,
  wordCount: cleaned.split(' ').length
};

Example 2: Chunk Text for TTS

Input Variable: message = {{#2.data.message}}

const text = input.message;
const MAX_WORDS = 1000;
const chunks = [];
let currentChunk = "";
let wordCount = 0;

const sentences = text.split(/(?<=[.!?])\s+/);

for (const sentence of sentences) {
  const sentenceWords = sentence.split(/\s+/).length;

  if (wordCount + sentenceWords > MAX_WORDS && currentChunk) {
    chunks.push({ index: chunks.length, chunk: currentChunk.trim() });
    currentChunk = sentence;
    wordCount = sentenceWords;
  } else {
    currentChunk += " " + sentence;
    wordCount += sentenceWords;
  }
}

if (currentChunk.trim()) {
  chunks.push({ index: chunks.length, chunk: currentChunk.trim() });
}

return chunks;

Example 3: Filter and Transform Array

Input Variable: users = {{#1.data.users}}

const users = input.users;

// Filter active users and transform
const activeUsers = users
  .filter(user => user.status === 'active')
  .map(user => ({
    id: user.id,
    fullName: `${user.firstName} ${user.lastName}`,
    email: user.email.toLowerCase()
  }));

return {
  users: activeUsers,
  count: activeUsers.length
};

Common Mistakes

Wrong: Trying to access data without Input Variables

const data = $("node1").data;  // ❌ $ is not defined

Correct: Add Input Variable first, then access it

// Add Input Variable: myData = {{#1.data}}
const data = input.myData;  // ✓ Works!

Tips

Tip: For debugging, return intermediate values to see them in execution details. Example: return {debug: myVar, result: finalValue}

Tip: Returning an array creates multiple bundles, perfect for splitting data for parallel processing.

Note: External network requests, file system access, and Node.js APIs are not available. Use the HTTP Request utility for API calls.

Code Utility - Flows Guide - Serenities