Genesys Cloud Python SDK: Handling token refresh automatically vs manual refresh

We are instrumenting our data pipeline with New Relic and need to call the Genesys Cloud API consistently for analytics. Currently, we are using the Python SDK (genesyscloud). The setup works fine initially, but the access token expires after the standard duration, causing subsequent API calls to fail with a 401 Unauthorized error. We have to manually call client.refresh_token() and re-initialize the client, which is messy in an automated loop.

Is there a built-in mechanism in the SDK to handle token refresh automatically? We want to avoid writing custom logic to catch the 401 and retry. Here is our current initialization block:

from genesyscloud import Configuration, ApiClient

configuration = Configuration()
configuration.access_token = initial_token
api_client = ApiClient(configuration)

We are seeing the error on the second batch of requests. We need a cleaner way to keep the session alive.

The genesyscloud Python SDK handles refresh automatically if you use AuthCode or ClientCredentials with persistence enabled. You don’t need to call refresh_token() manually.

from genesyscloud.auth import AuthCode
from genesyscloud.platformclient import configuration

config = configuration.Configuration()
config.host = 'https://api.mypurecloud.com'
auth = AuthCode(config=config, client_id='...', client_secret='...', scope='...', redirect_uri='...')
# Token refresh happens under the hood on 401