1. Home
  2. Scripting
  3. Cpanel
  4. How to start using WHM/cPanel API

How to start using WHM/cPanel API

Using WHM/cPanel API
Using WHM/cPanel API

WHM API gives the power to the users to access the interface features of WHM. You can get familiar with the cPanel API by using the browser to call a function. However, if you are logged into WHM using API call, it might seem a bit redundant, but you can use API call to get many helpful outputs. In this post, you will get the overview of how to use WHM API.

Where can the API be used?

API can be used to perform the following tasks:

  • Server administration tasks
  • Administrating cPanel
  • Administrating WHM reseller accounts
  • Managing cPanel
  • Managing WHM services

Now you know what the purpose of API is, you should also know how to use WHM API to perform these tasks. Two methods will be explored in this article for using API to get the desired output.

Method 1: How to use API in a browser?

You can call a function with the browser to make yourself familiar with the cPanel API, and in fact, this is the easiest way to get that thing done.

When you want to visit a website, all you need to do is enter the URL and similarly, to perform API calls, you only need to get into a URL, and the task will be completed. Have you ever noticed an additional data in the URL of WHM interface? I am talking about the extra data that was in the form of https://x.x.x.x:2087.

This data can be used to manipulate other data for performing administration functions on the server.

For following this method, you log in to the WHM as a root, and apart from that, you should also have a security token. Once, you fulfill both the criteria; you will have a ten digit session ID in the form of https://x.x.x.x:2087:cpsess##########.

If you are looking forward to carrying out the steps yourself, make sure that you fill the right details of your server in the command. Now that you know about the basic requirement, we can move ahead to know how to use API in a browser.

Let’s try using API in a browser by carrying out some essential task like creating a DNS zone. To get this done, use the ‘adddns’ function along with the URL formatting guide, which was explained earlier in the post. The constructed URL will look like the following example:

From the above URL, you get the following information:

  • The output format of the URL is JSON
  • The ‘adddns’ function is being used
  • The function is carried out in API 1 version

The other parts of the URL command are the variables that the function needs to operate. For carrying out this operation, log yourself into WHM and then enter the command in the browser. Once, you do that you will receive a JSON output either in a paragraph form or in a single line depending on the browser that you are using to perform the task. In the result section of the output, if you see 1, you should know that it is an indication of a positive output, which means that your action has been successfully carried out. You can also confirm it by checking ‘Edit a DNS Zone’ in WHM where in the list of the items you will be able to see your example.com. Once, you select ‘example.com’ you will able to see all the default records of cPanel.

Method 2: How to use Custom Code for API Calls?

The second method to interact with cPanel API via API call is using custom codes. When you decide to use codes for API calls, only your imagination can set a limit to the coding. But there are few pre-requisites to use custom code for API calls, and the first requirement is to access hash in WHM to validate your actions; it means you need to make sure that the commands that you are using are authenticated. When you have obtained hash, you gain the power to use the server without actually using a root password.

For creating the hash, visit WHM and go to the ‘Remote Access Key’ section. The chances are high that a key already exists on your server; if that is the case, you should use the pre-existing key, and if you are unable to see a key, you can always create a new key by clicking ‘Generate New Key.’ When you have a key, you will see a long string of text that has to be used in the file. You can also access that long line of text by entering /root/.accesshash. You later have to add the code, which has been described later.

If you are working on Perl, the Perl modules needs to be installed in your server by using the following two commands:

Command 1:

 # yum install perl-XML-Simple

Command 2:

  # /scripts/peprlinstaller LWP::Protocol::https

Now that you finally have access to hash, you can use it in the code to authenticate the strip for an API call. You have to use the following code:


use strict ;

use warnings ;

use LWP::UserAgent ;

use XML::Simple;

use Data::Dumper;

use HTTP::Cookies;


my $hostname = ‘localhost’

my $protocol = ‘https’;

my $accesshash = <<’END’;

‘Copy and paste the long string of texts that was earlier displayed while generating the key'


$accesshash =~ s/n//g;

my $useragent + LWP::UserAgent-> new(cookie_jar => HTTP::Cookies->new, );

$useragent ->default_header( ‘Authorization’ => “WHM root:$accesshash” );

my $url = “$protocol://$hostname:2087/json-api/adddns?api.version=1&domain=example2.com&ip=”;

my $response = $useragent ->get($url);

my $response_data = $response->decoded_content;print $response_data;

Please note that you have to replace the lines 14-42 with the information of your own hash. Also, make sure that the script needs to have an executable permission so that it is able to run on the server. You can take the permission by the entering the following command:

# chmod 755 dns-example.pl

After the commands have been given, you need to save the file, which you need to later run by using the following command:

/user/bin/perl dns-example.pl

If all the commands and codes are successfully accepted, you will see the JSON result as 1, which again shows that the action has been successfully completed.

You can try any of the above two methods according to your requirement to use WHM/cPanel for API.

Updated on December 18, 2017

Was this article helpful?

Related Articles

Add A Comment