Skip to main content

Help with my first time with cPanel API

Comments

7 comments

  • DavidN.
    Hello, Thanks for contacting us. As a reseller, it's possible that you might not be able to install files where they need to go in order to use our API classes. To add pages to cPanel, they have to go into /usr/local/cpanel/base/frontend/paper_lantern someplace, and you won't have write access there. LiveAPI only works from inside custom cPanel pages, as it requires cPanel's context to instantiate. That may be what you're running into. To make API calls as your reseller, you will need to make HTTPS requests against the server where your reseller account lives on port 2087. There is a WHM API call, 'cpanel', which allows you to make cPanel API calls (cPanel Api1, Api2, and UAPI) on behalf of one of your users. I hope this is helpful. If you still have questions, please feel free to ask, or open a support ticket at cPanel Customer Portal. Again, thanks for the question. David Nielson cPanel Developer
    0
  • Rafael Alvarez
    Thank you for answering David, now I'm trying with URL calls and it's working, however I'm obtaining the security token by loging in from the textbox and then copying the token from my browser's URL. Is there any other way to get the security token without doing that? Like logging in with another url call? Example:
    0
  • cPanelMichael
    I'm obtaining the security token by loging in from the textbox and then copying the token from my browser's URL. Is there any other way to get the security token without doing that?

    Hello :) The "Single Sign On" and "Secure Remote Logins" sections of the following document might help, depending on how exactly you prefer to develop the script: Guide to API Authentication - Software Development Kit - cPanel Documentation Thank you.
    0
  • DavidN.
    Hello again, If your script sets authentication headers correctly, it won't be necessary to use the security token. However, if you only have your reseller credentials and need to log into the user's account, you will need to use the create_user_session API call, then use the security token from the URL it returns for API calls as the user. You should find these documents helpful: Guide to API Authentication - Software Development Kit - cPanel Documentation WHM API 1 Functions - create_user_session - Software Development Kit - cPanel Documentation David Nielson cPanel Developer
    0
  • Rafael Alvarez
    Thank you to both of you David and Michael, I upgraded to a VPS service. I'll give it a try.
    0
  • Rafael Alvarez
    I think I'm almost there, but I'm getting the following output with an error: {"cpanelresult":{"apiversion":2,"error":"Sorry, the given email address is invalid.","data":[{"reason":"Sorry, the given email address is invalid.","result":0}],"func":"addpop","event":{"result":1},"module":"Email"}} Any idea? Here's my code:
    $whmusername = "user"; $whmpassword = "password"; $query = 'https://127.0.0.1:2087/json-api/cpanel?cpanel_jsonapi_user=mycpaneluser&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=Email&cpanel_jsonapi_func=addpop&domain="domain.com"&user="emailuser"&password="12345luggage"&quota="500"'; $curl = curl_init(); // Create Curl Object curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0); // Allow self-signed certs curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0); // Allow certs that do not match the hostname curl_setopt($curl, CURLOPT_HEADER,0); // Do not include header in output curl_setopt($curl, CURLOPT_RETURNTRANSFER,1); // Return contents of transfer on curl_exec $header[0] = "Authorization: Basic " . base64_encode($whmusername.":".$whmpassword) . "\n\r"; curl_setopt($curl, CURLOPT_HTTPHEADER, $header); // set the username and password curl_setopt($curl, CURLOPT_URL, $query); // execute the query $result = curl_exec($curl); if ($result == false) { error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query"); // log error if curl exec fails } curl_close($curl); print $result; ?>
    0
  • cPanelMichael
    Hello :) The error message suggests you are not entering the "email" parameter correctly. Are you excluding the @domain.com suffix for this parameter? Also, I encourage you to use cPanel UAPI instead of cPanel API 2 going forward: UAPI Functions - Email::add_pop - Software Development Kit - cPanel Documentation Thank you.
    0

Please sign in to leave a comment.