Retrieving All Contact Associations with Companies via HubSpot's Associations API

Hello,

I’m currently working with the HubSpot CRM Associations API to establish connections between contacts and companies. My goal is to retrieve all associations a contact has with various companies, not just the primary contact association.

Here’s the approach I’ve taken so far:

from hubspot.crm.associations import (
BatchInputPublicAssociation,
BatchInputPublicObjectId,
ApiException as AssociationApiException,
BatchResponsePublicAssociationMulti,
)

def bulk_read_associations(associations: pd.DataFrame, from_object_type: str, to_object_type: str):
batch_input_public_object_id = BatchInputPublicObjectId(
inputs=[{“id”: from_id} for from_id in associations[“from_id”].unique()])
try:
api_response = CLIENT.crm.associations.batch_api.read(
from_object_type=from_object_type,
to_object_type=to_object_type,
batch_input_public_object_id=batch_input_public_object_id,
)
except ApiException as e:
traceback.print_exc()
print(“Exception when calling batch_api->read: %s\n” % e)
raise e
return api_response

With this code, I’m only able to fetch the primary contact associations with companies. I’m looking for guidance on how to modify this code or use a different method to access all the associations a contact has with various companies, including secondary or tertiary associations.

Any advice or insights on this matter would be greatly appreciated!

Thank you in advance for your help.

Albert

Data Lead in Deale

Hi @AYanguas

Use this Curl

var request = require('request');
var options = {
 'method': 'GET',
 'url': 'https://api.hubapi.com/crm/v3/objects/contacts/Contcat_ID?associations=companies&archived=false',
 'headers': {
 'authorization': 'Bearer token'
 }
};
request(options, function (error, response) {
 if (error) throw new Error(error);
 console.log(response.body);
});

I hope this will help you out. Please mark it as Solution Accepted and upvote to help another Community member.
Thanks!

Hello Gaurav,

In other words, I have to make a call for each contact, right? Isn’t there a way to do it in batches?

Thank you very much for the quick reply.

I finally found the solution using the api v4.

I leave the code I used to get the data.

from hubspot.crm.associations.v4 import (

BatchInputPublicFetchAssociationsBatchRequest,

ApiException,

MultiAssociatedObjectWithLabel,

)

def bulk_read_associations_v4(

associations: pd.DataFrame, from_object_type: str, to_object_type: str, association_type: str

)

batch_input_public_fetch_associations_batch_request = BatchInputPublicFetchAssociationsBatchRequest(

inputs=[{“id”: from_id} for from_id in associations[“from_id”].unique()]

)

try:

api_response = CLIENT.crm.associations.v4.batch_api.get_page(

from_object_type=from_object_type,

to_object_type=to_object_type,

batch_input_public_fetch_associations_batch_request=batch_input_public_fetch_associations_batch_request,

)

except ApiException as e:

print(“Exception when calling batch_api->get_page: %s\n” % e)

return get_associations(data=api_response, association_type=association_type)