APIs & Integrations

thethlaing
Member

Get all contacts API order by addedAt

SOLVE

I’m trying to get all the contacts and I used Get all contacts API. Everything is fine except I cannot find an option to get the latest data first (order by addedAt). Please help!!!

0 Upvotes
1 Accepted solution
Dadams
Solution
HubSpot Employee
HubSpot Employee

Get all contacts API order by addedAt

SOLVE

Hi @thethlaing

@jerome.kwek is correct in that the Contacts API doesn’t support sorting, but we do have an endpoint that you can use to get contacts ordered by when they were created:
GET /contacts/v1/lists/all/contacts/recent
For example, this will get you the most recently created contacts from the demo portal:
https://api.hubapi.com/contacts/v1/lists/all/contacts/recent?hapikey=demo

This endpoint follows the same rules as getting recently updated contacts, so you’ll need to use the 1vid-offsetandtime-offset` to page through the records.
http://developers.hubspot.com/docs/faq/how-do-i-use-vidoffset-and-timeoffset

View solution in original post

2 Replies 2
Dadams
Solution
HubSpot Employee
HubSpot Employee

Get all contacts API order by addedAt

SOLVE

Hi @thethlaing

@jerome.kwek is correct in that the Contacts API doesn’t support sorting, but we do have an endpoint that you can use to get contacts ordered by when they were created:
GET /contacts/v1/lists/all/contacts/recent
For example, this will get you the most recently created contacts from the demo portal:
https://api.hubapi.com/contacts/v1/lists/all/contacts/recent?hapikey=demo

This endpoint follows the same rules as getting recently updated contacts, so you’ll need to use the 1vid-offsetandtime-offset` to page through the records.
http://developers.hubspot.com/docs/faq/how-do-i-use-vidoffset-and-timeoffset

jerome_kwek
Member

Get all contacts API order by addedAt

SOLVE

@thethlaing Not sure if the API itself comes with the ability to sort.

Otherwise, perhaps you can deal with it at code level. What I’d do is:

  1. Decode JSON (to array)
  2. Do sorting on the array based on key (addedAt); You can do custom sorting with PHP on datetime (http://stackoverflow.com/questions/8121241/sort-array-based-on-the-datetime-in-php)

Hope it helps!

0 Upvotes