JS Code
JS Code lets you solve with a few lines of JavaScript whatever no other component does exactly the way you need: calculate a value, reshape a list, combine data from several steps or call a web service. Whatever you send back with return is the component’s result.
It can also become a tool for an agent: the agent decides when to use it and with which data, and your code does the work.
When to use it
- Calculate totals, discounts or due dates from an order’s data.
- Change the shape of a list before sending it to another system.
- Call a supplier’s API that has no dedicated component.
- Give an agent a tool of your own, for example “calculate the shipping price from the weight and destination”.
Configuration
| Field | What it does |
|---|---|
| Input Data | The data your code receives. It can be a message, a record or a list. Inside the code it is in $data. |
| JavaScript Code | The code that runs. End it with return followed by the result. The field comes with commented examples of everything you can use. |
Inside the code you have:
| Name | What it is for |
|---|---|
$data | The data from Input Data. |
$args | The data the agent sends when the component is used as a tool. |
$components | The results of other components in the flow, by name, when the component works as a step of the flow. |
$env | The variables of the environment linked to the flow, for example $env.URL_API_PEDIDOS. Empty if the flow has no environment. |
$fetch(url, options) | Sends a request to a web service and returns its answer: whether it went well, the status code and the content. |
$tools["name"](data) | Uses a tool connected in Tools. |
$models["name"](text) | Sends a text to a model connected in Model and returns its answer. |
$wait(milliseconds) | Waits for the given time before continuing. |
$log(...) | Writes a message to the run log, useful to check what happened. |
$tools and $models always return a result with two parts: result with the answer and error if something went wrong.
Advanced options
| Field | What it does |
|---|---|
| Enable Tools Input | Adds the Tools connector so the code can use tools. Off by default. |
| Enable Models Input | Adds the Model connector so the code can query language models. Off by default. |
Tool tab
These fields are used when you connect the Tool output to an agent.
| Field | What it does |
|---|---|
| Tool Name | The name the agent sees for the tool. Default: js_tool. |
| Tool Description | Tells the agent what the tool does and when to use it. The clearer it is, the better the agent uses it. |
| Tool Input Schema | The data the agent must send when using the tool, with name and type. It reaches the code in $args. |
| Allow update agent state | Lets the code change the state of the agent that uses it. Off by default. |
Output
- Processed Data: what your code returns with
return. Connect it to the next step of the flow. - Tool: the component as a tool, to connect to an agent.
You use one output or the other: when you connect one, the other is hidden.
Example
A shop wants to calculate each order’s total including VAT before creating the invoice.
-
Connect the order to Input Data.
-
In JavaScript Code, type:
const base = $data.lines.reduce((sum, l) => sum + l.price * l.quantity, 0);return { order: $data.id, base, total: Math.round(base * 1.21 * 100) / 100 }; -
Connect Processed Data to the component that creates the invoice.
Good practices
- Start with
return $data;to see what the component receives, and write your code from there. - To try the connected tools, return
$tools: you will see which ones are available and what data they expect. Withreturn $env;you see the environment variables. - Do not type tokens or passwords in the code: store them as secret environment variables and read them with
$env. - There is a limit on web service, tool and model calls per run, and a maximum run time.
Related components
- Python Code: the same, writing Python.
- Environment Variable: reads an environment variable without writing code.
- API Request: calls a web service without writing code.