Issue with Fetching Contacts via API (Contact Records associated to Multiple Company Records)
SOLVE
Hi!
We're having an issue currently with fetching data from HubSpot to our custom client reporting app via HubSpot's API.
Just for context, we have one contact record associated to multiple company records, with the first associated company as the primary company (by default that is).
Issue with Fetching Contacts via API (Contact Records associated to Multiple Company Records)
SOLVE
Hi, @RRazote8👋 Thanks for your question. The short answer is the endpoint you are using won't return all the associated contacts for a company in a single call.
To retrieve all companies associated with a contact, you may want to use the CRM Associations API, specifically the GET /crm/v3/objects/contacts/{contactId}/associations/companies endpoint. This endpoint retrieves all the companies associated with a specific contact.
One thought is to iterate through each company and a separate API call for each to get the associated contacts. It's not ideal from a performance standpoint, but it's the most straightforward way that considers the limitations of the endpoint you are using.
Out of curiosity, is there a reason why you're using the Companies API endpoint — Get All Companies, instead of the CRM Associations API endpoint? It might help the community to understand your use case better if you could provide more details.
The issue you're experiencing is likely due to the fact that the API endpoint you're using only returns the contact records that are associated with the **primary company**. To get the contact records that are associated with **all** of the companies that a contact is associated with, you'll need to use the following API endpoint:
In your case, you would replace the `<company_id_1>`, `<company_id_2>`, and `<company_id_3>`placeholders with the ID values of the companies that you want to get contact records for.
Here is an example of how to use the API endpoint to get contact records for three companies:
This API endpoint will return a JSON response that contains an array of contact objects. Each contact object will contain the contact's data, including the company IDs that the contact is associated with.
Here is an example of a JSON response that would be returned for the above API request:
The issue you're experiencing is likely due to the fact that the API endpoint you're using only returns the contact records that are associated with the **primary company**. To get the contact records that are associated with **all** of the companies that a contact is associated with, you'll need to use the following API endpoint:
In your case, you would replace the `<company_id_1>`, `<company_id_2>`, and `<company_id_3>`placeholders with the ID values of the companies that you want to get contact records for.
Here is an example of how to use the API endpoint to get contact records for three companies:
This API endpoint will return a JSON response that contains an array of contact objects. Each contact object will contain the contact's data, including the company IDs that the contact is associated with.
Here is an example of a JSON response that would be returned for the above API request:
Issue with Fetching Contacts via API (Contact Records associated to Multiple Company Records)
SOLVE
Hi, @RRazote8👋 Thanks for your question. The short answer is the endpoint you are using won't return all the associated contacts for a company in a single call.
To retrieve all companies associated with a contact, you may want to use the CRM Associations API, specifically the GET /crm/v3/objects/contacts/{contactId}/associations/companies endpoint. This endpoint retrieves all the companies associated with a specific contact.
One thought is to iterate through each company and a separate API call for each to get the associated contacts. It's not ideal from a performance standpoint, but it's the most straightforward way that considers the limitations of the endpoint you are using.
Out of curiosity, is there a reason why you're using the Companies API endpoint — Get All Companies, instead of the CRM Associations API endpoint? It might help the community to understand your use case better if you could provide more details.
Issue with Fetching Contacts via API (Contact Records associated to Multiple Company Records)
SOLVE
Hi Jaycee,
Our dev responsible for this is actually the one asking haha (I'm not a dev btw, just an admin).
But basically, we wanted to have a 2-in-1 query that fetches contact data from an associated company, but failed to do so because we haven't reviewed the query string that much.
But this is all good, we actually managed to do a 2-query approach wherein we did the following:
1. Added this string so we can query the associated contact in a company