Python SDK pagination error when exporting daily analytics to S3 via boto3

Building a daily job to pull Genesys Cloud analytics data and dump it to S3. Using the genesyscloud Python SDK to fetch the data and boto3 for the upload. The issue is in the pagination logic.

The docs for the analytics API suggest using get_analytics_details with a date range. I’m setting up a loop to handle the nextPageToken.

from genesyscloud.analytics import AnalyticsApi
import boto3

analytics_api = AnalyticsApi(configuration)
s3_client = boto3.client('s3')

start_date = '2023-10-01T00:00:00Z'
end_date = '2023-10-02T00:00:00Z'

try:
 result = analytics_api.post_analytics_details(
 body=AnalyticsDetailsRequest(
 start_date=start_date,
 end_date=end_date,
 group_by=['date'],
 metrics=['conversation_count']
 )
 )
 print(f"Got first page, items: {len(result.items)}")
 
 while result.next_page_token:
 print(f"Fetching next page: {result.next_page_token}")
 result = analytics_api.post_analytics_details(
 body=AnalyticsDetailsRequest(
 start_date=start_date,
 end_date=end_date,
 group_by=['date'],
 metrics=['conversation_count'],
 next_page_token=result.next_page_token
 )
 )
except Exception as e:
 print(f"Error: {e}")

The first call works fine. Returns 200. I get the next_page_token. But the second call throws a 400 Bad Request. The error message says Invalid next_page_token.

I’ve checked the token string. It looks like a standard base64 encoded string. Not empty. Not null.

Is the next_page_token valid only for the initial request context? Or am I missing a header in the subsequent calls? The SDK doesn’t seem to have a dedicated pagination helper for this specific endpoint like it does for users or contacts.

Also, when I try to upload the aggregated JSON to S3 using s3_client.put_object, it works. So the S3 side is fine. The problem is strictly in the GC API pagination.

Anyone hit this with the Python SDK?