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.Documentation Index
Fetch the complete documentation index at: https://developers.hubspot.com/docs/llms.txt
Use this file to discover all available pages before exploring further.
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.

- The
exports.main()function is called when the code snippet action is executed. - The
eventargument 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 theexports.mainfunction.
event object contains the following properties:
| Property | Type | Description |
|---|---|---|
userMessage | Object | An object that contains two fields:
|
session | Object | An object that contains the following fields:
|
customState | Object | Only present if customState was passed in from a previous callback payload. |
event object for a contact who was chatting with a bot.
callback() function is used to send data back to the bot. The argument should be an object containing the following data:
| Property | Type | Description |
|---|---|---|
botMessage | String | This is the message your bot will display to the visitor. |
quickReplies | Array | A list of quick reply options passed to the bot after a click. Each option is an object with two fields:
|
nextModuleNickname | String | The nickname of the next module the bot should execute. If undefined, the bot will follow the default configured behavior. |
responseExpected | Boolean | If true, the bot will display the returned botMessage, wait for a response, then execute this code snippet again with that new response. |
customState | Object | Optional field to pass along to the next step. |
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.
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 thenpm semver convention.
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.
