Run custom JavaScript
Mix no-code and code in the same automation. The Write javascript step runs a script of your own in the middle of an automation: to reshape data, call an API, or reach a part of a page the visual steps cannot. It takes data from earlier steps through tokens and hands whatever it returns to the steps after it.
The agent uses this step too. When it builds an automation from a description and the no-code steps cannot do part of the job, it reaches for Write javascript, writes the script, wires in the data tokens and test-runs it. You can ask for the same in the Builder: the AI Assistant writes the code for a single step, or fixes a failing script.
For ready-to-use code, see the JavaScript snippets reference.
Add the step
Open the step finder and add Write javascript. The step has a plain code editor; write a standard script there or paste one in from your IDE. The script can use await at the top level, and functions keep a longer script readable.
const greeting = "Hello, World!";
if (greeting === "Hello, World!") {
return greeting;
}
For the best experience, run automations that use this step in the axiom.ai desktop app.
Two shapes of data
Everything that goes into or comes out of the step is one of two shapes. Knowing them saves most of the debugging.
- A 2D array, a table of rows and columns:
[["A", "B", "C"], ["D", "E", "F"]]. Scraped data, sheet data and any step with several rows. - A string, a single value:
"A".
Anything else your script returns (a boolean, a number, an object) is converted to its string form before later steps see it. A returned true arrives as "true", a returned 42 as "42". A script that returns nothing produces no output for later steps.
Get data in
Reference data from earlier steps with data tokens. Click Insert data and pick a token, or type it. axiom.ai swaps each token for the step's data at run time, as in any other step.
A token that holds several rows becomes a 2D array, so use it bare:
const data = [scrape-data];
console.log(data);
// Output: [["A", "B", "C"], ["D", "E", "F"]]
A token that holds a single value becomes a string, so wrap it in quotes to keep the script valid:
const data = '[scrape-data]';
console.log(data);
// Output: "A"
Once the token is in a variable, change the data however you need:
let data = [scrape-data];
data[0][1] = "X";
console.log(data);
// Original: [["A", "B", "C"], ["D", "E", "F"]]
// Output: [["A", "X", "C"], ["D", "E", "F"]]
Send data out
Return the value you want later steps to use. Return a 2D array when the data is tabular; later steps pick one or more of its columns with Insert data:
let data = [scrape-data];
data[0][1] = "X";
return data;
// Returned: [["A", "X", "C"], ["D", "E", "F"]]
Return a string when it is a single value; later steps reference it directly:
let data = [scrape-data];
return data[0][0];
// Returned: "A"
Run in the app
Toggle Run in app on to run the code inside the axiom.ai desktop app instead of the browser. It is off by default. On, the script gains:
- The Node.js filesystem API (
fs) for reading and writing local files. - The Puppeteer API for browser control beyond the no-code steps.
- The
chatGPT()helper for calling ChatGPT directly.
Off, console.log() writes to the browser console, which is the easiest way to debug a script. On, console.log() is disabled because the code no longer runs in the browser, and the script cannot reach the page. Run in app only applies to desktop runs; cloud runs always run the script in the page.
Examples
The code snippets section holds tried-and-tested scripts to paste into the step, by category:
- Send data to an API when Trigger webhook is not enough.
- Scrape an HTML table, attributes or meta tags; interact with dropdowns and the Shadow DOM; reshape data and build CSVs.
- Run in app snippets for local files and calling ChatGPT, and Puppeteer for browser control beyond the no-code steps.