cURL example or template for UAPI Return Email Accounts

Operating System & Version
Linux
cPanel & WHM Version
86.0.30

Indinfer

Member
Mar 25, 2018
17
1
3
Baltimore, Maryland
cPanel Access Level
Website Owner
I am new to using UAPI to access cPanel. I am also new to cURL. But perusing around, currently, I think cURL is the way for me to access cPanel from my application. The user is supposed to be able to create and delete email accounts from my application.

I hope that my being able to get a list of email accounts via UAPI will be enough for me to understand how to call the other UAPI functions.

I am looking here in the cPanel UAPI documentation:
Return email accounts · cPanel & WHM Developer Portal

I think I understand creating and using an authorization token. I'm hoping someone can give me an example (or template) cURL line to get a list of email accounts.
 

Indinfer

Member
Mar 25, 2018
17
1
3
Baltimore, Maryland
cPanel Access Level
Website Owner
I need to operate remotely. I think that if the curl line would work from a Windows command line, it should also work invoked from my program in Lisp and C++.

So I guess I need terminal mode cURL.

I'm okay trying something that works on a Mac, Linux, or Unix command line, because it might also work from Windows.

If we are getting all the email accounts one by one, I don't think I need a working loop. What I think I would need is something like
1. How to get the first account.
2. How to get the next account.
3. How to know when I got all the accounts.

Of course, I don't know whether cPanel UAPI works this 1, 2, 3 way.
 

Indinfer

Member
Mar 25, 2018
17
1
3
Baltimore, Maryland
cPanel Access Level
Website Owner
A appreciate your patience in trying to help me with my problem.

Yes, a link is good. I could not find "cURL" in the link I showed and the link you showed.

I have a test website to figure out how to do this. I can reveal the website and the token because I will delete the token to prevent some third party from abusing the website.

The website is: Assigned Email Manager (assigned-email-manager.com)
The token is: 7FBI3QIC1HNXYORBCDRWU2XL6TZP09N7

I am at a Windows command line. I type:

curl https://assigned-email-manager.com:2083/cpsess7FBI3QIC1HNXYORBCDRWU2XL6TZP09N7/execute/Email/list_pops


The response does not show any of the three email accounts. I am doing something wrong. But I don't know what.
 

ankeshanand

Well-Known Member
Mar 29, 2021
193
58
103
India
cPanel Access Level
Root Administrator
Thats for Creating Email Accounts.... and what you did is actually wrong... The reference I gave is for whmapi1 which is on Port 2087. The basic usage would be like:
Code:
https://hostname.example.com:2087/cpsess##########/json-api/accountsummary?api.version=1&user=username
In Your Case, it would be like
Code:
curl https://hostname.example.com:2087/cpsess##########/json-api/list_pops_for?api.version=1&user=username
Before that, You have to authenticate the root user using create_user_session ... which will give you a Security Token cpsess...
The Response on My Server is:
Code:
{"metadata":{"command":"list_pops_for","version":1,"reason":"OK","result":1},"data":{"pops":["[email protected]"]}}
If you need more Help, just let me know!
 

Indinfer

Member
Mar 25, 2018
17
1
3
Baltimore, Maryland
cPanel Access Level
Website Owner
Before that, You have to authenticate the root user using create_user_session ...
I think you figured out where my problem is because you alerted me that I need to get a proper session token. Seems to me that THAT's what I don't know how to do.

I was thinking that I could not use anything from whmapi1 because I (and hopefully my future clients) will be using a website hosting service, such as GoDaddy.com. We will not be providing any website hosting. We will be using. Am I wrong here? Can I indeed use both whmapi1 and uapi?

My eyes seem to be missing documentation on exactly how to use an API token to get a session token (cpsess...). I'm trying looking in both cPanel UAPI and WHM API1. Either my eyes keep missing it or I'm not looking in the right place. Could you point me to documentation that will hopefully make very clear to me how to use cURL to use an API token to get a session token?
 

ankeshanand

Well-Known Member
Mar 29, 2021
193
58
103
India
cPanel Access Level
Root Administrator
I think you figured out where my problem is because you alerted me that I need to get a proper session token. Seems to me that THAT's what I don't know how to do.

I was thinking that I could not use anything from whmapi1 because I (and hopefully my future clients) will be using a website hosting service, such as GoDaddy.com. We will not be providing any website hosting. We will be using. Am I wrong here? Can I indeed use both whmapi1 and uapi?

My eyes seem to be missing documentation on exactly how to use an API token to get a session token (cpsess...). I'm trying looking in both cPanel UAPI and WHM API1. Either my eyes keep missing it or I'm not looking in the right place. Could you point me to documentation that will hopefully make very clear to me how to use cURL to use an API token to get a session token?
To Create or List or do anything regarding API, You need whmapi1 and for that, You have to have a root account or Reseller Account. Because Client Side API is also available in cPanel but for that you'll need API keys for each cpanel account which seems impossible. From what you mentioned, I think you are preparing for an Email Hosting Solution or maybe some part of it.
For Email Hosting, You can do the following:
1.Setup a Small VPS with about 2 Cores and a Lot of Storage and Get cPanel. You can Host Emails from there using SMTP and IMAP/POP3
2. Use PHP for Making Client Area. It supports cURL and a Lots of other things needed to make Cross Connection. You can then integrate a Third party Webmail Service and all others through whmapi1 and Provide your end users also with API for email solutions.

