Beyond the Basics - JavaScript and Axiom.ai

Axiom.ai automations are already very powerful on their own, add JavaScript to them? You can extend it's functionalities tenfold, you're only limited by your development skills and imagination. The Write Javascript (opens new window) step that we provide allows you to add your scripts right into your automation and have them executed on the page that your bot is working on. This can be useful for adding custom logic, data processing and accessing Puppeteer functions within your runs.
Did you know that we also provide some snippets? Our growing library of JavaScript (opens new window) and Puppeteer (opens new window) snippets can help solve some standard use cases within your automations. Our community (opens new window) can also be a great place to get some help, if you are unsure.
# Using JavaScript within an automation
To get started, add a Write Javascript (opens new window) step to your automation. This step allows you to write a JavaScript script in your automation. The script that you write will only be executed when the automation run reaches the step that you have inserted the "Write Javascript" step at.
💡 Your script will be executed in the context of a function, this is important to note to understand the syntax and features that can be used within this step.
While Axiom.ai does provide a library of data manipulation (opens new window) steps that can be used to manipulate any data that you pass into your automation, or gather through your automations actions, there are times when you may want a more than the built in features provide. An example of this is adding a constant value to any empty cells of scrape data - when scrape data is written to a Google Sheet, these blanks will cause the data to shift as a result of their API. To get around this, a simple script can be used:
var data = [scrape - data]
const placeholder = '-'
for (var i = 0; i < data.length; i++) {
for (var j = 0; j < data[i].length; j++) {
if (data[i][j] == '') {
data[i][j] = placeholder
}
}
}
return data
Source: JavaScript Snippets (opens new window).
Functions can be declared within your scripts, allowing you to structure your code neatly, for example:
const add = (a, b) => {
return a + b
}
const sum = add(1, 2) // 3
# JavaScript limitations
Your scripts are encapsulated and will not interact with each other. For example, if you have a script that declares the variable example, you will not be able to access this within another script.
As the script that you insert into this step is executed as a function, you will not be able to import any additional libraries or modules into your script - doing so will actually cause issues with your automation and may prevent it from running.
If you are using JavaScript to access an API, it's important to remember that you may run into cross-origin resources sharing (CORS) issues. You may attempt running in-app option, but we are unable to guarantee that this will resolve the problem.
# Using Puppeteer within an automation
In the same way that you can use JavaScript within your automations, you can also access certain Puppeteer functions. This allows you to make completely custom automations that can interact with pages and perform actions using custom code - essentially using Axiom.ai as a wrapper for your custom scripts. Generally, you are able to use any function that is available in the Page (opens new window) class. This means that you can access functions such as:
- click()
- reload()
And many other functions that can be found in the Puppeteer documentation. This can be helpful if you need to access certain functions that are not natively available through Axiom.ai, such as automating the scrolling on a page, or performing actions on a subset of elements on a page, for example, only buttons that contain a certain string of text rather than all buttons on the page.
# Puppeteer limitations
Puppeteer scripts can only be run locally and can not be run in the cloud, this means that you will need to have the desktop application installed and active to run your scripts - you'll need to select "Run in app" within the step. This does limit the functionality of this integration, but JavaScript can usually be a stand-in in the event that you need to run this in the cloud.
# Running in-app
The "Run in app" option within the "Write Javascript" step can be used to run code in the context of the desktop application, rather than the context of the browser. If you are running your automation locally, you will need to have the desktop application installed and active. There are some benefits to using it with your other scripts, such as:
Ensuring your code does not interfere with the site's code - for example, triggering event listeners, conflicting function or variable names. Accessing the Puppeteer library (required to use the "Run in app" option).
The downside of running code in the desktop application is the inability to access elements, functions or variables from the page that your automation is running on. You will not have the ability to see the console output from code within the "Write Javascript" step while using this option.
# Using data tokens
Data tokens contain information that has been output from steps - for example, the [scrape-data] data token that is output from the "Get data from bot's current page" step contains all of the data that the step has retrieved from the page. These can be used within your custom scripts by clicking the "Insert data" option and this will insert the data token into your script. For example:
let data = [scrape - data]
For data tokens that just contain a single value, you can insert it like so, replacing the data token with the one from your automation:
let data = '[data-token]'
From this point you have an array of arrays that you can loop through, manipulate or use for logic conditions throughout your script. For example:
let data = [scrape - data]
for (var i = 0; i < data.length; i++) {
for (var j = 0; j < data[i].length; j++) {
console.log(data[i][j])
}
}
This can be helpful when creating custom integrations with services that make use of the data from your automations, such as posting to Bluesky (opens new window), sending data to Supabase (opens new window) or Tadabase (opens new window), or triggering other automations such as from Zapier (opens new window).
# Wrapping Up
By incorporating JavaScript and Puppeteer into your Axiom.ai automations, you unlock a realm of possibilities far beyond standard automation capabilities. Whether you're refining scraped data with custom logic, interacting with web pages using Puppeteer's powerful functions, or building intricate integrations with external services, these tools empower you to tailor your automations to your precise needs.
Remember, while JavaScript offers flexibility for data manipulation and custom logic, Puppeteer enables direct interaction with web pages, albeit with the limitation of local execution. Leverage our growing library of snippets and community support to navigate common use cases and overcome any challenges you encounter.
Keep in mind the limitations of encapsulated scripts and CORS issues when working with APIs, and always explore the "Run in app" option when necessary. By effectively utilizing data tokens, you can seamlessly integrate data from various steps within your automation into your custom scripts, creating dynamic and responsive workflows.
With Axiom.ai's JavaScript and Puppeteer integration, you're not just automating tasks; you're crafting bespoke solutions that enhance your productivity and streamline your workflows. We encourage you to experiment, explore, and push the boundaries of what's possible with automation.