How to use pagination?

hi, i am using some javascript code to return the list of people i am following from https://api.twitch.tv/helix/users/follows?from_id=myid , so far all ok , i receive the response , i parse the json , i follow 23 channels so that answer is correct , now as a first step i am trying to just list all users id ( will convert to names later once i have this working )

the relevant part of what i have so far is

respuesta = JSON.parse(twitch_following_api.responseText);

         for (var i = 0; i < respuesta.data.length; i++) {
                                var sigo = respuesta.data[i];
                                statusHTML +=  '<li>' + 'Following ' + i +  sigo.to_id +  ' followed ' + sigo.followed_at + '</li>';
                      }

Screenshot_2

so i found that lists only the default first 20 users in the array as shown in the screenshot
to get all the others as in Reference | Twitch Developers i need to use pagination , thing i never had to use before so i can’t see how to use the “after” and “first” parameters , and the “pagination” response ( if i believe those are the only ones i need to add )
how would use that to show all the users i follow in a list ?

thanks in advance

Well for you as you only follow 23 channels you don’t actually have to use pagination, you can set first to 100 (the maximum) and you’ll get the first 100 results, so will get all of your follows in a single request.

For requests that have more than 100 items though, if you look at the example on the right for Get User Follows you’ll see that as well as a data array of all the follows, there’s also "pagination":{ "cursor": "eyJiIjpudWxsLCJhIjoiMTUwMzQ0MTc3NjQyNDQyMjAwMCJ9" }. To use this cursor, simply make the same API request except you’ll use that cursor value and send it as the after parameter, and then you’ll get all the results after that cursor.

If there are still more pages, the results will contain a new cursor to use and keep repeating the process until there isn’t a new cursor, at which point you should have all results.

yeah, making changes here and there but still can’t have this thing to work, most of the time what happens is that never stops filing the list , even if i use my name when i follow less than 100, i think i need other tip here

this is basically what i do, would need to know if any of these concepts are wrong

my first request is
https://api.twitch.tv/helix/users/follows?from_id=' + localStorage.getItem("usernameID") + '&first=100'

i use 100 that is the max so later i can have the rest with the minimum amount of requests
i get “usernameID” from an earlier request

i have the response in a respuesta2 variable
respuesta2 = JSON.parse(following_req.responseText) ;
i list the first items, no issue so far

then at the end of this first request
paginacion = respuesta2.pagination.cursor ;
i define this new variable paginacion outside of the on button click event function and have all my requests inside, so that variable should be global and exists for the 2 requests, i think
from what i see in the example response the cursor seems to be the one i need ? ( doubt 1 )

then i have the second request in this loop
while ( paginacion != null )
( doubt 2 ) , is that what i should check, that the X.pagnation.cursor is not empty or i have to check that in other way ? )
my second request is

'https://api.twitch.tv/helix/users/follows?from_id=' + localStorage.getItem("usernameID")  + '&after=' + paginacion 

this time not using the “first” parameter but the “after” one with the cursor

so that it is, from what i understand of this pagination thing ( new to me so is not much ) this should be the way, but still it is not working like should …

i added this text and a counter at the start of the line to testing purposes , the ones with (part 1) is from the first request with the first 100 , then the ones with (part 2) from the second request with the after parameter
here after the 23 ( the ones i follow ) starts again from the number 1 , and stops at the 250) , kind of weird and random number to stop ? don’t know what to change anymore , any idea ?

the api returns the total number of items, which you can use to keep track of your count.

var currentCount = 0;
var cursor = ''

do {
    // send request
    for (var i = 0; i < response.data.length; i++) {
         // do work
         currentCount += 1;
         cursor = response. pagination.cursor;
    }
while (currentCount < response.total)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.