This is the Documentation for Creating a Session (Use it for Authenticating Webmail with your App)
This is the Documentation below for creating a Root Session (Use it to Login with root and use WHMAPI)
Code:
$user = "root";
$token = "API Token";

$query = "https://server.example.com:2087/json-api/create_user_session?api.version=1&user=root&service=whostmgrd";

$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);

$header[0] = "Authorization: whm $user:$token";
curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
curl_setopt($curl, CURLOPT_URL, $query);

$result = curl_exec($curl);

$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ($http_status != 200) {
    echo "[!] Error: " . $http_status . " returned\n";
} else {
    $json = json_decode($result, true);
    $login_url = $json['data']['url'];
    echo $login_url;
}
curl_close($curl);
Although this is the backend of what I made on PHP for Root login. Although, You can also use Root password instead of API token for Authentication... If you need it for Terminal, let me know and I'll type it for you...
 

Indinfer

Member
Mar 25, 2018
17
1
3
Baltimore, Maryland
cPanel Access Level
Website Owner
I have not said much about what I am trying to do. So, therefore, although I think what you are saying about getting will work, I don't think it will work for me. So, I will say a little more about what I am trying to do.

My applications will be desktop, Android, and iPad. It is for the masses of people who want multiple email accounts. Most will be very unsophisticated users. And they don't want to become sophisticated. They want to click on a button, and one of their email accounts disappears. Type in a user name and click on another button and a new email account appears ready for use. The few sophisticated users should find my app enables them to create and delete their email addresses super-fast.

All of these users will not want to spend any significant money to rent a VPS. Instead, they will want their own personal domain name and the cheapest kind of website for their email.

But now, I think I got a little further. But I still need help.

I finally found this page: How to Use cPanel API Tokens | cPanel & WHM Documentation

Combining that page with the documentation to get a list of existing email addresses, here is how my session went:

C:\Users\rlewi>curl -H'Authorization: cpanel username:7FBI3QIC1HNXYORBCDRWU2XL6TZP09N7' 'https://assigned-email-manager.com:2083/execute/Email/list_pops'

curl: (6) Could not resolve host: cpanel
curl: (3) Port number ended with 'F'
curl: (1) Protocol "'https" not supported or disabled in libcurl


I got the three errors. Does this only bear out what you described, that every user needs to get a VPS server? Or is there some way to figure out what I need to do from these errors?

Error 1 (could not resolve hose). I thought the host was "assigned-email-manager.com". Why does it mention cpanel?
Error 2 (port number). I thought the port number ended in a 3 (as in 2083). Where is the 'F'?
Error 3 (protocol). I thought cURL did support HTTPS. Is it enabled? Maybe not. How would I enable HTTPS in cURL?

Who knows? Maybe if we can solve just one of the errors, all of the errors will go away, and the cURL command will work.
 

ankeshanand

Well-Known Member
Mar 29, 2021
193
58
103
India
cPanel Access Level
Root Administrator
As Said, Your App then needs API Token Created by respective users...
Code:
C:\Users\Abc>curl 'https://assigned-email-manager.com:2083/execute/Email/list_pops' -H "Authorization: cpanel username:7FBI3QIC1HNXYORBCDRWU2XL6TZP09N7"
This will work!
 

Indinfer

Member
Mar 25, 2018
17
1
3
Baltimore, Maryland
cPanel Access Level
Website Owner
Yes, your cURL line works much better. You smashed two of the bugs. I scrutinized your line. It looks like you used double quotes instead of single quotes for the -H parameter. And you put a space between the -H switch and the parameter part. When I did that, I found that both of these versions worked better than before.

curl 'https://assigned-email-manager.com:2083/execute/Email/list_pops' -H "Authorization: cpanel username:7FBI3QIC1HNXYORBCDRWU2XL6TZP09N7"

curl -H "Authorization: cpanel username:7FBI3QIC1HNXYORBCDRWU2XL6TZP09N7" 'https://assigned-email-manager.com:2083/execute/Email/list_pops'


I think your order makes more sense. So, I am happy to continue with the cURL line as you presented it.

Maybe you get different results than I because you are executing from the server, and I am executing remotely?

Here is my session now:


C:\Users\rlewi>curl 'https://assigned-email-manager.com:2083/execute/Email/list_pops' -H "Authorization: cpanel username:7FBI3QIC1HNXYORBCDRWU2XL6TZP09N7"

curl: (1) Protocol "'https" not supported or disabled in libcurl


Maybe there is a cURL parameter I can add that force-enables HTTPS? I will look at the output from

curl --help
 

Indinfer

Member
Mar 25, 2018
17
1
3
Baltimore, Maryland
cPanel Access Level
Website Owner
Curious, using https versus http:


