Fetching contacts associated with a deal

Hi There - I am trying to retrieve contact details associated with a deal using associations API. I get the below error even though I have passed the parameters correctly.

TypeError: BasicApi.get_page() missing 2 required positional arguments: ‘object_type’ and ‘object_id’

Can someone clarify what is wrong with the code below? Thanks!

filter_groups = [{ "filters": [ { "propertyName": "dealstage", "operator": "EQ", "value": "closedlost" # Only closed-lost deals } ]}]search_request = { "filterGroups": filter_groups, "properties": ["dealname", "dealstage", "closedate"], "limit": 100}# Fetch deals within the specified date rangesearch_results = client.crm.deals.search_api.do_search(search_request)deals = search_results.resultsif not deals: print("No closed-lost deals found in the specified date range.") return# Process each deal to get associated contactsfor deal in deals: deal_id = deal.id deal_name = deal.properties.get("dealname") deal_close_date = deal.properties.get("closedate") print(f"Processing Deal: {deal_name}, Close Date: {deal_close_date}") # --- Get associated contacts using the Associations API --- associated_contacts = [] try: contact_associations = client.crm.associations.v4.basic_api.get_page( from_object_type="deals", from_object_id=deal_id, to_object_type="contacts", limit=100 ) if contact_associations.results: for association in contact_associations.results: contact_id = association.to_object_id try: # Fetch contact details by contact ID contact = client.crm.contacts.basic_api.get_by_id(contact_id, properties=["firstname", "lastname", "email", "jobtitle"]) contact_info = { "name": f"{contact.properties.get('firstname')} {contact.properties.get('lastname')}", "email": contact.properties.get("email"), "jobtitle": contact.properties.get("jobtitle") } associated_contacts.append(contact_info) print(f"Associated Contact: {contact_info['name']}, Email: {contact_info['email']}, Job Title: {contact_info['jobtitle']}") except ContactsApiException as e: print(f"Error retrieving contact {contact_id}: {e}")

Hey, @snarayanan :waving_hand: I hope you got this worked out already. If not, what happens when you make the same request using Postman?

Best,

Jaycee

Hi @Jaycee_Lewis - unfortunately, I’m still unable to get the code working. I tried V4 GET API in postman. Looks like its working in postman. Please advise on how I can solve the issue in my code.

Hi @snarayanan

If you want to read the contact associated with the deal then what you can do is use the deals search API to get the deal ID (if you don’t have it), using that you can use the deal read API and find associated contacts with that deal.

here is curl for read deal API curl --request GET \
 --url 'https://api.hubapi.com/crm/v3/objects/deals/?associations=contacts&archived=false' \
 --header 'authorization: Bearer YOUR_ACCESS_TOKEN'

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