APIs & Integrations

RRazote8
Participant

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).

 

Here's what we're using to get the contact record data from HubSpot: https://api.hubapi.com/companies/v2/companies/<companyID>/contacts

 

What's happening is that the contact record data for the first associated company is fetching, but for the other 3 it's not. It just posts as this:

RRazote8_0-1685327373039.png

 

Open to any suggestion on how to fix this. Thanks!

2 Accepted solutions
Jaycee_Lewis
Solution
Community Manager
Community Manager

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.

 

I have an example here using a contact and its associated companies from my test portal — Get all associated company of a 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.

 

Thank you! — Jaycee

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

View solution in original post

0 Upvotes
GRajput
Solution
Key Advisor | Platinum Partner
Key Advisor | Platinum Partner

Issue with Fetching Contacts via API (Contact Records associated to Multiple Company Records)

SOLVE

Hi @RRazote8 

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:

```
https://api.hubapi.com/contacts/v2/contacts?company_ids=<company_id_1>,<company_id_2>,<company_id_3>
```

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:

```
https://api.hubapi.com/contacts/v2/contacts?company_ids=1234567890,9876543210,0123456789
```

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:

```
{
"contacts": [
{
"id": "1234567890",
"company_ids": [
1234567890,
9876543210,
0123456789
]
},
{
"id": "9876543210",
"company_ids": [
9876543210,
0123456789
]
},
{
"id": "0123456789",
"company_ids": [
0123456789
]
}
]
}
```

Hope this will helps you out. Please mark it as Solution Accepted to help other Community member.

Thanks!




Gaurav Rajput
Director, MarTech( Growth Natives)

Book a meeting


View solution in original post

0 Upvotes
4 Replies 4
GRajput
Solution
Key Advisor | Platinum Partner
Key Advisor | Platinum Partner

Issue with Fetching Contacts via API (Contact Records associated to Multiple Company Records)

SOLVE

Hi @RRazote8 

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:

```
https://api.hubapi.com/contacts/v2/contacts?company_ids=<company_id_1>,<company_id_2>,<company_id_3>
```

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:

```
https://api.hubapi.com/contacts/v2/contacts?company_ids=1234567890,9876543210,0123456789
```

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:

```
{
"contacts": [
{
"id": "1234567890",
"company_ids": [
1234567890,
9876543210,
0123456789
]
},
{
"id": "9876543210",
"company_ids": [
9876543210,
0123456789
]
},
{
"id": "0123456789",
"company_ids": [
0123456789
]
}
]
}
```

Hope this will helps you out. Please mark it as Solution Accepted to help other Community member.

Thanks!




Gaurav Rajput
Director, MarTech( Growth Natives)

Book a meeting


0 Upvotes
Jaycee_Lewis
Solution
Community Manager
Community Manager

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.

 

I have an example here using a contact and its associated companies from my test portal — Get all associated company of a 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.

 

Thank you! — Jaycee

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 Upvotes
RRazote8
Participant

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

api.hubapi.com/crm/v3/objects/companies/<companyID>?associations=contacts&archived=false

 2. Then whatever's the endpoint on the first one, we'll get the contact ID and get the email, first name and last name using this query string: 

api.hubapi.com/crm/v3/objects/contacts/<contactID>?archived=false

 

Basically, the end result would be for us to post the data in our custom client reporting which will look like this (as representation only):

RRazote8_0-1685432910840.png

 

I hope that made sense haha but we'll still use the query you mentioned above in other scenarios that we will encounter. Thanks for this anyway!

0 Upvotes
Jaycee_Lewis
Community Manager
Community Manager

Issue with Fetching Contacts via API (Contact Records associated to Multiple Company Records)

SOLVE

Hey, @RRazote8 👋 Thanks for letting us know! Have a fantastic weekend! — Jaycee

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 Upvotes