1. Home
  2. Web Panels
  3. WHM/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 users the power 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, this might seem a bit redundant. You can use API call to get many helpful outputs.
In this tutorial we will teach you how to 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 learn 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; 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. 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? 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 administrative functions on the server.
For following this method, log in to the WHM as a root, you should also have a security token. Once you fulfil both of these criteria; you will have a ten digit session ID in the form of ‘https://x.x.x.x:2087:cpsess##########’.
Make sure that you fill the right details of your server in the command. Now that you know about the basic requirements, you can move ahead to know how to use API in a browser.
You can try using API in a browser by carrying out some essential task like creating a DNS zone. To do this, use the ‘addons’ 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 can 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 results section of the output, if you see 1, it is an indication of a positive output which means that your action has been successfully carried out. You can also confirm this by checking ‘Edit a DNS Zone’ in WHM where in the list of the items you will be able to see your ‘example.com’. After you select ‘example.com’, you will able to see all of 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, the limit of the coding is your imagination. There are, however, amfew pre-requisites to use custom code for API calls. The first requirement is to access hash in WHM to validate your actions; this means that 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.
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 need 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 here the long encrypted key text displayed earlier 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 by using the following command.

/user/bin/perl dns-example.pl

If all of 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.
If you wish, you can try any of the above two methods to use WHM/cPanel for API.

Updated on December 23, 2018

Was this article helpful?

Related Articles

Leave a Comment

[apsl-login-lite login_text='Please login with a social account']