Going to the next page of an API call

Hi, I have an API query that is cursor-based paginated (I believe). I have hit a wall here as I simply cannot get the rest of the data to load to my table. I have tried the cursor pagination function in Retool, and I have tried several approaches in JavaScript and with the URL, but I can't get anything to work.

The Problem

I want to have a table with all 680 results of my API call, but I am only getting a limited amount of results which I can set with the limit API parameter (max 250). I can see that there is a next_page element in the API call as well, and the value at index 1 of that array is the dispute ID of my API call. Now I though I might need to sort_by the API call and set the next page to one of the values in the next_page array, however, I cannot sort_by dispute ID. (The dispute ID can be found at the dispute_krn key as the last value after the : in that string.)

API call result

If I run the API query with a limit of 1, this is the result I get:

{
  "pagination": {
    "limit": 1,
    "count": 1,
    "total": 687,
    "next_page": [
      "1678799406473",
      "205362922"
    ]
  },
  "disputes": [
    {
      "dispute_krn": "krn:klarna:eu1:dispute:return:205362922",
      "investigation_status": "closed",
      "reason": "return",
      "opened_at": "2023-03-14T13:10:06.473Z",
      "closed_at": "2023-06-22T10:36:52.206Z",
      "closing_reason": "resolved",
      "capture_id": "a1c5f37b-7009-4852-a451-f96860a04322",
      "region": "eu1",
      "order": {
        // A lot of data that is not important
      ],
      "status": "closed",
      "disputed_amount": {
        "amount": 69900,
        "currency": "SEK"
      }
    }
  ]
}

Desired situation

I want all the 680 records of this API call to end up in my table in Retool. I am guessing I will need to iterate over this API call with some JavaScript code. The problem is that I don't know what values to use for this JavaScript code, and the subsequent API calls.

Can somebody guide me in the right direction here?

Hey @nielsk!

This looks like the Klarna API, is that correct? Would you mind sharing how you currently have your query set up?

It's possible to dynamically pass values as query parameters that either directly reference the properties on your table mentioned here, or get interpolated using additionalScope to work with a script that will return all pages at once, like the one described here.

You might also try something like

const maxPages = 20;
let allResults = [];
let nextCursor;

for(let i = 0; i < maxPages; i++){
   //get current page data
   const pageData = await klarnaQuery.trigger({
      additionalScope: {
        next_page: nextCursor
      }
    });

    //add pages results to total results
    allResults = allResults.concat(pageData.disputes);

    //grab next page cursor
    nextCursor = pageData.pagination.next_page;

    //check to see if there's another page, and if not exit loop
    if(nextCursor.length === 0) break;

    /* note:
     the conditional above is just a guess on my part,
     you'll likely want to see what the api returns when 
     there are no more pages and adjust it to match that
    */

}

return allResults;

As described in this thread you may also want to add something like the following as the definition for your next_page parameter in case it needs to be omitted at the start:

Hopefully that's helpful, please let me know if you're still running into issues!