Skip to main content
Use HubSpot’s chatflows tool to customize how visitors to your website can ask custom questions and interact directly with support or sales agents from your organization. As you build out a chatflow, you can create a chatbot to automatically handle specific visitor questions, and add actions based on certain rules, including the ability to run custom code snippets.

Add a code snippet action to a bot

When creating or editing a bot, you can add a code snippet by clicking the + icon to add an action as you normally would. From the action selection panel, click Run a code snippet.
Next, give your action a name and description, then select the Node.js runtime version that the code snippet will use.
Please note: it’s highly recommended you use the v20 runtime of Node.js for security purposes, as v18 entered its official end-of-life (EOL) on April 30, 2025.
Configure a code snippet action in a bot
The code will be triggered when the saved action is reached in a conversation. There are three main things to keep in mind when working with code snippets:
  • The exports.main() function is called when the code snippet action is executed.
  • The event argument is an object containing details for the visitor and chat session.
  • The callback() function is used to pass data back to the bot and user. It should be called in the exports.main function.
The event object contains the following properties: The code block provides an example event object for a contact who was chatting with a bot.
The callback() function is used to send data back to the bot. The argument should be an object containing the following data:

Limitations

Code snippets in bots are subject to the following limits:
  • For snippets created prior to August 1st, 2025, code snippets must finish running within 20 seconds. Any snippets created after August 1st, 2025 must finish running within 45 seconds.
  • Code snippets can only use up to 128 MB of memory.
Exceeding either of these limits above will result in an error.

Available libraries

Several popular Node.js libraries are available for use within the code snippet. It’s highly recommended you use the Node.js v20 runtime for security purposes, as v18 entered its official end-of-life (EOL) on April 30, 2025. The code block below provides version information for the libraries available to import into your snippet, specified using the npm semver convention.
The libraries can be loaded using the require() function at the top of your code. For example, the code block below imports the axios library and makes a request to the time.now API, then displays the current time in GMT.
The resulting output for running the above action in a bot is shown below:
Example code snippet in bot displaying the current time in GMT using axios and the time.now API
Last modified on April 15, 2026