Functions

Learn about the two types of functions in journeys—custom functions and connection functions—and how they enhance automation and integrations.

Functions in Journeys

Functions are powerful automation steps that allow the assistant to execute operations, call APIs, or process data. They enhance journeys by integrating external systems or defining custom logic. There are two types of functions:

  1. Connection Functions – Predefined functions that can only be imported from a journey template.
  2. Custom Functions – User-defined functions where custom JavaScript logic can be written.

Types of Functions

1. Connection Functions (Predefined)

A Connection Function is a function that comes pre-configured within a journey template. These functions connect to external services such as Shopify, Google Sheets, or internal APIs.

Characteristics:

  • Predefined Input Arguments – Users cannot modify or add new input arguments; only default parameters are available.
  • Fixed Response Format – The response structure is determined by the integration.
  • Imported from Templates – These functions cannot be created manually; they are part of journey templates.

When to Use:

  • When integrating with external services via predefined API calls.
  • When retrieving structured data from an external system.

2. Custom Functions (User-Defined)

A Custom Function allows users to write their own logic in vanilla JavaScript, following ECMAScript standards. These functions provide flexibility in handling API calls, processing user input, and defining responses.

Characteristics:

  • Fully Customizable – Users can write their own JavaScript logic.
  • Define Your Own Input Arguments – Unlike connection functions, users can create custom parameters.
  • Custom Responses – The function can return structured JSON, formatted text, or any other required response.

When to Use:

  • When an integration does not have a predefined connection function.
  • When custom data processing or logic execution is required.
  • When API calls or business logic need to be defined manually.

Input Arguments

Each function type supports input arguments, but with different levels of flexibility:

Function TypeCan Define Arguments?Argument Behavior
Connection Function❌ NoFixed arguments, predefined in templates.
Custom Function✅ YesUsers can add, modify, and structure arguments freely.

Arguments allow functions to:

  • Process user input (e.g., order number, email).
  • Retrieve external data dynamically.
  • Pass values between different journey steps.

Response Handling

The response format also varies between function types:

Function TypeCan Define Response?Response Behavior
Connection Function❌ NoFixed response format from the external service.
Custom Function✅ YesUsers can define structured JSON or text responses.

Example: Custom Function Response

A custom function might return structured JSON like this:

async function main(args) {
    const { inputValue } = args;
    
    return JSON.stringify({
        status: "success",
        message: `Processed input: ${inputValue}`
    });
}

This response can then be used within the journey.

Calling Fetch in a Custom Function

Custom functions in Refly support the Fetch API, allowing you to retrieve data from external services. However, there is a small modification to how fetch is used.

Fetch API Modification

When calling fetch, instead of using:

let data = await response.json();

You must use:

let data = await response.json;

This means that .json is treated as a property rather than a function call.

Example: Fetching Data in a Custom Function

async function main(args) {
    const response = await fetch("https://api.example.com/products");
    const products = await response.json;  // Modified Fetch API usage
 
    const responseText = `${products}`;
    return JSON.stringify({
        status: "success",
        data: responseText
    });
}

Please note that certain JavaScript methods, such as setTimeout(), are not supported out-of-the-box due to their dependence on browser or Node.js runtime APIs and not being part of the ECMAScript (JavaScript) language specification itself. This JavaScript reference document describes all built-in objects supported by functions code.

Using Functions in Journeys

Selecting a Function in the Journey Builder

Functions can be added directly from the Journey Builder when designing a journey. To add a function:

  1. Open a journey in the Journey Builder.
  2. Click the Function action from the left-side panel.
  3. A dropdown will appear with available functions, including:
    • Connection Functions (predefined functions from integrations).
    • Custom Functions (user-created functions with custom logic).
  4. Select the function you want to use.

Testing and Debugging

  • Use the test input fields to check how the function behaves.
  • Verify that the response format is structured correctly before integrating it into a journey.

Summary

FeatureConnection FunctionCustom Function
User-defined Logic❌ No✅ Yes
Can Add Custom Arguments❌ No✅ Yes
Response Customization❌ No✅ Yes
Integration TypeImported from TemplatesManually Created

Functions provide a way to enhance automation and integrate services within journeys. Whether using predefined connection functions or custom JavaScript functions, they are essential for making journeys more dynamic and responsive.