function
The Function block lets you run custom JavaScript or TypeScript code in your workflow. Use it to transform data, perform calculations, or implement custom logic that isn't available in other blocks.
Overview
The Function block enables you to:
Transform data: Convert formats, parse text, manipulate arrays and objects
Perform calculations: Math operations, statistics, financial calculations
Implement custom logic: Complex conditionals, loops, and algorithms
Process external data: Parse responses, format requests, handle authentication
How It Works
Configuration Options
Code Editor
Write your JavaScript/TypeScript code in a full-featured editor with:
Syntax highlighting and error checking
Line numbers and bracket matching
Support for modern JavaScript features
Native support for
fetch
Accessing Input Data
Use the input object to access data from previous blocks:
Common Examples
Data Transformation:
Calculations:
Data Validation:
Accessing Results
After a function executes, you can access its outputs:
<function.result>: The value returned from your function<function.stdout>: Any console.log() output from your code
Advanced Features
Async/Await Support
Use async functions for complex operations:
Error Handling
Implement robust error handling:
Performance Optimization
Optimize for large datasets:
Security and Limitations
Functions run in a secure environment with these restrictions:
Execution timeout: 30 seconds maximum to prevent infinite loops
Memory limits: Limited memory to prevent resource exhaustion
No network access: Cannot make HTTP requests (use API blocks instead)
Limited APIs: Only safe JavaScript APIs are available
Inputs and Outputs
Code: Your JavaScript/TypeScript code to execute
Timeout: Maximum execution time (defaults to 30 seconds)
Input Data: All connected block outputs available via variables
function.result: The value returned from your function
function.stdout: Console.log() output from your code
function.error: Error details if function failed
function.execution_time: Time taken to execute
Function Result: Primary output from your code
Debug Information: Logs and execution details
Access: Available in blocks after the function
Example Use Cases
Data Processing Pipeline
Scenario: Transform API response into structured data
API block fetches raw customer data
Function block processes and validates data
Function block calculates derived metrics
Response block returns formatted results
Business Logic Implementation
Scenario: Calculate loyalty scores and tiers
Agent retrieves customer purchase history
Function block calculates loyalty metrics
Function block determines customer tier
Condition block routes based on tier level
Data Validation and Sanitization
Scenario: Validate and clean user input
User input received from form submission
Function block validates email format and phone numbers
Function block sanitizes and normalizes data
API block saves validated data to database
Example: Loyalty Score Calculator
Best Practices
Keep functions focused: Write functions that do one thing well to improve maintainability and debugging
Handle errors gracefully: Use try/catch blocks to handle potential errors and provide meaningful error messages
Test edge cases: Ensure your code handles unusual inputs, null values, and boundary conditions correctly
Optimize for performance: Be mindful of computational complexity and memory usage for large datasets
Use console.log() for debugging: Leverage stdout output to debug and monitor function execution
Was this helpful?
