JavaScript Module
Last updated
Last updated
The "JavaScript" module, the platform's standard module for use in any flow, allows you to run JavaScript code safely within a controlled environment. This module is ideal for performing data transformations, custom calculations, and other JavaScript-specific programmatic operations.
Check below for what is available for use, what is restricted, and practical examples of how to use this feature.
Standard JavaScript code execution: You can use all the standard JavaScript functionalities, including variable declarations, loops, conditionals, object manipulation and arrays.
Asynchronous functions and Promises: Support for async/await and Promises, allowing asynchronous operations such as simulated data requests or operations that rely on timers.
Timers:
setTimeout: Available to schedule asynchronous operations after a specified delay.
clearTimeout: Available to cancel timers created with setTimeout.
String and number manipulation: Use of global functions such as parseInt, parseFloat, Number, String, and others to manipulate numbers and strings.
Native JavaScript functions: All native JavaScript functions, such as Math, Date, JSON, Array, Object, and RegExp, are available.
Memory: You can use up to 60MB of memory to execute the code.
Module Import: It is not possible to import modules using require or import. This includes both native Node.js modules and third-party libraries, ensuring a secure and isolated environment.
System Access: There is no access to the file system, network, or other system operations that could compromise the security of the platform.
Process Handling: Functions for manipulating processes, such as process.exit() and process.kill(), are not available.
Restricted Keywords: Certain keywords and operations, such as eval and Function, are restricted to prevent the execution of code that could escape the secure environment.
Access to Global Variables: The code executed is isolated from the global context, with no access to global variables external to the sandbox.
The "JavaScript" module appears in the Tools option when editing a flow. As soon as you click to add it, the modal opens for configuration:
Fill in the following fields:
Key: variable identifier
Value: type or drag a variable
Type: choose the most appropriate data type. Options are: text, number, boolean, object, and array.
After completing the configurations, click "Next".
On this screen, you can execute and view the data transformation information and manipulate it with JavaScript.
All parameters are passed within the "data" field. In practice, this means that all parameters are within a "data" object, which can be accessed by the JavaScript code.
Data Type: "data" is a JSON object. This means it can contain key-value pairs, where the keys are strings and the values can be data types such as strings, numbers, arrays, objects, booleans, or null.
Custom Fields: The content of data is completely customizable and can contain any information the user’s code needs to function. There are no specific restrictions on the structure of the data, other than being JSON serializable.
Numbers and Strings: Numeric or text values that can be used for calculations or manipulations.
Arrays and Objects: More complex data structures that can contain lists of items or other nested objects.
Configuration Data: Specific configuration information that determines how the code should behave.
Imagine a scenario where we have two parameters. In this example, we will call them "number1" and "number2". We input the data and use the following code in JavaScript:
Next, we click "Run." The result will be the sum of the parameters, as shown in the image below:
In this example, we will work with an asynchronous scenario using Promises in JavaScript.
Imagine we want to process the data after a certain time. We will use two parameters, which are inside "data," and the function will return a Promise.
The code below simulates an asynchronous operation using setTimeout to wait for one second before resolving the Promise with a message and the provided data, after clicking "Run":
The result can be seen in the image below:
We will create a JWT token using the JavaScript module in this example.
To do this, we will use the signJwtToken function provided by the platform within the module context. This function accepts three parameters:
payload: This field is required and must be an object. Its value corresponds to the token body.
secret: This field is required and must be a string. Its value is the secret key used to generate and validate the token.
The signJwtToken function takes a payload (data we want to include in the token) and a secret key to sign the token.
Usage example
In the input data, you can configure a parameter or variable if needed. For instance:
Example of input data:
We insert the code below into the JavaScript Code area:
After execution, the result can be seen in the image below:
In case of an error, the response will be different, as shown below:
In this example, we will decode a JWT token to verify and access the data stored within it.
We will use a function provided by the module called decodeJwtToken. This function requires two parameters:
token: This field is required and must be a string. It represents the token to be decoded and verified.
secret: This field is required and must be a string. It represents the secret key used to sign the token.
Usage example
For the input, we used the following data:
We inserted the code below into the JavaScript Code area:
After execution, the result can be seen in the image below:
In case of an error, the response will be different, as shown below:
Input Validation: Always validate input data to ensure correct code execution and avoid unexpected errors.
Performance: Avoid infinite loops or deep recursions that can cause excessive memory usage.
.
To finalize the module configuration, click "Save".
options: This field is optional and must be an object. For more details about its configuration, refer to the JavaScript library documentation: .
Considering that the parameters payload and secret are required, additional options can be included. In this example, we used expiresIn. To view other available options, .