C:\Users\rlewi>curl 'https://assigned-email-manager.com:2083/execute/Email/list_pops' -H "Authorization: cpanel username:7FBI3QIC1HNXYORBCDRWU2XL6TZP09N7"

curl: (1) Protocol "'https" not supported or disabled in libcurl



C:\Users\rlewi>curl 'http://assigned-email-manager.com:2083/execute/Email/list_pops' -H "Authorization: cpanel username:7FBI3QIC1HNXYORBCDRWU2XL6TZP09N7"

curl: (1) Protocol "'http" not supported or disabled in libcurl



I don't know where anything is enabled or disabled in libcurl. I presume cURL uses libcurl? But I can deal with enabling a protocol if enabling amounts to including some command line parameter. I don't even know where cURL is located in Windows. Windows 10 comes with cURL already installed.
 

Indinfer

Member
Mar 25, 2018
17
1
3
Baltimore, Maryland
cPanel Access Level
Website Owner
I understood from this website (Windows 10 – CURL Protocol "’https" not supported or disabled in libcurl Error | administrator (wordpress.com) ) that there is something about using cURL in Windows in which the cURL command line should use double quotes instead of single quotes. So, I don't know whether this is progress or retrogress. But here is my session now:

C:\Users\rlewi>curl "https://assigned-email-manager.com:2083/execute/Email/list_pops" -H "Authorization: cpanel username:7FBI3QIC1HNXYORBCDRWU2XL6TZP09N7"

Access denied


Now I am really at a dead end. I think I am doing the authorization correctly. I get an "Access denied" error but am not directed to any reason or document.

I own the website. If anyone should have access, wouldn't I have access?
 

Indinfer

Member
Mar 25, 2018
17
1
3
Baltimore, Maryland
cPanel Access Level
Website Owner
To Create or List or do anything regarding API, You need whmapi1, and for that, You have to have a root account or Reseller Account. Because Client-Side API is also available in cPanel but for that you'll need API keys for each cpanel account which seems impossible. From what you mentioned, I think you are preparing for an Email Hosting Solution or maybe some part of it.
For Email Hosting, You can do the following:
1.Setup a Small VPS with about 2 Cores and a Lot of Storage and Get cPanel. You can Host Emails from there using SMTP and IMAP/POP3
2. Use PHP for Making Client Area. It supports cURL and a Lots of other things needed to make Cross Connection. You can then integrate a Third party Webmail Service and all others through whmapi1 and Provide your end users also with API for email solutions.
ankeshanand,

Maybe I finally see the wisdom of what you said earlier. I appreciate your patience greatly in working with me until I finally understand.

I am sure my potential clients will not pay US$ 400 OR 500 per year just to have multiple emails and their own domain. But if I do what you said to get VPS with 2 Cores and cPanel, maybe I can host multiple domains. If I can do that, maybe I can share out this server with many clients. Thus, the cost to each client can be a fraction of the cost of the VPS. Each client needs to add and delete their own email accounts. My application could limit a client's access so that one client cannot disturb another client's use. Since we are talking about email, performance can be much less than for a website.

Thank you so much, ankeshanand. It is very possible you gave me THE answer early on.
 

ankeshanand

Well-Known Member
Mar 29, 2021
193
58
103
India
cPanel Access Level
Root Administrator
ankeshanand,

Maybe I finally see the wisdom of what you said earlier. I appreciate your patience greatly in working with me until I finally understand.

I am sure my potential clients will not pay US$ 400 OR 500 per year just to have multiple emails and their own domain. But if I do what you said to get VPS with 2 Cores and cPanel, maybe I can host multiple domains. If I can do that, maybe I can share out this server with many clients. Thus, the cost to each client can be a fraction of the cost of the VPS. Each client needs to add and delete their own email accounts. My application could limit a client's access so that one client cannot disturb another client's use. Since we are talking about email, performance can be much less than for a website.

Thank you so much, ankeshanand. It is very possible you gave me THE answer early on.
Access Denied.... That dosen't seem likely. Have you checked the API tokens?

If you still not able to do it... Could you share the API Details in Conversation? (Remember, cPanel Advises not to share personal data on Forums... and the same will be said by cPRex after he views this message! ;) )

Make a Plan like this:
1 Purchase a Cheap VPS from Hetzner or Contabo or someone like that....(They charge like 5$/Month)
2. Setup WHM/cPanel
3 Use json-api on Port 2087 if you want to achieve Email Hosting.
 
  • Like
Reactions: Indinfer

Indinfer

Member
Mar 25, 2018
17
1
3
Baltimore, Maryland
cPanel Access Level
Website Owner
Ankesh Anand,

Yes, I did check the tokens. I think remote access to cPanel is prohibited by the web hosting provider for shared website hosting. I did not verify this because I did not want to wait over 90 minutes to connect via their chat.

I think we have the cURL line correct. I greatly appreciate your help.

I am shopping web hosting. You have given me a good idea of what to expect.
 
  • Like
Reactions: ankeshanand