Analytics API paging object logic causing infinite loop in custom reporting tool

I’m building a custom reporting tool using the Embeddable Client App SDK to fetch detailed interaction data for our supervisors. The goal is to pull every single conversation record from the last 30 days. I’m using the /api/v2/analytics/conversations/details/query endpoint.

The documentation says to use the paging object with pageSize and pageNumber. I’ve written a loop that increments pageNumber and checks if pageCount is greater than the current page. Here is the logic:

const fetchAllData = async () => {
 let pageNum = 1;
 const pageSize = 1000; // Max allowed
 
 while (pageNum <= pageCount) {
 const response = await fetch(
 `/api/v2/analytics/conversations/details/query?pageSize=${pageSize}&pageNumber=${pageNum}`,
 {
 headers: { 'Authorization': 'Bearer ' + token }
 }
 );
 const data = await response.json();
 
 // Update pageCount from response
 pageCount = data.paging.pageCount;
 
 processData(data.entities);
 pageNum++;
 }
};

The problem is that pageCount in the response sometimes changes or seems inconsistent with the actual number of pages available. If I have 2500 records and pageSize is 1000, I expect pageCount to be 3. But sometimes the API returns pageCount: 2 on the first call, then my loop stops early. Other times it returns pageCount: 10 which seems wrong for that data volume.

I’ve tried:

  • Setting pageSize to 100, 500, and 1000.
  • Checking the totalCount field instead of pageCount.
  • Adding a delay between requests to avoid rate limiting.

The totalCount seems more stable, but the docs emphasize using pageCount for pagination control. Is there a known bug with how pageCount is calculated in the Analytics API? Or am I misinterpreting the paging object structure? The response JSON looks like this:

{
 "paging": {
 "pageSize": 1000,
 "pageNumber": 1,
 "pageCount": 2,
 "totalCount": 1500
 }
}

Why would pageCount be 2 if totalCount is 1500 and pageSize is 1000? That should be 2 pages (1000 + 500). But if I go to page 2, I only get the remaining 500. Then on the next request for page 3, the API returns 204 No Content. So the loop breaks correctly, but the initial pageCount value is confusing. I want to rely on the paging object to know when to stop, not on empty responses. How should I handle this discrepancy?

The docs for the analytics query endpoint state: “The response includes a paging.nextPageToken field.” You’re using pageNumber which ignores the server’s cursor. Switch to token-based paging.

var paging = new Paging { NextPageToken = response.Paging.NextPageToken };