How to use 3CX API?

uniscanadmin

Customer
Joined
Mar 18, 2019
Messages
48
Reaction score
6
Hi

I was able to get access to it, but what should I do with it next, or rather what parameters should I set where?

In the specified documentation (https://www.3cx.com/docs/configuration-rest-api/), there is no sample code on how to use the received access token. Tell me, for example, how to get a list of subscribers?
 
Ok, I solved the problem myself. I'll leave the code here for future generations.

Python:
import requests
import json

# Set the data for the request
url = 'https://3cx.server.com/connect/token'  # change to the desired URL

# Create the payload for the request
payload = {
    'client_id': 'adminAPI',
    'client_secret': 'adsfadesfrfewadgfsadgfsdgfasdfas',
    'grant_type': 'client_credentials'
}

# Send a POST request
response = requests.post(url, data=payload)

# Check the response
if response.status_code == 200:
    json_string = response.json()  # Get the response as a JSON object
    access_token = json_string['access_token']  # Extract the access_token

    url_QuickTest = 'https://3cx.server.com/xapi/v1/Defs?$select=Id'
    headers = {
        'Authorization': f'Bearer {access_token}'  # Set the Authorization header with the access token
    }

    # Send a GET request
    response = requests.get(url_QuickTest, headers=headers)

    # Check the response
    if response.status_code == 200:
        json_string = response.json()  # Get the response as a JSON object
        print('Success:', json_string)  # Print the response in JSON format

else:
    print('Error:', response.status_code, response.text)  # Print error status and response text
 
I've reviewed your code, and while I am not a Python programmer, it is surprisingly similar to c#. Your code looks a lot like mine and would have been helpful If I had seen your code first. You should be aware that there is a github project with sample code. Sadly, for me, for the first time in 18 years it's not in C#, but you may find it helpful:

https://github.com/3cx/xapi-tutorial

But I sure would value a C# sample project. I'm having to learn new languages or figure this out by myself because the sample code is nearly worthless for me.
 
chatgpt does a pretty good job on converting code to a language you are familiar with.
I just paste the whole swagger.yaml and ask for powershell code for an endpoint and start from there
 
First I'd like to thank @uniscanadmin for providing another sample of code, I've added link to it to my GitHub readme page at https://github.com/luxzg/3CX-XAPI_examples so people find it more easily.

I'd also like to second what guidoq said, since available examples are a messy mix of code from Python, PowerShell, PHP, .NET, then we have swagger.yaml, half-written official documentation page, and those TypeScript examples, and so on... I'd just feed it all to it and you get pretty good sample code for any major programming language you need. With documentation lacking severely (at the moment) I think it's fastest route to understanding it all. This is partially why I've decided to put up all explanations on GitHub, and am currently trying to gather other existing code samples around these community forums to one place. Looking for all this information manually is exhausting, and if I already wasted days and days on it, I'd rather have it in one place, both for me and my colleagues, as well as anyone else in same situation. And with some luck, people will maybe share back like some already did here, so we can grow this small codebase of samples to something useful to everyone. Btw, I'm neither 3CX professional nor am I professional programmer, but I do what I can with resources at hand... Cheers!
 
  • Like
Reactions: guidoq

Members Online Now

Forum statistics

Threads
111,831
Messages
589,277
Members
164,660
Latest member
RJenkinsROCK