API - how to order collection items in response

Hi,

I’m making an API call to get a list of items in a collection (I’m using limit and offset in the API call which will be set dynamically depending on the user who is logged in).

It’ll be something like this:

https://api.webflow.com/collections/COLLECTION_ID/items?limit=10&offset=5

This will return me the 5th to 15th items in a collection, however is there a way to select the 5th to 15th items in the collection if I order by a particular field in that collection - for example order by “Published Date” or to order by “Sorting” field? Not sure if this is possible! Any ideas would be great - how is this achieved in the front end when ordering a Collection List in the Designer?

Thanks!
Andrew

1 Like

Hi @andrewrubio

I can’t advise on the API side, but in the Designer you can order a collection list by choosing ‘limit items’ in the sort/filter and then specifying the range.

You can also look at giving the collection a # number field, and each item you want ordering a number - then in the collection list using ‘sort order’ to read from that field…

Hope that helps

According to the documentation, there are no filtering or sorting paramaters. There is only limit and offset.

http://developers.webflow.com/#get-all-items-for-a-collection

If you add a sort parameter it raises an error stating that the sort parameter must be an array.
It seems there’s an undocumented feature for sorting. I couldn’t make it work so far.

I’m actually looking for a filter feature, but sorting would be a second best that would allow me to traverse my items faster.

1 Like

Ok, now I got this:

You can use https://api.webflow.com/collections/COLLECTION_ID/items?limit=10&offset=5&sort[0]=MYFIELD&sort[1]=ASC

I know it is probably too late to help you, but it may help someone else.

3 Likes

This is weird. @Nelson_Oliveira thanks for your work figuring out how to sort. Unfortunately I wasn’t able to make DESC sorting work. But any little bit helps. At least I don’t have to return my entire blog of posts to our app to see the latest four posts. Just have to hard-code an offset that we can update. Would love any additional lessons learned on using filter, sort, and offset with the API.

I did find though that you can get the field name you want to ASC sort on from the returned items from a call to GET /collections/:collection_id/items. It will list out the actual database field name, even if you’ve renamed it.

You can use DESC sort if set “-” before name of field when you sort

https://api.webflow.com/collections/COLLECTION_ID/items?offset=0&limit=10&sort[0]=-FIELD1

If you need sort from more fields you can use:

https://api.webflow.com/collections/COLLECTION_ID/items?offset=0&limit=100&sort[0]=-FIELD1&sort[1]=-FIELD2

1 Like