There's a new version of the HubSpot API

As of November 30, 2022, HubSpot API Keys are being deprecated and are no longer supported. Continued use of HubSpot API Keys is a security risk to your account and data. Your API Keys could be deactivated at any time after Nov. 30th, and we recommend that you migrate to Private Apps as soon as possible so you do not lose business-critical functionality.

Running code snippets in bots

When creating or editing a bot, you may add a code snippet by clicking the "+" to add an action as you normally would. From the action selection panel, click on "Run a code snippet".run-a-code-snippet-2


Next, give your action a nickname. Within the code editing pane, you will see our default template for Node.js 6.10. The details of the "event" object and possible response object formats are detailed


Once you've saved your action, the code will be triggered once the applicable part of the conversation has been reached.

There are three main items 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 contains details for the visitor and chat session.
  • The callback() function is used to pass data back to the bot and user and should be called in the exports.main function.

The event object will contain the following data:

  "userMessage": {
  // Details for the last message sent to your bot
    "message": "100-500",
    // The last message received by your bot, sent by the visitor
    "quickReply": {
    // If the visitor selected any quick reply options, this will be a list of the selected options.
    // Will be 'null' if no options were selected.
      // A list of quick reply options selected by the visitor
  "session": {
    "vid": 12345,
    // The contact VID of the visitor, if known.
    "properties": {
    // A list of properties collected by the bot in the current session.
      "CONTACT": {
        "firstname": {
          "value": "John",
          "syncedAt": 1534362540592
        "email": {
          "value": "",
          "syncedAt": 1534362541764
        "lastname": {
          "value": "Smith",
          "syncedAt": 1534362540592

The callback() function is used to send data back to the bot. The argument should be an object with the following data:

  "botMessage": "Thanks for checking out our website!",
  // This is the message your bot will display to the visitor.
"quickReplies": [
value:'option', // Passed to the bot as the response on click
label:'Option' // Gets displayed as the button label
], // the quickReplies object is optional "nextModuleNickname": "SuggestAwesomeProduct", // The nickname of the next module the bot should execute. If undefined, the bot will follow the default configured behavior "responseExpected": false // If true, the bot will display the returned botMessage, wait for a response, then execute this code snippet again with that new response. }

Available libraries

Several popular Node.js libraries are available for use within the code snippet.

The libraries can be loaded using the require() function at the top of your code as normal.

const request = require('request');

exports.main = (event, callback) => {
    request('', function (error, response, body) {

        const responseJson = {
            "botMessage": "The current time in GMT is " + JSON.parse(body).time,
            "responseExpected": false