Working example of retrieving associations with the python API

I am trying to find even one example of retrieving associations between companies and contacts using the python API.

The closest I’ve come is suggestions to use something like

api_client.crm.associations.batch_api.read("companies", "contacts")

but that fails with an ApiException (a missing request body).

I simply want to find all associations between any contact and any company in a (fairly small) hubspot project.

Hi, @CDahl1 :waving_hand: 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?

Thank you! — Jaycee

Is it simply impossible to do the fairly straightforward action @GRajput did with the rest API with the client SDK?

Hi @CDahl1

Sure, here is an example of how to retrieve associations between companies and contacts using the Python API:

import requests

# Get your HubSpot API key from the HubSpot Admin Center.
API_KEY = “YOUR_HUBSPOT_API_KEY”

# Create a request object.
request = requests.Request(“GET”, “https://api.hubapi.com/crm/v3/associations/companies/contacts/batch/read”, headers={“Authorization”: “Bearer {}”.format(API_KEY)})

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

Thanks!

Thx - but that is not an example of using Hubspot’s python API (you’re using Requests - and the raw json API - not Hubspot’s client SDK)

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 :

# Create a request object.

request = requests.Request(“GET”, “https://api.hubapi.com/crm/v3/associations/companies/contacts/batch/read”, headers={“Authorization”: “Bearer {}”.format(API_KEY)})

prepared_request = requests.Session().prepare_request(request)

# Send the request.

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 :waving_hand: 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.

Again, thank you very much for your follow-up.

Best,

Jaycee

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?

Hey, @nma2242 :waving_hand: I’ll take a look and follow up here. — Jaycee

Hey, @nma2242 :waving_hand: I don’t see the “old” landing page either. But here is a direct link to the repo — HubSpot Python SDK. — Jaycee

This is the way! Thank you very much. The docs are quite abstract and lead down a path that doesn’t really make sense (or work)