Creating and Deleting Users via cURL in 3CX – Department Assignment Issue

VUSALDADASHOV

Gold Partner
Basic Certified
Joined
Dec 30, 2021
Messages
68
Reaction score
15
Hi everyone,

I'm trying to create users in 3CX via the API using cURL. The user gets created successfully, but I noticed that it is created without any department assigned.

Here’s the command I’m using:

Code:
TOKEN=$(curl -sk -X POST "https://3cx.domain.tld:5001/connect/token" -H "Content-Type: application/x-www-form-urlencoded" -d "client_id=MYCLIENTID&client_secret=MYCLIENTSECRET&grant_type=client_credentials" | jq -r .access_token)

curl -sk -X POST "https://3cx.domain.tld:5001/xapi/v1/Users" -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d @- <<EOF
{
  "Number": "$NUMBER",
  "Enable2FA": false,
  "Require2FA": false,
  "MyPhoneShowRecordings": true,
  "MyPhoneAllowDeleteRecordings": false,
  "MyPhoneHideForwardings": false,
  "MyPhonePush": true,
  "RecordCalls": true,
  "AllowOwnRecordings": true,
  "RecordExternalCallsOnly": true,
  "RecordEmailNotify": false,
  "FirstName": "$FIRST_NAME",
  "LastName": "$LAST_NAME",
  "DisplayName": "$FIRST_NAME $LAST_NAME",
  "EmailAddress": "$EMAIL",
  "Mobile": "$MOBILE",
  "WebMeetingFriendlyName": "$SAMACCOUNTIDWITHOUTDOT",
  "VMEmailOptions": "None",
  "VMPIN": "$NUMBER",
  "Language": "en",
  "SendEmailMissedCalls": false
}
EOF

I’ve attached a screenshot showing the user created without a department.

Questions:
  1. How can I assign a department (PrimaryGroupId) when creating a user via the API? When i'm adding PrimaryGroupId paramenter i get this error
    Code:
    {"error":{"code":"","message":"PrimaryGroupId:\nWARNINGS.XAPI.NOT_FOUND","details":[{"code":"","message":"WARNINGS.XAPI.NOT_FOUND","target":"PrimaryGroupId"}]}}
  2. What is the correct method to delete a user or at least disable it via the API? I tried using DELETE /Users/{id} but it didn’t work, and also Pbx.BatchDelete returned an error.

Thanks in advance!
 

Attachments

  • 3CX_EXT.png
    3CX_EXT.png
    32.7 KB · Views: 2
JSON:
"Groups": [{"GroupId": 5000, "Rights": {"RoleName": "users"}}]
You should know the group number. You can get it beforehand through another GET request.

Remember:
If you want to know how 3CX does this, use a suitable browser and use its developer tools to track the API communication between the browser and 3CX. It tells you everything, including what's needed to delete a user with Pbx.BatchDelete ...
 
  • Like
Reactions: Evolute IT
JSON:
"Groups": [{"GroupId": 5000, "Rights": {"RoleName": "users"}}]
You should know the group number. You can get it beforehand through another GET request.

Remember:
If you want to know how 3CX does this, use a suitable browser and use its developer tools to track the API communication between the browser and 3CX. It tells you everything, including what's needed to delete a user with Pbx.BatchDelete ...
I know the group number, I can GET it
it is static. 149
 
I found how to do that


Code:
vusald@VUSALD-DELL:~$ curl -sk -X PATCH "https://3cx.domain.tld:5001/xapi/v1/Users($USERID)" \
  -H "Authorization: Bearer $TOKEN" \
  -H "OData-Version: 4.0" \
  -H "Prefer: return=representation" \
  -H "Content-Type: application/json" \
  -d @- <<EOF
{
  "Id": $USERID,
  "Groups": [
    {
      "@odata.type": "#Pbx.UserGroup",
      "GroupId": $GROUPID,
      "Rights": { "RoleName": "users" }
    }
  ]
}
EOF
vusald@VUSALD-DELL:~$ curl -sk "https://3cx.domain.tld:5001/xapi/v1/Users($USERID)?\$select=Id,Number&\$expand=Groups(\$select=GroupId,Rights)" \
  -H "Authorization: Bearer $TOKEN" | jq .
{
  "@odata.context": "https://3cx.domain.tld:5001/xapi/v1/$metadata#Users(Id,Number,Groups(GroupId,Rights))/$entity",
  "Number": "571",
  "Id": 307,
  "Groups": [
    {
      "GroupId": 149
    }
  ]
}


Now will this change in next updates ?)
 
and this one removes

Code:
curl -sk -X DELETE "https://3cxx.domain.tld:5001/xapi/v1/Users($USERID)" -H "Authorization: Bearer $TOKEN" -H "OData-Version: 4.0" -w "\nHTTP %{http_code}\n"
 

Members Online Now

No members online now.

Forum statistics

Threads
111,843
Messages
589,327
Members
164,679
Latest member
SamadMYK