Skip to main content
Version: Latest (4.61.0)

Code

This component executes plain JavaScript code within a sandbox making it secure and safe.

note

Access to the console and the eval function are disabled.

See how it works:
Code Explainer

Calling done

Because JavaScript allows you to write code asynchronously, you need to call the done function when your code is done. When you are not using any asynchronous syntax, the done call should be at the end of your script.

// your code
done();
note

In cases where you are calling return in the global scope, you need to call done before the return statement.

note

The Code component does not utilize handlebars. To refer to incoming data, you can use value. followed by the data reference.

For example, if the incoming data is the following object:


{
"result": {
"_id": "123",
"kind": "RECORD"
}
}

To refer to the "kind" in the Code component, you can do something like:


const kind = value.result.kind;

Note that the Code component is the only place where you need to add value. before the incoming data reference.

Modules

The following modules are available within the code component:

  • Node.js Path module, use methods basename, dirname, extname, join, parse or relative

  • Node.js URL module, use URL

  • Luxon DateTime, use DateTime

For documentation on the modules, please use the manufacturer's official documentation.

Functions

The following functions are available in the Code component:

send(value:any): void

Sends data to the output of the component.

error(message:string, data:any): void

Throws an error in the component. Like other components, the component will stop and the error will appear in the log panel unless the error output is connected.

setVariable(key:string, value:any): void

Sets a global variable that can be used in any code component or HTTP component hook in the same flow.

getVariable(key:string): unknown

Gets a global variable.

deleteVariable(key:string): void

Deletes a global variable.

getFlowVariable(key:string, parseAs:string): unknown

Gets a flow variable. By default parseAs is set to string. Options: 'boolean', 'array', 'number', 'object', 'date', 'string'. Note that the variable syntax %variableName% is not supported in the code component and that flow variables are read-only.

hexToBase64(value:string): string

Converts a hexadecimal string to its Base64-encoded representation.

jwt(payload:any, privateKey:string, options?:object): string

Generates a signed JSON Web Token (JWT) containing the given payload, using the private key.
payload: Can be an any value. The payload is the data you want to include inside the token (e.g., user info or claims). privateKey: The private key used to sign the token.
options: Additional optional signing options such as

  • algorithm (e.g. 'RS256', 'HS256')
  • expiresIn (e.g. '1h', 3600)
  • issuer, audience, etc. See more information on all possible options

Example
const payload = {
"aud": "https://login.microsoftonline.com/12345/oauth2/token",
"exp": (Date.now() / 1000)+7200,
"iss": "456",
"jti": "456_12",
"nbf": (Date.now() / 1000)-3600,
"sub": "456"
};
const privateKey = "some private key";
const options = {
"algorithm" : "RS256",
"header" : {
"x5t" : Buffer.from("A5D5", "hex").toString("base64")
}
};
const token = jwt(payload, privateKey, options);

parseAs(value:string, parseAs:string): unknown

Parses a value as something else. Options: 'boolean', 'array', 'number', 'object', 'date', 'string'. By default parseAs is set to string.

log(type:string, message:any, data:any): void

Writes a log record. Type options: 'info', 'error', 'warn', 'debug'.

clone(value:object): object

Clones an object.

md5(value:string): string

Gets a md5 hash of the value.

sha1(value:string): string

Gets a sha1 hash of the value.

sha256(value: string): string"

Gets a sha256 hash of the value.

sha512(value:string): string

Gets a sha512 hash of the value.

escapeRegexSpecialChars(value:string): string

Escapes special characters in the given value string using a built-in regex pattern, ensuring safe use in regular expressions.

xpath(xml:string, expression:string): Node[] | string[] | number[] | boolean[] | null

Extracts data from an XML string using the provided XPath expression. The function returns the extracted data as an array of nodes.

parseHtml(html:string, options?: CheerioOptions | null, isDocument?: boolean): Cheerio

Parses an HTML string. The function returns a Cheerio object for the parsed HTML document, which mimics the behavior of jQuery. This method internally calls cheerio.load.

Example
const $ = parseHtml("<h1>Hello, world!</h1>");
log("info", $("h1").text()); // Output: Hello, world!
log("info", $.html()); // Output: <html><head></head><body><h1>Hello, world!</h1></body></html>

Check out the Cheerio docs for more information on how to select elements, traverse the DOM, and manipulate the DOM.

tip

To convert the entire parsed HTML document back into a string, use $.html(). This will return a full HTML document, including <html>, <body>, and <head> tags, even if these were not present in the original HTML document.

lookup(mappingName:string, lookupValues:array): Record<string, string>

Gets mapping values previously loaded into the Content Store using the Load Mapping component. See the Load Mapping component documentation for more information.

getMappingKeys(mappingName:string): Promise<string[]>

Gets the keys of a mapping.

lookupByKey(lookupKey:string): Record<string, string>

Gets mapping values by their key. As opposed to the lookup function, this function requires the full lookup key.

lookupByField(mappingName:string, query:Record<string, string>): Record<string, string>

Gets a mapping using only one key in case there are multiple. See the Load Mapping component documentation for more information.

getFlowId(): string

Gets the id of the flow the component is running in.

getFlowName(): string

Gets the name of the flow the component is running in.

getProjectId(): string

Gets the id of the project the flow is running in.

getEnvironment(): string

Gets the environment the flow is running in.

Configuration

Inputs | Outputs


Code
Input
Output
Error
00 0 (ilxzu8s)
Any message sent with the send() function.