Best way to setup this rest api with paging

Hi All,

I have been reading this forum and the documentation and tried a couple of options but cannot seem to get it to work, also wondering what the best approach is.

So I have a rest api with below fields that can be used to get the whole dataset

current_page 1
first_page_url X
from 1
last_page 5
last_page_url X
next_page_url X
per_page 100
prev_page_url null
to 100
total 410

For example I followed this example but I got stuck on how to set page in the url parameters,that's ony the option I have, change the page through the url parameters

I think the best option would be to check the last_page and then current page and then +1 the url parameters page field and then loop until the last_page number is reached.

Or like the above example is to loop until the next_page_url is null
in that case I only need to learn how to do the +1 on the page url field

Hope I am a bit clear, I am still learning :slight_smile:

Hey @Sander!

Do you have any docs on the API you're hitting?

On first glance, it sounds like you'd want to replace queryResult.pagination_token with queryResult.next_page_url (you might also want to change the variable name it's stored in accordingly :sweat_smile: ). Then, since it's getting passed using additionalScope you should be able to include it in the URL parameters as is done in the example.

It also looks like you have the total number of pages though which means that you might not necessarily have to do cursor-based pagination. If just changing current_page works you might try something like:

const pageLimit = 100; //to avoid infinite loops!
let page = 1;
let allData = [];

for(page; page < pageLimit; page++){
  //fetch data
  const currentPageData = await bulkUpdateQuery.trigger({additionalScope: {page}});

  //append recently fetched data
  allData = [...allData, ...currentPageData.data];

  //check if this is the last page and exit the loop if so
  if(page === currentPageData.last_page) break;
}

if(page === pageLimit) console.log("Page limit reached");

return allData;

OMG @Kabirdas you rock, the second option is working perfectly. Going to read it a few times to actually understand it but the result is awesome. I love the retool team

Gald it worked! :grin: