Function Node
Run serverless JavaScript functions with full access to workflow variables.
Overview
The Function node allows you to write and execute JavaScript functions that run in a serverless environment. Unlike the Code node (which runs inline), Function nodes are deployed as separate functions with their own execution context.
Configuration
| Field | Description | Required |
|---|---|---|
Function Name |
A descriptive name for the function | No |
Code |
JavaScript code to execute | Yes |
Output Variable |
Variable name to store the result | Yes |
Default Template
// This is the default function you need to use.
// Inside the function, you have access to all the variables created previously.
export const handler = async (variables) => {
console.log(variables)
return variables;
}
Writing Functions
Your function must export a handler function that:
- Receives a
variablesobject with all workflow variables - Returns the result (can be any JSON-serializable value)
- Can be async for asynchronous operations
Example: Process Data
export const handler = async (variables) => {
const { users, filter_status } = variables;
// Filter users by status
const filtered = users.filter(u => u.status === filter_status);
// Calculate statistics
const stats = {
total: users.length,
filtered: filtered.length,
percentage: (filtered.length / users.length * 100).toFixed(2)
};
return { filtered_users: filtered, stats };
}
Example: API Call
export const handler = async (variables) => {
const { api_key, query } = variables;
const response = await fetch('https://api.example.com/search', {
method: 'POST',
headers: {
'Authorization': `Bearer ${api_key}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ q: query })
});
return await response.json();
}
Accessing Variables
All workflow variables are available in the variables object:
export const handler = async (variables) => {
const userName = variables.user_name;
const apiResponse = variables.api_response;
const items = variables.scraped_data.items;
// Process and return
return { processed: true };
}
Differences from Code Node
| Feature | Function Node | Code Node |
|---|---|---|
| Execution | Serverless deployment | Inline execution |
| Template | export const handler |
Direct return |
| Use Case | Complex logic, external calls | Simple transformations |
| Timeout | Longer (configurable) | 30 seconds |
Best Practices
- Use descriptive function names
- Handle errors with try/catch
- Return structured data for easy access
- Keep functions focused on single tasks
- Use async/await for asynchronous operations