Python Platform SDK: Handling token refresh automatically vs manual refresh

I am writing a Python script using the Genesys Cloud Platform SDK to pull interaction data. The script runs every 15 minutes via a cron job. Right now I am manually calling client.refresh() before every API call to make sure the token is valid. It works but it feels wrong. The documentation mentions that the SDK should handle token refresh automatically when the access token expires. I tried removing the manual refresh call and just initializing the client once at the start of the script. The first call works fine. But when the cron job runs again 15 minutes later, the API call fails with a 401 Unauthorized error. I am assuming the cached token expired and the SDK did not refresh it automatically. Here is my current setup code:

from purecloud_platform_client import Configuration, ApiClient, PlatformApi

config = Configuration()
config.access_token = 'my_static_token'
client = ApiClient(configuration=config)
interaction_api = PlatformApi(client)

# Later in the script...
try:
 data = interaction_api.get_interactions_search_search(interaction_search_request=body)
except Exception as e:
 print(f"Error: {e}")

I read somewhere that the SDK uses a refresh token internally if provided during initialization. But I am only passing a static access token because I am using a service account. Do I need to pass the refresh token explicitly? Or is there a different way to configure the Configuration object so it handles the expiry without me calling refresh() manually? I want to avoid the overhead of refreshing on every single call if the token is still valid. Any code examples would be helpful.

You don’t need to manually call refresh() on every request. The Genesys Cloud Python SDK has built-in token management that handles refreshes automatically when an access token expires. It’s designed to be stateless between calls, so you just initialize the client once and let the underlying logic take care of the rest.

If you’re running a long-lived script or a cron job that stays in memory, the SDK’s internal cache might hold onto an expired token if it doesn’t detect the 401 immediately. The cleanest way to handle this is to catch the ApiException with a 401 status code and trigger a refresh only then, or just rely on the SDK’s default behavior if you’re restarting the process frequently.

Here’s how you’d wrap a call to handle the edge case where the token is stale:

from genesyscloud.platform_client import PlatformClient
from genesyscloud.api_exception import ApiException

client = PlatformClient()
client.auth_client.authenticate()

try:
 # Your API call here
 response = client.conversations_api.get_conversations_conversation(conversation_id="your-id")
except ApiException as e:
 if e.status == 401:
 client.auth_client.refresh_access_token()
 # Retry your call here

Most of the time, just removing the manual refresh works fine. The SDK checks the expiry time before sending the request. If it’s close to expiring, it refreshes in the background. You’ll save a lot of latency by not forcing a refresh every 15 minutes.

The SDK handles refreshes automatically, but keep in mind that if you’re running multiple threads, you need a single shared platformClient instance. Creating a new client per thread will bypass the refresh logic and cause race conditions.

The SDK definitely handles the refresh loop for you, so those manual calls are just adding latency. Just instantiate the client once at module level and let it ride.

from purecloudplatformclientv2 import Configuration, ApiClient, RoutingApi

config = Configuration()
config.host = "https://api.mypurecloud.com"
config.access_token = "your_token"

# Singleton instance handles its own refresh logic
api_client = ApiClient(configuration=config)
routing_api = RoutingApi(api_client)

# No refresh() needed here. It auto-refreshes on 401.
queues = routing_api.get_routing_queues()