Pagination - how to get most recent records

The Pagination feature goes from record 1 to the highest record. This makes it difficult to pull the most recent complete set of 100 records. Ex. Say I have a 1001 record database and want to pull records 902-1001? I can write a formula to keep updating the page number but eventually I’ll roll over to the first record in the next group of 100 and it’ll return only 1 record. I ALWAYS want the most recent 100 whether it be 898-997 or 902-1001.

Is there an API feature way to achieve this or do I have to do it with a formula and keep changing the Page number and rowsPerPage?

Hey Steve,

I would put add a new ‘date created’ field that auto-populates to the record. Then when you query the API you can filter by date and also sort by date which will give you the most recent records.

1 Like

Kelson

Thank you, and that would work, but on my previous post/topic here I stated that I cannot get the Filter feature to work using cURL (which is what I’m using). So, filtering isn’t an option. I’m stuck with the Page feature for now.

Do you know how to format the cURL URL to include filtering? You can see the example I gave in the post just before this one in the timeline.

@Steve11, you can use add sort parameters to your URL, where field_25 should be replaced with your date created field as Kelson mentioned. You can read reference the Knack API documentation here.
I also added the rows_per_page parameter too in the below example.

https://api.knack.com/v1/objects/object_1/records?sort_field=field_25&sort_order=desc&rows_per_page=100

1 Like

I feel kinda dumb, I was on the documentation page right next to this and didn’t see it. Thank you for pointing it out to me. This is what I was looking for.

1 Like

@StephenChapman
Do you know anything about using cURL? I had a similar post just before this one asking how to include filters in the cURL URL. Here’s what I tried but instead of returning the one record that it should find, it instead returns the first 25 records of the database.

Here’s what was suggested to my by Knack support via email and it doesn’t filter anything:

curl --location --globoff ‘https://api.knack.com/v1/objects/object_7/records?filters={match:or,rules:[{field:field_32,operator:contains,value:“1408”}]}’ --header ‘X-Knack-Application-Id: [MyAppId]’ --header ‘X-Knack-REST-API-KEY: [MyApiKey]’

It should look something like below:

curl -X GET 'https://api.knack.com/v1/objects/object_7/records?rows_per_page=25&sort_field=field_28&sort_order=desc&filters=[{"match":"and","rules":{"field":"field_32","operator":"is","value":"1408"}]}' 
-H 'X-Knack-Application-ID: [Your App ID]' 
-H 'X-Knack-REST-API-Key: [Your API Key]'

You can get the filter string in the Knack Builder by filtering records in your intended table (object), which you can add after the &filters=in your URL.

An alternative is to perform a view-based GET on a grid of data in one of your Knack pages.
This way, you can apply the intended sort and filters directly into the Source settings of the grid, limit the fields you want to return in your query, and you can even limit the number of records to return (e.g. top 10).
This would only be applicable if you want to return the same filters all the time, however in your case, I assume your field_32 value would be a variable.

curl -X GET 'https://api.knack.com/v1/pages/scene_xx/views/view_yy/records?rows_per_page=25
-H 'X-Knack-Application-ID: [Your App ID]' 
-H 'X-Knack-REST-API-Key: [Your API Key]'

Here’s an example of returning the top 10 sales orders within the last 12 months by descending order date.

To make it more secure, and not expose your app’s API key, I strongly suggest using a view-based GET with a user token authorization method, which you can read more about here: Remote User Logins (knack.com).

Hope that helps! I’m on holidays for the next two weeks, so happy to help after I return! :palm_tree: