Different Responses Fix when using "/settings/v3/users/" Endpoint

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_stateSUCCESS
primaryTeamIdNot defined in output field[]
ownerIdNot defined in output field458921319
ownerNameNot defined in output field

Why might this be occuring?

Hi @Drew_Kulas, I hope that you are well!
Thanks for reaching out to the HubSpot Community!
I’d love to put you in touch with our Top Experts: Hi @sylvain_tirreau, @Anton and @zach_threadint do you have suggestions to help @Drew_Kulas, please?
Have a great day and thanks so much in advance for your help!
Bérangère

Hi @Drew_Kulas, I hope that you are well!
I wanted to check on you, do you still need help?
If yes, let me check with our Top Experts to see if they have ideas for you:
Hi @Teun, @stefen and @Indra do you have suggestions to help @Drew_Kulas, please?
Have a lovely day and thanks so much in advance for your valuable contributions!
Bérangère

Yes I do still need hel.

Hi @Drew_Kulas, ok thanks for confirming!
Let’s check with other Experts and see if we can get you some help:
Hi @WesQ, @mangelet and @Gonzalo do you have some ideas to help @Drew_Kulas, please?
Have a great day and thanks so much for your help!
Bérangère

Hi @Drew_Kulas, I hope that you are well!
For information, here are some documentation about the endpoint you mentioned that might help you:
- User provisioning
- Users
- Response: json() method

It seems that your code expected JSON from https://api.hubapi.com/settings/v3/users/458921319, but instead received HTML.
The error shows the response began with <html>, which is typical when receiving an error page (e.g., 404 Not Found, 401 Unauthorized, or 500 Internal Server Error), not valid API JSON.
Can you confirm that you are indeed trying to fetch teams for the user via this endpoint?
Not all endpoints return team data directly, make sure you’re calling the right one.
Also, the API endpoints require a valid OAuth token or API key. Can you please double-check your headers?
If your token is expired or invalid, HubSpot returns an HTML error page, which causes this parsing issue.
Hi @sylvain_tirreau, @MatthewShepherd, @alyssamwilie and @Kevin-C do you have other suggestions to help @Drew_Kulas, please?
Have a lovely day and thanks so much in advance!
Bérangère

Hi,

hubspot_owner_id returns the ID of an Owner (id field). The actual UserID expected by the Settings API is found in the userId property of the OwnersAPI response; if you pass the ownerId as is, Hubspot often returns an HTML login page (hence the “<html>…”).
I would try this in your place:

const owner = await ownersApi.getById(ownerId)
idToUse = owner.userId
requestUrl = `/settings/v3/users/${idToUse}`