Basic Node Example from Associations v4 not working

I want to use a basic node example from API > CRM > Associations v4

For some reason I get an error, even if I use the basic example.

test.js:16

const apiResponse = hubspotClient.crm.associations.v4.basicApi.get-/crm/v4/objects/{objectType}/{objectId}/associations/{toObjectType}(objectType, objectId, toObjectType, after, limit);

“SyntaxError: Invalid regular expression flags”

I would be grateful for any support!

Full Code

const hubspot = require("@hubspot/api-client");
const config = require("config");
const token = config.get("token");
const hubspotClient = new hubspot.Client({ accessToken: token });

const objectType = "deal";
const objectId = 9005914056;
const toObjectType = "contact";
const after = undefined;
const limit = 500;

try {
 const apiResponse = await hubspotClient.crm.associations.v4.basicApi.get-/crm/v4/objects/{objectType}/{objectId}/associations/{toObjectType}(objectType, objectId, toObjectType, after, limit);
 console.log(JSON.stringify(apiResponse, null, 2));
} catch (e) {
 e.message === 'HTTP request failed'
 ? console.error(JSON.stringify(e.response, null, 2))
 : console.error(e)
}

Hi @db_immocation

In my experience most API calls have a limit of 100.

The documentation points to Rate Limits rather than the Limit Parameter, so I would try your code with a limit of 100.

Did that work?

Mike

Here to learn more about HubSpot and share my HubSpot Knowledge. I’m the founder of Webalite a HubSpot Partner Agency based in Wellington, New Zealand and the founder of Portal-iQ the world’s first automated HubSpot Portal Audit that helps you work smarter with HubSpot.

@Mike_EastwoodNo. That changes nothing. A limit of 1 does not work either.

Just wanted to throw in a strong upvote for this; I don’t know why the HubSpot API documentation

  1. Never seems to use the same method of querying itself twice
  2. Has examples with immediate top-level errors so frequently

I’ve usually been able to puzzle my way through it (and will report back here if I can fix this too) but the documentation being inconsistent and wrong is a huge barrier to entry here; especially for folks who are just trying to custom code query their own instance.

I discussed the problem with a Node developer friend. His best guess:

The HubSpot documentation is wrong.

With the warning that I am not a node developer, but an amateur who hacks things to work in HubSpot workflows, I was able to get the response I wanted with a more generic request made to the v4 API. HubSpot’s custom code actions don’t actually support the version of the HubSpot API Client that the v4 Associations API is on, which is nightmarish, and their editor hates the sample code you were using (which I know is what is in the documentation HubSpot themselves provides).

For reference, here is HubSpot’s documentation showing which version of their own api the custom code action supports (v8), and here is them saying that Associations v4 is only on v9 and higher, so you have to use the generic http request to use the Associations v4 API.

Here is the code I used in my custom code action, where I am trying to retrieve all Companies associated to the Deal the workflow is running from. ‘record_id’ is my input variable of the deal’s id, and it’s outputting the whole response as a string for a future code action. Use at your own risk, this is only to the best of my current ability and not something I can validate.

var http = require("https");

exports.main = async (event, callback) => {
const dealRecordId = event.inputFields['record_id'].trim();

 var options = {
 "method": "GET",
 "hostname": "api.hubapi.com",
 "port": null,
 "path": "/crm/v4/objects/deal/"+dealRecordId+"/associations/company",
 "headers": {
 "accept": "application/json",
 "content-type": "application/json",
 "authorization": "Bearer "+process.env.Workflow_2_Token
 },

 };

 try {
 var req = await http.get(options, function (res) {
 var chunks = [];
 res.on("data", function (chunk) {
 chunks.push(chunk);
 });
 res.on("end", function () {
 var body = Buffer.concat(chunks);
 console.log(body.toString());
 callback({
 outputFields: {
 companiesFromDeal: body.toString()
 }
			});
 });
 });
 req.end();
 } catch (e) {
 e.message === 'HTTP request failed'
 ? console.error(JSON.stringify(e.response, null, 2))
 : console.error(e)
 }
}

Thank you very much for your thoughts on this topic and for the example you gave. You are absolutely right - at the moment we have to work with the wrong documentation and use alternative approaches.
Just as a note, I tried the example on my local machine with the latest version of hubspot/api-client and it doesn’t work there either.

db_immocation_0-1697444303471.png

Ah (huh, HubSpot censors h*ll, that’s funny) - I’m not sure exactly where the distinction would be; as my example is running in custom code actions on their API v8.

Except - you might not have the https protocol in your package? The example actually just doesn’t use the HubSpot API at all due to the issues you identified. Depending on how you’re executing (looks like node) you may also need to swap out require for

import http from "https";

because that’s a whole thing with node.

@ArdenM Sorry my comment was misleading. I mean my inital example from Associations V4.
Your (alternative) code ist running fine.

Totally fine, thanks for the clarification! Glad that you’ve got something working. Even though the documentation is still wrong, would you mind marking my previous response as a solution? It might help other folks with the same problem. No worries if not!