I’ve got a script reading a CSV and looping through api.create_user() to spin up agents. It works fine for 50 users, but then I start getting 429 Too Many Requests. The docs mention pagination for reads, but nothing on write throttling. Should I be batching these calls or is there a specific bulk endpoint I’m missing? My current loop just sleeps for 200ms between calls, which feels brittle. Any ideas on how to handle this gracefully in the Python SDK?
The docs state: “Rate limits are applied per API endpoint and vary by method. POST requests to /api/v2/users generally have a lower throughput than GET requests.”
You’re hitting the limit because 200ms isn’t enough buffer when the server processes async tasks. The Python SDK doesn’t auto-throttle writes. You need to implement exponential backoff or use the Retry logic from urllib3.
Here’s a quick wrapper using tenacity to handle the 429s gracefully. It waits and retries with a random delay.
from tenacity import retry, wait_exponential, stop_after_attempt
from purecloud_platform_client import UsersApi
@retry(wait=wait_exponential(multiplier=1, min=4, max=10), stop=stop_after_attempt(5))
def safe_create_user(users_api, user_body):
return users_api.post_users(body=user_body)
# In your loop:
try:
safe_create_user(api, user_data)
except Exception as e:
print(f"Failed after retries: {e}")
Don’t loop blindly. The token scope doesn’t matter here, it’s the request rate.