Platform SDK JS pagination loop hanging on queue fetch across divisions

We’re building a custom agent desktop widget using the Embeddable Client App SDK and need to populate a dropdown with all queues across multiple divisions. I’m using the queuesApi from @genesyscloud/flow-connector or the standard platform SDK, but the pagination logic seems to hang when crossing division boundaries. I have a recursive function that calls getQueues with pageSize=100 and checks nextPageToken. It works fine for the first division, but when the token points to the next division, the promise resolves with an empty array instead of the next set of data. The code looks like this:

async function getAllQueues(apiInstance) {
 let allQueues = [];
 let nextPageToken = null;
 do {
 const response = await apiInstance.getQueues({
 pageSize: 100,
 nextPageToken: nextPageToken
 });
 if (response.entities) {
 allQueues = [...allQueues, ...response.entities];
 nextPageToken = response.nextPageToken;
 }
 } while (nextPageToken);
 return allQueues;
}

The issue is that response.nextPageToken becomes null prematurely, even though we know there are more queues in the eu-west-1 division. I’ve verified the OAuth token has the read:queue scope and read:division permissions. The network tab shows the last request returning a 200 OK with an empty entities array, which breaks the loop. I’m wondering if the SDK handles cross-division pagination differently than the raw REST API, or if I’m missing a specific parameter to force fetching all divisions. I’ve tried adding divisionId in the query, but that limits it to one division, which defeats the purpose. We need a reliable way to fetch every queue regardless of division without hitting the API limit. The current approach feels fragile and I can’t find documentation on how the SDK handles this specific multi-division edge case. Any ideas on why the token expires early or if there’s a better method?

You’re probably hitting a division boundary without refreshing the access token or passing the correct divisionId context in the next request. The SDK’s getQueues method defaults to the user’s home division, so if you’re trying to fetch across multiple divisions, you need to explicitly set the divisionId parameter for each call. Also, check your retry logic. If the API returns a 429 or a transient 5xx, your recursive function might be stuck in a tight loop without a backoff strategy.

Here’s a safer pattern using async/await and proper division handling. Make sure you have the queue:read scope. This approach fetches all queues for a specific division and handles pagination cleanly without recursion depth issues.

const getAllQueuesForDivision = async (platformClient, divisionId) => {
 let allQueues = [];
 let nextPageToken = null;
 do {
 const response = await platformClient.QueuesApi.getQueues(
 100, // pageSize
 null, // expand
 false, // includeCount
 nextPageToken, // nextPageToken
 null, // sortBy
 null, // sortOrder
 divisionId // divisionId
 );
 allQueues = allQueues.concat(response.entities);
 nextPageToken = response.nextPageToken;
 } while (nextPageToken);
 return allQueues;
};

// Usage: Loop through your known division IDs
const divisions = ['12345', '67890'];
const allQueues = await Promise.all(divisions.map(divId => getAllQueuesForDivision(platformClient, divId)));
console.log(allQueues.flat());

You’re overcomplicating this by manually handling nextPageToken. Just let the SDK handle the iteration for you.

const queues = await queuesApi.queuesQueueGet({ pageSize: 100, divisionId: 'your_division_id' });

This returns an async iterator. Just loop through it and you’re good.