Jump to content
  • 0

API Pagination




So i've got an API call working (table\records), but i'm wondering from the results, how do you know how many pages I need to loop through to get all the results from a table?  currenlty i've got 950 rows so this could change to over 1000 quickly, just wanted to know how best to handle it.  

Any suggestions? 

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

If you get a full page of results, there is most likely more records to get.

var qPageNumber = 1;


function callApi() {
   const qXhr = new XMLHttpRequest;
   const qMethod = 'GET';
   const qPath = 'https://acctid.caspio.com/rest/v2/tables';
   const qTable = 'table_name';
   const qSelect = '*';
   const qPageSize = 1000;
   let qUrl = qPath + '/' + qTable + '/records?q.select=' + qSelect + '&q.pageNumber=' + qPageNumber + '&q.pageSize=' + qPageSize;
   qXhr.open(qMethod, qUrl);
   qXhr.setRequestHeader('Authorization', 'bearer ' + caspioToken);
   qXhr.setRequestHeader("Content-Type", "application/json");
   qXhr.onload = function() {
      const count = JSON.parse(qXhr.responseText).Result.length;
      if(count == 1000) { // if you get a full page of results, there are "very likely" more records
         console.log('a full page of records returned - get the next page');
         qPageNumber = qPageNumber + 1;
      } else {
         console.log('less than a full page of records returned - no more pages');
         // do something with all the records

I hope this helps

Link to comment
Share on other sites

  • 0

In python it is possible to do this with a "while loop":

#start with an empty list
all_records_list = []

# while there is at least one record on a page, get each page and append it to the all_records_list
while len(records_on_page) > 1:
  url = 'https://***.caspio.com/rest/v2/tables/***/records?q.pageSize=1000&q.pageNumber='+ str(i)
  response = requests.get(url,headers=headers)
  response_dictionary = response.json()
  records_on_page = response_dictionary['Result']
  # for clarity, print what page number we are on and how many records we have so far
  print("Page number "+ str(i))
  i += 1

Unfortunately Python is my strongest language, so I can only offer help in that regard. But I am sure there is a way to do the same in Javascript with a loop also!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...