Javascript

Overview

The JavaScript connector enables your Leena AI Agents and workflow designers to run custom JavaScript code as a step in a workflow, so teams can add their own logic without engineering help.

Typical uses include reshaping data between systems, performing calculations, formatting values, parsing JSON or XML, and applying conditional business rules that the standard workflow nodes don't cover.

API Details

Unlike most connectors, the JavaScript connector does not call a third-party API. Your code runs in Leena AI's own runtime. Each execution runs in an isolated sandbox and is subject to a 60-second execution time limit.

Setup

The JavaScript connector needs no credentials, API keys or OAuth configuration, but a connection must still be created before the action can be used in a workflow.

Prerequisites

  • Access to your Leena AI workspace with connector management permissions
  • Basic familiarity with JavaScript for the workflow authors

Get credentials

No credentials are required, since no external system is involved.

Add connection

  1. Log in to your Leena AI workspace
  2. Navigate to Settings > Integrations
  3. Search for "JavaScript" and select it from the list to add a new connector
  4. Save the connector. There are no credential or configuration fields.
  5. (Optional) Click Test connection. The JavaScript connector has no external dependency, so the test always succeeds once the connection is saved.

Actions

The following action is supported for the JavaScript connector:

Execute JavaScript Code

Runs a custom JavaScript function as a workflow step and returns its result to later steps. Here are some common use cases:

  • Data Transformation: Reshape the output of one connector into the input another expects
  • Custom Calculations: Compute values such as tenure, prorated leave or totals
  • Conditional Logic: Apply business rules that would be cumbersome to build with workflow nodes
  • Formatting & Parsing: Convert between JSON and XML, normalize dates, build strings

Input Parameters

Here are the input parameters required to set up this action:

Mandatory

NameDescription
CodeThe JavaScript to run (code). It must define a top-level function named execute; the value it returns (or resolves to, if it is async) becomes the output of the action.

Optional

NameDescription
ArgumentsAn ordered list of values passed to execute as positional arguments, in the order they are added (args). Click Add param to add one. Each value can be static text or a workflow variable.

How the code runs

  • Entry point: Define function execute(...) or async function execute(...) at the top level. The first argument you add is passed as the first parameter, the second as the second, and so on.
  • Argument types: Arguments are passed as entered. Values typed in the form arrive as strings, so convert them yourself where needed (for example Number(amount) or JSON.parse(payload)).
  • Asynchronous code: async/await and Promises are supported; the action waits for the returned Promise to resolve.
  • Built-in libraries: The following are available as globals, with no require or import:
GlobalLibrary
_Lodash
axiosAxios (HTTP client)
luxonLuxon (dates and times; e.g. luxon.DateTime)
cryptoNode.js crypto
BufferNode.js Buffer
xml2jsxml2js (XML ⇄ JS objects)
  • Sandbox restrictions: Code runs in an isolated context. require, import, process, the file system and other Node.js modules are not available. Standard JavaScript built-ins (JSON, Math, Date, Promise, Array, etc.) are available.
  • Time limit: Each execution is stopped after 60 seconds.

Here is a sample JSON input:

{
  "code": "async function execute(firstName, lastName, joiningDate) {\n  const joined = luxon.DateTime.fromISO(joiningDate);\n  const tenureYears = Math.floor(luxon.DateTime.now().diff(joined, 'years').years);\n  return {\n    fullName: _.startCase(`${firstName} ${lastName}`),\n    tenureYears\n  };\n}",
  "args": ["john", "doe", "2021-04-15"]
}

The code above, formatted:

async function execute(firstName, lastName, joiningDate) {
  const joined = luxon.DateTime.fromISO(joiningDate);
  const tenureYears = Math.floor(luxon.DateTime.now().diff(joined, 'years').years);
  return {
    fullName: _.startCase(`${firstName} ${lastName}`),
    tenureYears
  };
}

Response

  • Success: The return value of execute is returned as the action's output (status 200) and can be used in later workflow steps. For the sample above:
{
  "fullName": "John Doe",
  "tenureYears": 5
}
  • Error: If the code throws an error, or execute is not defined, the action returns status 500 with the error message:
{
  "error": {
    "message": "Cannot read properties of undefined (reading 'id')"
  }
}
  • Timeout: If the code runs longer than 60 seconds, the action returns status 408:
{
  "error": {
    "message": "Execution timed out"
  }
}

Did this page help you?