Bulk User Provisioning - 400

It’s like building a house of cards - everything looks right until one tiny thing throws the whole thing off. We’ve got a PowerShell script for bulk user creation using the /api/v2/users endpoint and it’s started failing with a 400 Bad Request on the phoneNumber field. We’ve got around 250 agents, so manually creating them is, well, doing jack all.

The script’s been running fine for months, but it’s now hitting this issue consistently. The documentation says phoneNumber should be E.164 format, which we are sending. It’s not a token issue - we’ve confirmed token refresh works.

Here’s the relevant bit of the script - stripped down for clarity:

$uri = "https://api-us-west-2.genesyscloud.com/api/v2/users"
$body = @{
 "username" = "test.user123"
 "firstName" = "Test"
 "lastName" = "User"
 "email" = "test.user123@example.com"
 "phoneNumber" = "+15551234567" #E.164 format
} | ConvertTo-Json
$headers = @{
 "Authorization" = "Bearer $env:GC_TOKEN"
 "Content-Type" = "application/json"
}

Invoke-RestMethod -Uri $uri -Method POST -Headers $headers -Body $body

The API response just gives a generic “Invalid request” and a 400. Nothing else. We’re on Genesys Cloud release 2024-08. I’ve checked the user’s default country code, thinking maybe something’s off there, but the default country code is set correctly. The console is empty.

N.

2 Likes

Fun one today. Think of the Genesys Cloud user object as a form - every field has a rule, and the API is a strict grader. A 400 on phoneNumber usually means the format is wrong, or the length isn’t what the API expects. It’s rarely a data issue - more often, it’s how you’re telling the API the data.

We’ve seen this before - the API’s picky about phone numbers. It needs E.164 format - that’s country code + number, no spaces or dashes. A US number would look like +15551234567. PowerShell can mangle that formatting easily.

Here’s a quick snippet to sanitize the phone number before sending it. It checks length, adds the country code if missing, and strips non-numeric characters.

function Sanitize-PhoneNumber {
 param (
 [string]$phoneNumber
 )

 $phoneNumber = $phoneNumber -replace '[^0-9]' # Remove all non-numeric characters
 if ($phoneNumber.Length -lt 10) {
 $phoneNumber = "+1" + $phoneNumber # Assume US if short
 }
 return $phoneNumber
}

# Example usage
$userPhoneNumber = "+1 (555) 123-4567"
$sanitizedNumber = Sanitize-PhoneNumber -phoneNumber $userPhoneNumber
Write-Host "Original: $userPhoneNumber"
Write-Host "Sanitized: $sanitizedNumber"

Make sure you’re passing $sanitizedNumber to the /api/v2/users endpoint. It’s a small change, but the API won’t budge on format.