# Working example of retrieving associations with the python API

**URL:** <https://community.hubspot.com/t/working-example-of-retrieving-associations-with-the-python-api/82091>\
**Category:** APIs & Integrations\
**Tags:** python\
**Created:** [May 26, 2023, 10:49am UTC](https://community.hubspot.com/t/working-example-of-retrieving-associations-with-the-python-api/82091 "2023-05-26T10:49:43Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![CDahl1](https://avatars.discourse-cdn.com/v4/letter/c/34f0e0/32.png) [@CDahl1](https://community.hubspot.com/u/CDahl1)\
**Post date:** [May 26, 2023, 10:49am UTC](https://community.hubspot.com/t/working-example-of-retrieving-associations-with-the-python-api/82091/1 "2023-05-26T10:49:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [May 27, 2023, 3:44am UTC](https://community.hubspot.com/t/working-example-of-retrieving-associations-with-the-python-api/82091/2 "2023-05-27T03:44:40Z")

</div>

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?

Thank you! — Jaycee

---

<div class="post-metadata">

**Author:** ![CDahl1](https://avatars.discourse-cdn.com/v4/letter/c/34f0e0/32.png) [@CDahl1](https://community.hubspot.com/u/CDahl1)\
**Post date:** [May 29, 2023, 10:50am UTC](https://community.hubspot.com/t/working-example-of-retrieving-associations-with-the-python-api/82091/3 "2023-05-29T10:50:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GRajput](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/grajput/32/102020_2.png) [@GRajput](https://community.hubspot.com/u/GRajput)\
**Post date:** [May 28, 2023, 2:29pm UTC](https://community.hubspot.com/t/working-example-of-retrieving-associations-with-the-python-api/82091/4 "2023-05-28T14:29:57Z")

</div>

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](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!

---

<div class="post-metadata">

**Author:** ![CDahl1](https://avatars.discourse-cdn.com/v4/letter/c/34f0e0/32.png) [@CDahl1](https://community.hubspot.com/u/CDahl1)\
**Post date:** [May 29, 2023, 10:48am UTC](https://community.hubspot.com/t/working-example-of-retrieving-associations-with-the-python-api/82091/5 "2023-05-29T10:48:26Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![LucasV](https://avatars.discourse-cdn.com/v4/letter/l/4bbf92/32.png) [@LucasV](https://community.hubspot.com/u/LucasV)\
**Post date:** [December 15, 2023, 5:06pm UTC](https://community.hubspot.com/t/working-example-of-retrieving-associations-with-the-python-api/82091/6 "2023-12-15T17:06:02Z")

</div>

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](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 !

---

<div class="post-metadata">

**Author:** ![CDahl1](https://avatars.discourse-cdn.com/v4/letter/c/34f0e0/32.png) [@CDahl1](https://community.hubspot.com/u/CDahl1)\
**Post date:** [May 31, 2023, 8:19pm UTC](https://community.hubspot.com/t/working-example-of-retrieving-associations-with-the-python-api/82091/7 "2023-05-31T20:19:23Z")

</div>

Finally figured it out - the documentation is truly bad here - but this works.

```lia-code-sample
# 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)

```

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [May 31, 2023, 8:42pm UTC](https://community.hubspot.com/t/working-example-of-retrieving-associations-with-the-python-api/82091/8 "2023-05-31T20:42:20Z")

</div>

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?

```lia-code-sample
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

---

<div class="post-metadata">

**Author:** ![nma2242](https://avatars.discourse-cdn.com/v4/letter/n/9de0a6/32.png) [@nma2242](https://community.hubspot.com/u/nma2242)\
**Post date:** [October 2, 2025, 9:50pm UTC](https://community.hubspot.com/t/working-example-of-retrieving-associations-with-the-python-api/82091/9 "2025-10-02T21:50:26Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [October 3, 2025, 8:56pm UTC](https://community.hubspot.com/t/working-example-of-retrieving-associations-with-the-python-api/82091/10 "2025-10-03T20:56:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [October 20, 2025, 4:16pm UTC](https://community.hubspot.com/t/working-example-of-retrieving-associations-with-the-python-api/82091/11 "2025-10-20T16:16:28Z")

</div>

Hey, @nma2242 👋 I don’t see the “old” landing page either. But here is a direct link to the repo — [**HubSpot Python SDK**](https://github.com/HubSpot/hubspot-api-python?tab=readme-ov-file#readme). — Jaycee

---

<div class="post-metadata">

**Author:** ![BQaddoumi](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/bqaddoumi/32/24676_2.png) [@BQaddoumi](https://community.hubspot.com/u/BQaddoumi)\
**Post date:** [March 28, 2024, 2:17pm UTC](https://community.hubspot.com/t/working-example-of-retrieving-associations-with-the-python-api/82091/12 "2024-03-28T14:17:00Z")

</div>

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)
