Function Node

Overview

The Function node executes custom JavaScript code to transform data, perform calculations, or implement custom logic. This is ideal for data manipulation tasks that don’t require AI reasoning or external tool calls.

When to Use

Use a Function node when you need to:

  • Transform data formats (e.g., convert arrays, restructure objects)
  • Perform calculations or aggregations
  • Filter or map over collections
  • Format strings or dates
  • Validate data structure
  • Implement custom business logic
  • Parse or stringify JSON

Configuration

Code Editor

Code: Write JavaScript that will be executed

  • Full ES2021 JavaScript syntax support
  • Access to input data via payload variable
  • Return value becomes the node’s output
  • Synchronous execution only (no async/await)

Available Variables

  • payload: The complete workflow execution context
    • payload.results.<nodeId> - Outputs from previous nodes
    • payload.trigger - Data from the workflow trigger
    • payload.intelliaContext - Workflow metadata

Return Value

The function must return a value:

  • Return an object to create structured output
  • Return a primitive (string, number, boolean) for simple values
  • Return null or undefined for no output
  • Returned value is available at $.results.<nodeId>

Examples

Example: Format Date

Node ID: formatDate

Code:

const timestamp = payload.results.trigger.timestamp;
const date = new Date(timestamp);

return {
  formatted: date.toLocaleDateString('en-US', {
    year: 'numeric',
    month: 'long',
    day: 'numeric'
  }),
  iso: date.toISOString()
};

Output:

{
  "formatted": "December 22, 2025",
  "iso": "2025-12-22T02:00:00.000Z"
}

Access: $.results.formatDate.formatted

Example: Calculate Total

Node ID: calculateTotal

Code:

const items = payload.results.fetchItems.items;

const subtotal = items.reduce((sum, item) => {
  return sum + (item.price * item.quantity);
}, 0);

const tax = subtotal * 0.1;
const total = subtotal + tax;

return {
  subtotal: subtotal.toFixed(2),
  tax: tax.toFixed(2),
  total: total.toFixed(2),
  itemCount: items.length
};

Output:

{
  "subtotal": "145.50",
  "tax": "14.55",
  "total": "160.05",
  "itemCount": 3
}

Example: Filter Array

Node ID: filterActive

Code:

const users = payload.results.fetchUsers.data;

const activeUsers = users.filter(user => user.status === 'active');

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

Example: Transform Data Structure

Node ID: transformToMap

Code:

const products = payload.results.getProducts.items;

// Convert array to object keyed by product ID
const productMap = {};
products.forEach(product => {
  productMap[product.id] = {
    name: product.name,
    price: product.price,
    inStock: product.inventory > 0
  };
});

return {
  products: productMap,
  total: products.length
};

Output:

{
  "products": {
    "prod_123": {
      "name": "Widget",
      "price": 29.99,
      "inStock": true
    },
    "prod_456": {
      "name": "Gadget",
      "price": 49.99,
      "inStock": false
    }
  },
  "total": 2
}

Example: String Manipulation

Node ID: formatAddress

Code:

const address = payload.results.validateAddress.data;

const parts = [
  address.street,
  address.city,
  address.state,
  address.zip
].filter(Boolean);

return {
  oneLine: parts.join(', '),
  multiLine: parts.join('\n'),
  components: {
    street: address.street || '',
    city: address.city || '',
    state: address.state || '',
    zip: address.zip || ''
  }
};

Example: Conditional Logic

Node ID: determinePriority

Code:

const order = payload.results.trigger.order;

let priority = 'normal';
let rushFee = 0;

if (order.total > 1000) {
  priority = 'high';
} else if (order.requestedDate) {
  const daysUntilNeeded = Math.floor(
    (new Date(order.requestedDate) - new Date()) / (1000 * 60 * 60 * 24)
  );

  if (daysUntilNeeded < 3) {
    priority = 'rush';
    rushFee = 50;
  }
}

return {
  priority: priority,
  rushFee: rushFee,
  estimatedDelivery: priority === 'rush' ? '1-2 days' : '5-7 days'
};

Best Practices

  • Keep it Simple: Use Function nodes for straightforward transformations
  • Error Handling: Wrap risky operations in try-catch blocks
  • Return Early: Return immediately if conditions aren’t met
  • Null Safety: Check for null/undefined before accessing properties
  • Pure Functions: Avoid side effects; only transform input to output
  • Document Complex Logic: Use comments for non-obvious transformations
  • Test Thoroughly: Verify edge cases like empty arrays, null values

Limitations

  • No Async: Cannot use async/await or Promises
  • No External Libraries: Only built-in JavaScript features available
  • No Network Calls: Cannot make HTTP requests (use Tool Call nodes instead)
  • No File System: Cannot read/write files
  • Execution Timeout: Functions must complete within the execution timeout
  • Memory Constraints: Large data transformations may hit memory limits

Common Patterns

Safe Property Access

// Safely access nested properties
const email = payload.results.user?.data?.contact?.email || 'no-email@example.com';

return { email };

Array Operations

const items = payload.results.fetchItems.data || [];

return {
  // Map
  names: items.map(item => item.name),

  // Filter
  expensive: items.filter(item => item.price > 100),

  // Find
  firstMatch: items.find(item => item.category === 'electronics'),

  // Some/Every
  hasExpensive: items.some(item => item.price > 100),
  allInStock: items.every(item => item.stock > 0)
};

Object Operations

const data = payload.results.apiResponse.data;

return {
  // Merge objects
  merged: { ...data, timestamp: Date.now() },

  // Pick properties
  summary: {
    id: data.id,
    name: data.name,
    status: data.status
  },

  // Transform keys
  uppercase: Object.fromEntries(
    Object.entries(data).map(([k, v]) => [k.toUpperCase(), v])
  )
};