Trying to bulk-create users from a CSV using the Python SDK but it’s timing out. Here’s the snippet:
for row in reader:
body = models.PostUsersBody(name=row['name'], email=row['email'])
client.users_api.post_users(body=body)
Getting a 504 Gateway Timeout after 50 rows. Should I be batching these or is there a specific bulk endpoint I’m missing?
Cause: The Python SDK isn’t designed for firehose-style API calls. You’re hammering the post_users endpoint sequentially without any backoff or concurrency handling, which triggers Genesys rate limits and eventually the 504 timeout from the gateway. The SDK’s default timeout is too aggressive for bulk operations, and you’re not giving the backend time to process each creation before slamming it with the next one.
Solution: Don’t loop naively. Use the concurrent.futures library to parallelize requests with a controlled worker pool, and wrap each call in a retry logic. Here’s a pattern that works well for bulk user creation:
from purecloudplatformclientv2 import UsersApi, PostUsersBody, Configuration
from purecloudplatformclientv2.rest import ApiException
import concurrent.futures
import time
def create_user(client, name, email):
"""Create a single user with retry logic."""
body = PostUsersBody(name=name, email=email)
retries = 3
for attempt in range(retries):
try:
response = client.users_api.post_users(body=body)
return response
except ApiException as e:
if e.status == 429: # Rate limit
time.sleep(2 ** attempt) # Exponential backoff
continue
raise e
def bulk_create_users(client, csv_data, max_workers=5):
"""Parallelize user creation."""
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
# Map each row to the create_user function
futures = {
executor.submit(create_user, client, row['name'], row['email']): row
for row in csv_data
}
for future in concurrent.futures.as_completed(futures):
row = futures[future]
try:
result = future.result()
print(f"Created user: {row['name']}")
except Exception as exc:
print(f"User {row['name']} generated an exception: {exc}")
# Usage
# csv_data = [...] # Your parsed CSV list of dicts
# bulk_create_users(platform_client, csv_data)
Keep the max_workers low (5-10) to avoid tripping the org-level API rate limits. If you’re creating hundreds of users, consider breaking the CSV into chunks and running this script in stages. Also, make sure your OAuth token has the user:write scope.