Python script for daily Genesys Analytics export to S3 timing out with 504 Gateway Timeout

Hey everyone,

I’m trying to automate a daily export of interaction analytics data from Genesys Cloud to our S3 bucket using a Python script. The goal is to pull the last 24 hours of data and push it to S3 so we can run some custom reporting that doesn’t fit into the standard dashboard widgets.

I’ve got the OAuth token handling sorted out using the genesyscloud SDK, and the S3 upload part with boto3 works fine in isolation. The issue is when I try to chain the API call to /api/v2/analytics/conversations/queues/ranges with the S3 upload. The Genesys API is returning a 504 Gateway Timeout when I request a date range larger than 1 hour. I know I need to chunk the requests, but my current loop seems to be hitting some rate limit or just timing out before the data comes back.

Here’s the relevant snippet of my code:

import boto3
import genesyscloud
from datetime import datetime, timedelta

def get_analytics_data(org_id, queue_id, start_time, end_time):
 # Assuming auth is handled elsewhere
 analytics_api = genesyscloud.AnalyticsApi()
 try:
 response = analytics_api.post_conversations_queues_ranges(
 org_id=org_id,
 body={
 "dateFrom": start_time.isoformat(),
 "dateTo": end_time.isoformat(),
 "groupBy": "conversation", # This might be too heavy?
 "queues": [queue_id]
 }
 )
 return response
 except Exception as e:
 print(f"Error: {e}")
 return None

# Chunking logic
start = datetime.utcnow() - timedelta(days=1)
end = datetime.utcnow()
chunk_size = timedelta(hours=1)

current_start = start
while current_start < end:
 current_end = min(current_start + chunk_size, end)
 data = get_analytics_data(org_id, queue_id, current_start, current_end)
 if data:
 # Upload to S3
 s3_client = boto3.client('s3')
 s3_client.put_object(Bucket='my-analytics-bucket', Key=f"data_{current_start}.json", Body=data)
 current_start += chunk_size

The script fails consistently after the first chunk. I’m wondering if I’m missing a pagination token or if the groupBy parameter is causing the backend to hang. I’ve tried reducing the chunk size to 15 minutes, but it still times out. Any ideas on how to structure this request to avoid the 504? Or is there a better endpoint for bulk exports?