Calling HubSpot API endpoints from chatflow (conversation) run code snippet

Hi all,

It seems you cannot call

const hubspot = require(‘@hubspot/api-client’);

in a “run code snippet” action in a chatflow conversation.

So then how would one call an HubSpot API Endpoint from inside a chatflow?
I am trying to write some code which searches through a specific objectType to return results.

// Import the Hubspot NodeJS Client Library - this will allow us to use the HubSpot APIs

const hubspot = require(‘@hubspot/api-client’);

/*

This function is called when the custom code action is executed.

*/

exports.main = (event, callback) => {

const objectType = “2-25573472”; // This is the object type ID for the “Location” object in HubSpot

let searchResults = [];

//const inviteeEmail = event.inputFields[‘email’];

const city = event.userMessage.message

const hubspotClient = new hubspot.Client({ accessToken: “tokenHERE” });

// Create search criteria

const filter = { propertyName: ‘city’, operator: ‘EQ’, value: city }

const filterGroup = { filters: [filter] }

const sort = JSON.stringify({ propertyName: ‘city’, direction: ‘DESCENDING’})

const properties = [‘name’]

const searchCriteria = {

filterGroups: [filterGroup],

sorts: [sort],

properties

}

// Search the CRM for Location matching “city” variable defined earlier

hubspotClient.crm.objects.searchApi.doSearch(objectType, searchCriteria).then(searchLocationResponse => {

// If total equals 0 no results found

if(searchLocationResponse.total != 0){ //NO MATCH FOUND

searchResults = searchLocationResponse.results;

// If a match is found, return the Location object

const responseJson = {

botMessage: `I found ${searchResults.length} locations matching the city ${city}.`,

responseExpected: false,

searchResults: searchResults

}

callback(responseJson);

} else {

// If no match is found, return an error message

const responseJson = {

botMessage: `I’m sorry, I couldn’t find any locations matching the city ${city}.`,

responseExpected: false

}

callback(responseJson);

}

});

}

Hi @MGrezak :waving_hand:

As per HubSpot’s documentation, there are currently only a handful of Node.js libraries available for use in Chatflow Code Snippets. Unfortunately, HubSpot’s API Client doesn’t seem to be one of them. As such, you’ll have to rely on what’s currently available for making HTTP requests (I’m guessing this will be the deprecated “request” library).

I hope that proves helpful. Please let me know if you have any follow-up questions.