Retrieve Real-Time Queue Metrics via the Genesys Cloud Statistics API
What You Will Build
- A Python script that authenticates to Genesys Cloud and retrieves real-time queue statistics, including waiting call counts and available agent counts.
- This tutorial uses the Genesys Cloud PureCloudPlatformClientV2 Python SDK.
- The implementation covers OAuth authentication, endpoint construction, and handling pagination for large organizations.
Prerequisites
- OAuth Client: A Genesys Cloud OAuth client with the
api_userorapi_gatewaygrant type. - Required Scopes:
queue:realtime:view(Required for viewing real-time queue statistics)analytics:queues:view(Often required depending on specific metric granularity, thoughqueue:realtime:viewis primary for live data)
- SDK Version:
genesys-cloud-purecloud-platform-client-v2>= 130.0.0 - Language/Environment: Python 3.8+
- Dependencies:
genesys-cloud-purecloud-platform-client-v2python-dotenv(for secure credential management)
Authentication Setup
Genesys Cloud uses OAuth 2.0 for authentication. For server-to-server integrations (like this script), you will use the Client Credentials flow. The Python SDK handles token acquisition and refresh automatically when initialized correctly.
Create a .env file in your project root to store credentials securely.
# .env
GENESYS_CLIENT_ID=your_client_id_here
GENESYS_CLIENT_SECRET=your_client_secret_here
GENESYS_REGION=us-east-1
Initialize the SDK client. The PureCloudPlatformClientV2 class manages the authentication context.
from purecloud_platform_client_v2 import PureCloudPlatformClientV2
from purecloud_platform_client_v2.api import QueueApi
from purecloud_platform_client_v2.model import QueueRealtimeQuery
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
def get_platform_client():
"""
Initializes and returns the Genesys Cloud Platform Client.
"""
client_id = os.getenv("GENESYS_CLIENT_ID")
client_secret = os.getenv("GENESYS_CLIENT_SECRET")
region = os.getenv("GENESYS_REGION", "us-east-1")
if not client_id or not client_secret:
raise EnvironmentError("GENESYS_CLIENT_ID and GENESYS_CLIENT_SECRET must be set in .env")
# Initialize the platform client
platform_client = PureCloudPlatformClientV2(
client_id=client_id,
client_secret=client_secret,
region=region
)
return platform_client
Implementation
Step 1: Constructing the Real-Time Query
The core endpoint for real-time queue data is POST /api/v2/analytics/queues/details/query. Unlike standard REST GET requests, this endpoint uses a POST body to define the query parameters. This allows for complex filtering and aggregation of real-time metrics.
The key object is QueueRealtimeQuery. You must specify the groupBy and metrics you require. For queue-specific data, grouping by queue is standard.
from purecloud_platform_client_v2.model import GroupBy, QueueRealtimeQuery
def build_queue_query():
"""
Constructs a QueueRealtimeQuery object to fetch waiting counts and agent availability.
"""
# Define the metrics you want to retrieve.
# 'waitingCount' = Number of conversations waiting in the queue.
# 'availableAgents' = Number of agents currently available to take calls.
# 'busyAgents' = Number of agents currently on a call or wrap-up.
metrics = [
"waitingCount",
"availableAgents",
"busyAgents",
"talkingAgents",
"wrapupAgents"
]
# Group by queue to get stats per queue.
# You can also group by 'skill' or 'member' if needed.
group_by = GroupBy(
group_by="queue"
)
# Create the query object
query = QueueRealtimeQuery(
group_by=group_by,
metrics=metrics
)
return query
Note on Metrics: The waitingCount metric represents the total number of interactions currently waiting in the queue. availableAgents represents agents who are logged in, not on a call, and not in wrap-up, and are eligible to receive interactions from this queue.
Step 2: Executing the API Call
Use the QueueApi class from the SDK. You need to pass the queue_api instance and the query object built in Step 1.
def fetch_realtime_queue_stats(platform_client):
"""
Fetches real-time queue statistics using the QueueApi.
"""
queue_api = QueueApi(platform_client)
query = build_queue_query()
try:
# Execute the query
# The API returns a QueueRealtimeResponse
response = queue_api.post_analytics_queues_details_query(body=query)
return response
except Exception as e:
print(f"Error fetching queue stats: {e}")
raise
Step 3: Processing Results and Handling Pagination
The API returns a QueueRealtimeResponse object. This object contains a list of QueueRealtimeEntity objects in the entities field. Each entity corresponds to a queue (since we grouped by queue).
If you have more queues than the default page size (usually 200), you must handle pagination. The response includes a nextPageUri if more data is available.
def process_queue_response(response):
"""
Parses the QueueRealtimeResponse and prints key metrics.
"""
if not response.entities:
print("No queue data returned.")
return
print(f"{'Queue ID':<30} {'Name':<25} {'Waiting':<10} {'Available':<10} {'Busy':<10}")
print("-" * 85)
for entity in response.entities:
queue_id = entity.id
queue_name = entity.name
# Extract metrics from the entity
# Metrics are stored in a dictionary-like structure within the entity
waiting_count = entity.metrics.get("waitingCount", 0)
available_agents = entity.metrics.get("availableAgents", 0)
busy_agents = entity.metrics.get("busyAgents", 0)
print(f"{queue_id:<30} {queue_name:<25} {waiting_count:<10} {available_agents:<10} {busy_agents:<10}")
# Check for pagination
if response.next_page_uri:
print("\n... Pagination detected. Use nextPageUri to fetch remaining queues.")
print(f"Next Page URI: {response.next_page_uri}")
Complete Working Example
Below is the full, copy-pasteable Python script. Save this as get_queue_stats.py. Ensure you have installed the dependencies: pip install genesys-cloud-purecloud-platform-client-v2 python-dotenv.
import os
import sys
from dotenv import load_dotenv
# Import Genesys Cloud SDK
from purecloud_platform_client_v2 import PureCloudPlatformClientV2
from purecloud_platform_client_v2.api import QueueApi
from purecloud_platform_client_v2.model import QueueRealtimeQuery, GroupBy
def load_credentials():
"""Loads credentials from .env file."""
load_dotenv()
client_id = os.getenv("GENESYS_CLIENT_ID")
client_secret = os.getenv("GENESYS_CLIENT_SECRET")
region = os.getenv("GENESYS_REGION", "us-east-1")
if not client_id or not client_secret:
raise EnvironmentError("GENESYS_CLIENT_ID and GENESYS_CLIENT_SECRET are required in .env file.")
return client_id, client_secret, region
def init_client(client_id, client_secret, region):
"""Initializes the PureCloudPlatformClientV2."""
try:
platform_client = PureCloudPlatformClientV2(
client_id=client_id,
client_secret=client_secret,
region=region
)
return platform_client
except Exception as e:
print(f"Failed to initialize client: {e}")
sys.exit(1)
def get_realtime_queue_data(platform_client):
"""
Retrieves real-time queue statistics.
Returns:
list: A list of dictionaries containing queue metrics.
"""
queue_api = QueueApi(platform_client)
# Define metrics to retrieve
metrics = [
"waitingCount",
"availableAgents",
"busyAgents",
"talkingAgents",
"wrapupAgents",
"offlineAgents"
]
# Define grouping
group_by = GroupBy(group_by="queue")
# Build query
query = QueueRealtimeQuery(
group_by=group_by,
metrics=metrics
)
try:
# Execute API call
response = queue_api.post_analytics_queues_details_query(body=query)
# Process entities
queue_data = []
if response.entities:
for entity in response.entities:
queue_info = {
"id": entity.id,
"name": entity.name,
"waitingCount": entity.metrics.get("waitingCount", 0),
"availableAgents": entity.metrics.get("availableAgents", 0),
"busyAgents": entity.metrics.get("busyAgents", 0),
"talkingAgents": entity.metrics.get("talkingAgents", 0),
"wrapupAgents": entity.metrics.get("wrapupAgents", 0),
"offlineAgents": entity.metrics.get("offlineAgents", 0)
}
queue_data.append(queue_info)
# Handle Pagination (Optional: Implement loop if nextPageUri exists)
if response.next_page_uri:
print("Warning: Results are paginated. Only first page retrieved.")
return queue_data
except Exception as e:
print(f"API Error: {e}")
raise
def main():
# 1. Load Credentials
client_id, client_secret, region = load_credentials()
# 2. Initialize Client
platform_client = init_client(client_id, client_secret, region)
# 3. Fetch Data
print("Fetching real-time queue statistics...")
queue_data = get_realtime_queue_data(platform_client)
# 4. Display Data
if not queue_data:
print("No queue data found.")
return
print(f"\n{'Queue Name':<30} {'Waiting':<10} {'Avail':<10} {'Busy':<10} {'Talking':<10} {'Wrapup':<10}")
print("-" * 90)
for q in queue_data:
print(f"{q['name']:<30} {q['waitingCount']:<10} {q['availableAgents']:<10} "
f"{q['busyAgents']:<10} {q['talkingAgents']:<10} {q['wrapupAgents']:<10}")
if __name__ == "__main__":
main()
Common Errors & Debugging
Error: 401 Unauthorized
Cause: The OAuth token is invalid, expired, or the client credentials are incorrect.
Fix:
- Verify
GENESYS_CLIENT_IDandGENESYS_CLIENT_SECRETin your.envfile match the values in the Genesys Cloud Admin Console. - Ensure the OAuth client has not been revoked.
- Check that the region in
.envmatches your organization’s region (e.g.,us-east-1,eu-west-1).
# Debugging tip: Print the error response body
try:
response = queue_api.post_analytics_queues_details_query(body=query)
except Exception as e:
if hasattr(e, 'body'):
print(f"Error Body: {e.body}")
raise
Error: 403 Forbidden
Cause: The OAuth client lacks the required scope queue:realtime:view.
Fix:
- Go to Genesys Cloud Admin Console > Security > OAuth Clients.
- Edit your client.
- Add
queue:realtime:viewto the scopes list. - Save and restart your script. Note: Scope changes may take a few minutes to propagate.
Error: 429 Too Many Requests
Cause: You have exceeded the rate limit for the Statistics API. Real-time endpoints have stricter rate limits than historical data endpoints.
Fix: Implement exponential backoff. Do not poll this endpoint faster than every 5-10 seconds unless specifically required by your use case.
import time
def fetch_with_retry(queue_api, query, max_retries=3):
for attempt in range(max_retries):
try:
return queue_api.post_analytics_queues_details_query(body=query)
except Exception as e:
if hasattr(e, 'status') and e.status == 429:
wait_time = 2 ** attempt
print(f"Rate limited. Waiting {wait_time} seconds...")
time.sleep(wait_time)
else:
raise
raise Exception("Max retries exceeded")
Error: Empty entities List
Cause: No queues match the query criteria, or no agents are logged in to any queues.
Fix:
- Ensure there are queues created in your Genesys Cloud org.
- Ensure at least one agent is logged in. Real-time statistics are only populated when there is activity or logged-in members associated with the queue.
- Verify you are querying the correct region.