3CX V20 API

pedrotms

Platinum Partner
Joined
Mar 5, 2024
Messages
19
Reaction score
11
Guys, I'm trying to access the v20 API. I have already enabled it on the panel and noted the token. I didn't quite understand what the URL structure is, but I couldn't get any feedback from the API. https://{{3CXFQDN}}/webapi/{{AccessKey}}/
 
https://{3CXFQDN}/xapi/v1/swagger.yaml
This will let you see all available endpoints, parameters, and returned values.

I'll outline the following code in python that I use to authenticate and access resources but you should be able to extrapolate to preferred programming language.






https://{3CXFQDN}/#/office/integrations/api
If you created a token using the above you will then need to call the "connect/token" endpoint and pass the client_id and client_secret to get an Authorization code.

If you don't have an enterprise license you won't be able to do the above but you can achieve the same general functionality by creating an extension with system owner (or appropriate permission level) and then calling the api endpoint to login using the username/password to get a bearer token, and then save the associated cookies. I will look for that code and add later once I find it




def GET_AppToken(fqdn, client_id, client_secret):
global access_token, refresh_token
url = fqdn+'connect/token'
headers={'Content-Type':'application/x-www-form-urlencoded'}
body = {'client_id': client_id,'client_secret':client_secret,'grant_type':'client_credentials'}
response =requests.post(url, data = body, headers= headers)

if(response.status_code ==200):
response_data = response.json()
access_token = response_data["access_token"]
return True
else:
return False



def GET_SystemStatus(fqdn,system_id):
global access_token
print("System Status: Starting")
url = f'{fqdn}xapi/v1/SystemStatus'
headers = {'Authorization': f'Bearer {access_token}'}
try:
response = requests.get(url, headers = headers)
if response.status_code != 200:
print("Response Content:", response.content.decode('utf-8')) # Decode response content
if response.headers.get('Content-Type', '').startswith('application/json'):
print("Response JSON:", response.json())
else:
response_data = response.json()



client_id = "client_id"
client_secret = "client_secret"
fqdn = "https://{fqdn}"


if GET_AppToken(fqdn,client_id,client_secret) ==True:
GET_SystemStatus(fqdn,system_id)
 
  • Like
Reactions: dkrilcic
Here is the bit to log in using extension credentials, bear in mind the endpoints/permissions are limited to what the associated user has access to. Also note this isn't the full script just the associated portions to initial login and then proceeding from there


import requests

def login(fqdn: str, username: str, password: str, securityCode: str):
global access_token, refresh_token, cookie, authResult
url = f"https://{fqdn}/webclient/api/Login/GetAccessToken"
data = {"Username": username, "Password": password, "SecurityCode" : securityCode}
headers = {'Content-type': 'application/json', 'Ngsw-Bypass':'bypass'}
tempResults = {}
try:
response = requests.post(url, data = json.dumps(data), headers= headers)
cookie = response.cookies.get_dict()
cookie = cookie["RefreshTokenCookie"]
response_data = response.json()
return response_data
except requests.RequestException as e:
return {e}




results_login = login(results[2],results[0], results[1], "")
access_token = results_login.get("Token", {}).get("access_token")
refresh_token = results_login.get("Token", {}).get("refresh_token")
 
  • Like
Reactions: dkrilcic
  • Like
Reactions: dkrilcic
Throwing in some more PowerShell examples and explanations here:

https://github.com/luxzg/3CX-XAPI_examples

@Colby D. - great examples up there, thanks in everyone's name for sharing!

@fxbastler - likewise, great samples, too bad I never thought about looking at 3cx.de when searching weeks ago, sheesh!

Linked this thread in my readme as well, so people can find good examples more easily
 
  • Like
Reactions: NatalyS_3CX
thank you

When I find problems here, from time to time I think about it and link them to the German forum. I usually write there.
I also link to the international forum in the German forum if there are solutions to problems.
It's just not that easy and I don't want to always write everything twice.
 
  • Like
Reactions: dkrilcic
a quick update to add the properly indented version

Python:
def login(fqdn: str, username: str, password: str, securityCode: str):
    global access_token, refresh_token, cookie, authResult
    url = f"https://{fqdn}/webclient/api/Login/GetAccessToken"
    data = {"Username": username, "Password": password, "SecurityCode" : securityCode}
    headers = {'Content-type': 'application/json', 'Ngsw-Bypass':'bypass'}
    tempResults = {}
    try:
        response = requests.post(url, data = json.dumps(data), headers= headers)
        cookie = response.cookies.get_dict()
        cookie = cookie["RefreshTokenCookie"]
        response_data = response.json()
        return response_data
    except requests.RequestException as e:
    return {e}




results_login = login(results[2],results[0], results[1], "")
access_token = results_login.get("Token", {}).get("access_token")
refresh_token = results_login.get("Token", {}).get("refresh_token")
 
  • Like
Reactions: dkrilcic
Why bother with powershell if a couple of wget / curl does the trick
Code:
URL="your_fqdn"
USER="your_username"
PASS="your_password"

# Generate the token; The "jq" command is to extract the token

TOKEN=$(curl -s  -X POST "$URL/webclient/api/Login/GetAccessToken" -H "Content-Type: application/json" -d "{\"SecurityCode\": \"\", \"Password\": \"$PASS\", \"Username\": \"$USER\"}
" | jq -r '.Token.access_token')

#With the token, get the big json that contains all the information about the system
RESULT=$(wget --quiet --method=GET --header="Authorization: Bearer $TOKEN" --output-document=- "$URL/xapi/v1/SystemStatus")
 

Forum statistics

Threads
111,977
Messages
590,094
Members
164,906
Latest member
Nari