Hi, @CDahl1 Thanks for your question. The batch read API method you’re trying to use requires a body with a list of the IDs of the objects you want to find associations for. Hey, @JBeatty@ChrisoKlepke, do you have a Python example handy? Or any suggestions for @CDahl1?
# Send the request.
response = requests.Session().send(request)
# Check the response status code.
if response.status_code == 200:
# The request was successful.
# Get the response body.
response_body = response.json()
# Iterate over the associations.
for association in response_body[“results”]:
# Print the company and contact IDs.
print(association[“from”][“id”], association[“to”][“id”])
else:
# The request failed.
print(response.status_code)
This code will retrieve all associations between companies and contacts in your HubSpot account. The associations will be returned in a JSON array. Each association will have two properties: from and to. The from property will contain the ID of the company, and the to property will contain the ID of the contact.
You can use this code to retrieve associations between companies and contacts in any HubSpot project, regardless of its size.
Hope this will helps you out. Please mark it as Solution Accepted to help other Community member.
Hi @GRajput ,
I’ve tried your code, unfortunatly it return an error : “can only send prepared requests”
Thus, I have updated your code with this part :
response = requests.Session().send(prepared_request)
But it still return an error 405
It would really help me to run this one ion order to get a batch on all associations instead of running one time for each company.
Thanks for you help !
Finally figured it out - the documentation is truly bad here - but this works.
# assuming I already retrieved a list of companies
from hubspot.crm.associations import BatchInputPublicObjectId
batch_ids = BatchInputPublicObjectId([{'id': c.id} for c in companies] )
associations = api_client.crm.associations.batch_api.read(from_object_type="company",
to_object_type="contacts",
batch_input_public_object_id=batch_ids)
Hey, @CDahl1 Thank you very much for taking the time to come back and post your solution. I agree there are areas, like this one, where we can improve the documentation and provide examples.
I was out for an extended weekend over the holiday, but I was curious about finding an example.
Making the same assumption that you’ve already acquired your companies, have you tried something like this?
for company in companies:
# Create PublicObjectId for the company
company_id = PublicObjectId(id=company.id)
# Retrieve associated contacts for the company
associated_contacts = crm_associations_api.batch_api.read(
from_object_type="companies",
to_object_type="contacts",
batch_input_public_object_id=[company_id]
)
To loop over each company to get the associated contacts? With ‘associated_contacts’ containing the contacts associated with the current company.
Hey Jaycee - replying here because while the new Hubspot API documentation has been great for me so far, but I still have not been able to find documentation specfic to the Python SDK. Can you tell me how to tell what methods/properties are available to each object when using the Python SDK?