I have a script to bulk-create users from a CSV file using the Genesys Cloud Python SDK. The authentication part works fine. I can get the access token without issues. The problem is when I loop through the CSV rows and call the create_user method. It works for the first few users. Then it stops. The API returns a 400 Bad Request. The error message says ‘Role not found’. I am passing the role IDs directly from the CSV. These IDs are valid. I verified them in the UI. The code looks like this:
from genesyscloud import authentication_api, user_management_api
api_instance = user_management_api.UserManagementApi(configuration)
for row in csv_reader:
body = {
"name": row["name"],
"email": row["email"],
"roles": [{"id": row["role_id"]}]
}
try:
api_instance.post_users(body=body)
except Exception as e:
print(e)
The error happens on the 10th iteration. I am using the same credentials. Maybe there is a rate limit? Or is the token expiring? I have tried adding a sleep timer. It did not help. The error persists. I need to know if the Python SDK handles pagination or batching differently. Or if I need to refresh the token inside the loop. The documentation is not clear on this. I am stuck.
Studio doesn’t have a native “bulk create” action for users, so if you’re trying to orchestrate this inside a script, you’re fighting the tool. But since you mentioned Python SDK, let’s stick to that. The “Role not found” error on valid IDs usually means the API call is hitting a different organization or the role IDs are stale. More likely, you’re hitting rate limits and the error is misleading, or the JSON payload structure is off for the roles array.
Check your create_user payload. The roles field expects an array of objects, not just strings. If you’re passing ["role_id_1"], it fails. It needs [{ "id": "role_id_1" }].
Here’s a quick snippet to verify the structure before sending:
from purecloudplatformclientv2 import UserPost, RoleRef
# Assuming user_data is a row from your CSV
roles_list = []
for role_id in user_data['role_ids'].split(','):
roles_list.append(RoleRef(id=role_id.strip()))
user_body = UserPost(
name=user_data['name'],
email=user_data['email'],
roles=roles_list
)
try:
api_instance.create_user(body=user_body)
print(f"Created {user_data['name']}")
except Exception as e:
print(f"Failed: {e}")
Also, add a small sleep between requests. Genesys Cloud throttles aggressive bulk operations. If you’re hammering it, it’ll drop requests with vague errors. Use time.sleep(0.5) in your loop.
If you’re still stuck, dump the raw JSON you’re sending. It’s almost always a format issue. The SDK helps, but it doesn’t catch bad role references if they’re scoped to a different org. Double-check your environment variable for the base URL too. Wrong environment, wrong roles.