I am using the `/settings/v3/users/’ endpoint. I am using this to build custom code for an activty tracker that triggers when certain activites have occured, such as Emails, Calls, Meetings, and Deals. I have set up Teams in HubSpot based on the users roles. The principle is to use this activity tracker to keep communication with clients through only Account Executives. I am using the hubspot_owner_id. Here is the function I am using as well as the error message from the failed response when testing in HubSpot:
const Hubspot = require(‘@hubspot/api-client’);
// Fetch the teams for a given user (owner)
async function fetchUserTeams(hubspotClient, userId) {
try {
console.log(`Fetching teams for userId: ${userId}`);
const resp = await hubspotClient.apiRequest({
method: ‘GET’,
path: `/settings/v3/users/${userId}`,
});
const json = await resp.json();
console.log(‘Full API response:’, json);
// Extract primary team ID
const primaryTeamId = json.primaryTeamId ? [json.primaryTeamId] : [];
const teamIds = […primaryTeamId];
console.log(`Found teamIds for user ${userId}:`, teamIds);
return teamIds;
} catch (error) {
console.log(`Failed to fetch teams for userId: ${userId}. Error:`, error);
return [];
}
}
// Fetch owner details (optional, for logging/display)
async function fetchOwner(hubspotClient, ownerId) {
if (!ownerId) return { firstName: ‘Unknown’, lastName: ‘Owner’ };
const resp = await hubspotClient.apiRequest({
method: ‘GET’,
path: `/crm/v3/owners/${ownerId}`,
});
const json = await resp.json();
return { firstName: json.firstName, lastName: json.lastName };
}
exports.main = async (event, callback) => {
// Log the full event for debugging
console.log(‘Full event:’, JSON.stringify(event, null, 2));
// Use only hs_object_id as the contact ID
const contactId = event.inputFields.hs_object_id;
console.log(‘Using contactId (hs_object_id):’, contactId);
if (!contactId) {
console.log(‘No hs_object_id found in inputFields!’);
callback({ outputFields: { error: ‘No hs_object_id found in inputFields!’ } });
return;
}
const hubspotClient = new Hubspot.Client({ accessToken: process.env.PrivateAccessKey });
// Get the contact’s owner ID
const contact = await hubspotClient.crm.contacts.basicApi.getById(contactId, [‘hubspot_owner_id’]);
const ownerId = contact.properties.hubspot_owner_id;
console.log(‘Contact ownerId (hubspot_owner_id):’, ownerId);
const owner = await fetchOwner(hubspotClient, ownerId);
// Fetch and log the primary team for the owner
const primaryTeamId = await fetchUserTeams(hubspotClient, ownerId);
// Log the result
console.log(‘Primary Team ID for owner:’, primaryTeamId);
// Return results in outputFields
callback({ outputFields: {
primaryTeamId,
ownerId,
ownerName: `${owner.firstName} ${owner.lastName}`
}});
};
Error Response:
2025-06-16T17:43:15.185Z INFO Failed to fetch teams for userId: 458921319. Error: FetchError: invalid json response body at https://api.hubapi.com/settings/v3/users/458921319 reason: Unexpected token '<', "<html>
<he"... is not valid JSON
at /opt/nodejs/node_modules/node-fetch/lib/index.js:273:32
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async fetchUserTeams (/var/task/file.js:11:18)
at async exports.main (/var/task/file.js:60:25) {
type: 'invalid-json'
}
| hs_execution_state | SUCCESS |
| primaryTeamIdNot defined in output field | [] |
| ownerIdNot defined in output field | 458921319 |
| ownerNameNot defined in output field |
Why might this be occuring?