Search for users by email with multiple emails

SOLVE
JAnderson1
Participant

Hi,

 

I have a user with a secondary and a primary email. What API call do I use to find the user by primary or secondary email? This call fails to return any results. I tried both hs_additonal_emails and email as the field:

 

POST https://api.hubapi.com/crm/v3/objects/contacts/search?hapikey=X HTTP/1.1
Accept: application/json
Content-Type: application/json
Host: api.hubapi.com
Content-Length: 721
Expect: 100-continue
Connection: Keep-Alive

{
"filterGroups": [
{
"filters": [
{
"value": "it@iii.com",
"propertyName": "hs_additional_emails",
"operator": "CONTAINS_TOKEN"
}
]
}
],
"sorts": [],
"limit": 100
}

 

 

emails.png

0 Upvotes
1 Accepted solution

Accepted Solutions
IMykhayliv
Solution
Participant

Hello again, Teun. We looked into it further, and we were able to resolve the issue - it turns out that, in line with the API, we needed to have multiple filter groups, with 1 filter each for the necessary OR comparison, as described below:

 

https://developers.hubspot.com/docs/api/crm/search

 

"When multiple filters are provided within a filterGroup, they will be combined using a logical AND operator. When multiple filterGroups are provided, they will be combined using a logical OR operator. The system supports a maximum of three filterGroups with up to three filters each."
 
And when removing the extra filter in the 1st group in the curl example on the page, the JSON structure matches your recommended format:
 
"curl https://api.hubapi.com/crm/v3/objects/contacts/search?hapikey=YOUR_HUBSPOT_API_KEY \
--request POST \
--header "Content-Type: application/json" \
--data '{
"filterGroups":[
{
"filters": [
{
"propertyName": "firstname",
"operator": "EQ",
"value": "Alice"
}
]
},
{
"filters": [
{
"propertyName": "enum1",
"operator": "HAS_PROPERTY"
}
]
}
]
}'"
 
 
Thank you for your help, and have a great day. We can consider the matter closed.

View solution in original post

6 Replies 6
Teun
Guide | Diamond Partner

Hi @JAnderson1 ,

 

I copied the solution of this post, might help you out. I see that the code matches your code. But I tested this solution with Postman, and the described setup below should work. I could find my contact with my primary email but also with my secondary email.

Using the EQ operator when searching on the 'email' property won't return contacts based on their secondary emails; it'll only return contacts with a primary email that matches the search term.

 

What you might consider doing instead (or in combination) is using the CONTAINS_TOKEN operator and searching the 'hs_additional_emails' field. Here's an example body that should do the trick:

 

{
  "filterGroups": [
    {
      "filters": [
        {
          "value": "((email))",
          "propertyName": "hs_additional_emails",
          "operator": "CONTAINS_TOKEN"
        }
      ]
    },
    {
      "filters": [
        {
          "value": "((email))",
          "propertyName": "email",
          "operator": "EQ"
        }
      ]
    }
  ]
}

 

Did my answer solve your issue? Help the community by marking it as the solution.
JAnderson1
Participant

Hi,

 

Thanks for the code.

 

I tried the JSON, but the contact fails to return:

 

POST https://api.hubapi.com/crm/v3/objects/contacts/search?hapikey=X

 

{
"filterGroups": [
{
"filters": [
{
"value": "EMAIL",
"propertyName": "hs_additional_emails",
"operator": "CONTAINS_TOKEN"
}
]
},
{
"filters": [
{
"value": "EMAIL",
"propertyName": "email",
"operator": "EQ"
}
]
}
]
}

0 Upvotes
Teun
Guide | Diamond Partner
This is really odd. What programming language are you using? I tested the above body with Postman and was able to get the results.
Is your hapikey valid?
Did my answer solve your issue? Help the community by marking it as the solution.
0 Upvotes
IMykhayliv
Solution
Participant

Hello again, Teun. We looked into it further, and we were able to resolve the issue - it turns out that, in line with the API, we needed to have multiple filter groups, with 1 filter each for the necessary OR comparison, as described below:

 

https://developers.hubspot.com/docs/api/crm/search

 

"When multiple filters are provided within a filterGroup, they will be combined using a logical AND operator. When multiple filterGroups are provided, they will be combined using a logical OR operator. The system supports a maximum of three filterGroups with up to three filters each."
 
And when removing the extra filter in the 1st group in the curl example on the page, the JSON structure matches your recommended format:
 
"curl https://api.hubapi.com/crm/v3/objects/contacts/search?hapikey=YOUR_HUBSPOT_API_KEY \
--request POST \
--header "Content-Type: application/json" \
--data '{
"filterGroups":[
{
"filters": [
{
"propertyName": "firstname",
"operator": "EQ",
"value": "Alice"
}
]
},
{
"filters": [
{
"propertyName": "enum1",
"operator": "HAS_PROPERTY"
}
]
}
]
}'"
 
 
Thank you for your help, and have a great day. We can consider the matter closed.

View solution in original post

Teun
Guide | Diamond Partner
Awesome! Happy to help!
Did my answer solve your issue? Help the community by marking it as the solution.
Teun
Guide | Diamond Partner

Awesome, happy that you we're able to resolve it!

Did my answer solve your issue? Help the community by marking it as the solution.
0 Upvotes