APIs & Integrations

avyer
Participant | Platinum Partner
Participant | Platinum Partner

CRM Search API v3 sorting

I'm exploring search v3 api and I'm using an OR filter for email and name, all is well but I was just wondering if there's a filter or sort for my preferred search results.

 

 

{
  "filterGroups": [
    {
      "filters": [
        {
          "propertyName": "email",
          "operator": "EQ",
          "value": "mhand@bxala.com"
        }
      ]
    },
    {
      "filters": [
        {
          "propertyName": "firstname",
          "operator": "EQ",
          "value": "larry"
        },
        {
          "propertyName": "lastname",
          "operator": "EQ",
          "value": "masi"
        }
      ]
    }
  ]
}

 

For example, if my search would return a result from email, and another for the firstname and lastname, can I sort the order of results being the one matching the email first?

{
  "total": 2,
  "results": [
    {
      "id": "29335701",
      "properties": {
        "createdate": "2020-04-22T19:31:29.675Z",
        "email": "larry@masimarketing.com",
        "firstname": "Larry",
        "hs_object_id": "29335701",
        "lastmodifieddate": "2020-04-22T20:05:31.543Z",
        "lastname": "Masi"
      },
      "createdAt": "2020-04-22T19:31:29.675Z",
      "updatedAt": "2020-04-22T20:05:31.543Z",
      "archived": false
    },
    {
      "id": "25784751",
      "properties": {
        "createdate": "2020-01-13T17:41:05.905Z",
        "email": "mhand@bxala.com",
        "firstname": "Michael",
        "hs_object_id": "25784751",
        "lastmodifieddate": "2020-04-22T21:57:46.739Z",
        "lastname": "Hand"
      },
      "createdAt": "2020-01-13T17:41:05.905Z",
      "updatedAt": "2020-04-22T21:57:46.739Z",
      "archived": false
    }
  ]
}

 

0 Upvotes
3 Replies 3
WendyGoh
HubSpot Employee
HubSpot Employee

CRM Search API v3 sorting

Hey @avyer,

 

When looking to sort the return results, you can use the sorting rule stated on this documentation: CRM API | Search:

 

curl https://api.hubapi.com/crm/v3/objects/contacts/search?hapikey=YOUR_HUBSPOT_API_KEY \
  --request POST \
  --header "Content-Type: application/json" \
  --data '{
    "sorts": [
      {
        "propertyName": "createdate",
        "direction": "DESCENDING"
      }
    ]
  }'

In your case, the propertyName would be email. 

avyer
Participant | Platinum Partner
Participant | Platinum Partner

CRM Search API v3 sorting

This wouldn't work for me as this would only sort email property alphabetically. In my case, If the the search would match an email, it would be on top of the results along with the other matches from other properties. Is this possible?

0 Upvotes
WendyGoh
HubSpot Employee
HubSpot Employee

CRM Search API v3 sorting

Hey @avyer,

 

To clarify, do you mean that you'd like to sort by email and other properties and if the contact has email you'd like it to be populated first followed by the other properties that you're sorting? 

 

If so, one suggestion that I have is to use the search and sort function together.

 

First you search for contacts that only contains email and you sort is by email and the other properties. This way, the results will only return contact with emails. Is this something that you're looking for? 

0 Upvotes