APIs & Integrations

aparisi
Participant

CRM API Search Contact - Need to set limit=2 to get one result

Hi, HubSpot community

 

I'm using the HubSpot python library to search contacts by email:

 

filters = [Filter(property_name="email", operator="EQ", value="test@test.com")]
filter_group = FilterGroup(filters=filters)
request = PublicObjectSearchRequest(
            filter_groups=[filter_group],
            limit=1,
            properties=["hs_object_id"]
)
api_client.crm.contacts.search_api.do_search(search_request)

 

And this is the response I get:

{'paging': {'next': {'after': '1', 'link': None}}, 'results': [], 'total': 2}

 

However, if I modify my request and set limit=2, I get the only registry that exists with that email:

{'paging': None,
 'results': [{'archived': False,
              'archived_at': None,
              'created_at': datetime.datetime(2023, 2, 3, 9, 58, 20, 684000, tzinfo=tzutc()),
              'id': '60551',
              'properties': {'hs_object_id': '60551'},
              'properties_with_history': None,
              'updated_at': datetime.datetime(2023, 2, 3, 9, 59, 4, 657000, tzinfo=tzutc())}],
 'total': 2}

 

It's interesting that in both responses, "total" is 2, even when there's only one record with that email.

Another thing to notice is that this problem doesn't happen with all the emails.

 

1) Does anyone know why this could happen?

2) What's the meaning of "total"? Is it the total amount of registries with that email? If that's the meaning, the value is totally wrong.

 

Thanks in advance,

Adrian

 

3 Replies 3
ChrisoKlepke
Key Advisor | Elite Partner
Key Advisor | Elite Partner

CRM API Search Contact - Need to set limit=2 to get one result

Hey @aparisi , 

 

thank you for reaching out to the community, and I'm happy to look at your code. From what I can understand, the syntax looks a bit off. I took the liberty and refactored it a little and added an API exception in case something is messed up.

 

import os
from pprint import pprint
from hubspot import HubSpot
from hubspot.crm.contacts import PublicObjectSearchRequest, ApiException

api_client = HubSpot(access_token=os.getenv("access_token"))

public_object_search_request = PublicObjectSearchRequest(
    filter_groups=[
        {
            "filters": [
                {
                    "propertyName": "email",
                    "operator": "EQ",
                    "value": "test@test.com",
                }
            ]
        }
    ],
    properties=["hs_object_id"],
    limit=1,
)
try:
    api_response = api_client.crm.contacts.search_api.do_search(
        public_object_search_request=public_object_search_request
    )
    pprint(api_response)
except ApiException as e:
    print("Exception when calling search_api->do_search: %s\n" % e)

 

I can't really speak to the issues you're currently experiencing, though. But please let me know if this helps already and/or what responses you're getting. 

 

The total, however, speaks to the number of contacts in your case that can be found with your parameters. It doesn't mean that these are returned the response. Limit means the number of results per page. If you want to cycle to the next page, you will need to use the after argument. 

 

If this solves it, consider helping others in the community to find answers faster by marking this as a solution. I'd certainly appreciate it. 

 

Cheers, 

Chriso

0 Upvotes
aparisi
Participant

CRM API Search Contact - Need to set limit=2 to get one result

Hey @ChrisoKlepke, thanks for your quick reply.

 

From the snipped I posted, I excluded some details like error handling for the sake of simplicity. Regardless of the legibility of the code, I would like to focus on the actual issue: Why do I need to specify limit=2 to get a single contact by email?

 

 

ChrisoKlepke
Key Advisor | Elite Partner
Key Advisor | Elite Partner

CRM API Search Contact - Need to set limit=2 to get one result

Hey @aparisi , 

 

Like I said, I can't speak to the problem you're facing regarding the limit. But I noticed that the syntax in the filter, filter groups and PublicObjectSearchRequest looked off. I would suggest trying that and see what responses you're getting this time, but if that doesn't help, I can't think of anything else. Maybe someone else has an idea. 

 

Cheers, 

Chriso

0 Upvotes