{"id":24712,"date":"2014-12-03T13:00:00","date_gmt":"2014-12-03T19:00:00","guid":{"rendered":"http:\/\/blog.cpanel.net\/?p=24712"},"modified":"2014-12-03T13:00:00","modified_gmt":"2014-12-03T19:00:00","slug":"introduction-to-using-the-whm-api","status":"publish","type":"post","link":"https:\/\/devel.www.cpanel.net\/blog\/tips-and-tricks\/introduction-to-using-the-whm-api\/","title":{"rendered":"Introduction to using the WHM API"},"content":{"rendered":"

Hey everyone!<\/p>\n

This post is meant to be a brief overview for using the WHM API. We’ll explore two methods of using the API – from a browser and from a Perl script on the command line.<\/p>\n


\nFinding out what you can do with the API<\/b><\/p>\n

You can view a list of all the Available Functions for the API calls at http:\/\/documentation.cpanel.net\/disp…ions-Functions<\/a>. We’ll reference this page later when we’re constructing our API commands. You can also get specific details about each function by clicking the item, such as the adddns function at http:\/\/documentation.cpanel.net\/disp…API+1+-+adddns<\/a> which we’ll be using throughout this guide as our example.<\/p>\n

Using the API in a Browser and Constructing the API URL<\/span><\/b><\/p>\n

The easiest way to get familiar with the cPanel API is to call a function using your browser. Just like visiting a website by entering a URL, you can enter a URL that will perform API calls. During your use of the WHM interface you’ve likely noticed additional data after the typical URL of https:\/\/x.x.x.x:2087<\/a> and we can manipulate that data to perform administrative functions on the server. Although calling functions using the API may seem redundant since you’re already logged into WHM, and thus have full access to the interface and available GUI tools, it will let you see if your API call works as you expect and gives you some helpful output (in either JSON or XML format, whichever you prefer) if you do run into problems.<\/p>\n

Here’s an image that shows the basic formatting of an API call – this looks a lot like the familiar WHM login of https:\/\/x.x.x.x:2087<\/a> but just includes extra data after the session ID.<\/p>\n

\"WHM_API_1_breakout\"<\/a><\/p>\n

***NOTE***
\nThe following methods assume you’re already logged in to WHM as root and have a security token. This means that you’ll have a unique, 10-digit session ID in the format of
https:\/\/x.x.x.x:2087:cpsess##########<\/a>. In the following examples I’ll just use the localhost IP of 127.0.0.1 and cpsess########## to avoid confusion so be sure to edit these commands with the appropriate details for your own server if you want to try these out for yourself.
\n**********<\/p>\n

Using the URL<\/span><\/b><\/p>\n

Now that we have a basic idea of what the URL should look like, let’s perform a basic task with the API – creating a DNS zone. According to the functions page listed earlier in this guide we’ll want to use the “adddns” function. If we follow the URL formatting guide above and the layout details from the function guide at http:\/\/documentation.cpanel.net\/disp…API+1+-+adddns<\/a> we can construct our URL to look like this:<\/p>\n

\n
https:\/\/127.0.0.1:2087\/cpsess##########\/json-api\/adddns?api.version=1&domain=example.com&ip=127.0.0.1<\/pre>\n<\/div>\n

If we analyze this command using the above URL breakout image we have the following areas::<\/p>\n

-The “output format” is JSON<\/p>\n

-we’re using the “adddns” function<\/p>\n

-The API version we’re using is version 1<\/p>\n

The rest of the command is the variables that the function requires to operate. I’ve just used “example.com” for the domain to create and I’m setting it up on the IP of 127.0.0.1<\/p>\n

Now, assuming we’re already logged into WHM, we can enter that command in our browser – you should receive JSON output that looks similar to the following, but depending on your browser this could be all displayed on one line.<\/p>\n

\n
{\n   metadata: {\n      result: 1,\n      version: 1,\n      reason: 'Added example.com ok belonging to user root',\n      command: 'adddns'\n   }\n}<\/pre>\n<\/div>\n

In general, when working with JSON, “1” indicastes positive output, meaning the action completed successfully. We can see from this output that the domain was accepted and example.com has been created on the server. If we check out WHM >> Edit a DNS Zone you should see example.com in your list now, and selecting it will show a zone file with all the default cPanel records:<\/p>\n

\"example1_adddns\"<\/a><\/p>\n

Using Custom Code for API Calls<\/b><\/span><\/p>\n

The second way to interact with the cPanel API is to call the API functions through custom code. When writing your own code there are many options limited only by your creativity. The examples here are just that – examples to show you the basics so you can see how the API code functions within a Perl script. They are by no means exhaustive in any way.<\/p>\n

Prerequisites<\/span><\/b><\/p>\n

Since this code will be called from a file we need to have a way to interact with WHM so we use an access hash so our commands are authenticated just as if we were clicking items in the WHM interface. An access hash is a way for a program to access a server without having to use the root password. This hash is used for several things in the cPanel system, most notably setting up DNS Clusters. To create this hash you can visit WHM >> Remote Access Key. There is likely a key already in place on your server – if this is the case it’s best to just use what already exists. If there isn’t a key in place you can generate one with the blue “Generate New Key” button on the right.<\/p>\n

\"access_hash\"<\/a><\/p>\n

That long string of text is what we’ll need to use in our file. You can also find that at \/root\/.accesshash. We’ll need to add that block to line 14 of the code in the example below.<\/p>\n

Since we’re working with Perl we’ll also need some of the standard Perl modules installed on the server. You’ll want to run the following two commands to get these modules installed:<\/p>\n

\n
yum install perl-XML-Simple\n\/scripts\/perlinstaller LWP::Protocol::https<\/pre>\n<\/div>\n

Building the Code<\/span><\/b><\/p>\n

Now that we have the access hash we can use that in our code to authenticate our script to make an API call. Just like in our first example, here is some code that will create a DNS zone for example2.com. I’m using example2.com since we already created example1.com in our previous test. You can copy and paste the following code into a file named dns-example.pl using your favorite text editor. The file is named with the “.pl” extension because it is a Perl script. It looks like there is a large amount of code necessary to create a DNS zone, but much of this is just the access hash.<\/p>\n

\n
#!\/usr\/bin\/perl\nuse strict;\nuse warnings;\nuse LWP::UserAgent;\nuse XML::Simple;\nuse Data::Dumper;\nuse HTTP::Cookies;\n \n$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;\n \nmy $hostname = 'localhost';\nmy $protocol = 'https';\nmy $accesshash = <<'END';\na1afa058158ada545f9cd9c0bb3cde63\n0baa595074c2e42e2f8513103688eb6c\nbb6c343a05826b6b6e300b77e566b876\nde4c14ad947efc3d27566c5bc69caeea\n4edcb02301fb87b49bcf2e7ccfb4184a\nb42b83f41ded645c7c9061ab369bddc9\nc786062777d7832e123a8334f56aef3e\n4388f368249022cabde665d7261ab88a\n70d030daefafdaead1ce815e98558d6c\ncb7bbc1052a60df4ed110e0c0f909bd6\nefc43e9923542e290fc6209d038adae9\nbee24339bf9d849fcd78c50d29474193\ndfeb9de9f979cb06e90d3d66b618369d\n180ae4382550a177316f872254739990\n8b5de8ab871029a47f29ccd90ee5e52f\nb12bf41f37de3ead8cdd26027d50364d\n1a6df8c0f2d29ecd29aab9254eeb6808\nd232f1e729453a5a5de27e276e9bba1d\n8a27ba0d4690af299d4e461757bfa412\n1a0bb3474b46d14926ff7351a1d2d550\nb19351d670c82c82cfdf661ed7effc8d\n530af69720062aa62e48ea5c45261691\nd2c49b4def5f2e47beac5f796304a024\n4ecb921948a8c77c00d78a4fee6e8743\n7430731106a8f56e2853798533672868\nd92f2eb9b9444492ac2701f1367e0b9f\n843078ac419a2e6a90342848f756edde\n1dfb59a5fdfed2ad8954ec3c2ee7b868\n2c9aa7f30d722a566d755b54778c6a96\nEND\n$accesshash =~ s\/n\/\/g;\nmy $useragent = LWP::UserAgent->new(\ncookie_jar => HTTP::Cookies->new,\n);\n$useragent->default_header( 'Authorization' => \"WHM root:$accesshash\" );\n my $url = \"$protocol:\/\/$hostname:2087\/json-api\/adddns?api.version=1&domain=example2.com&ip=127.0.0.1\";\n my $response = $useragent->get($url);\n my $response_data = $response->decoded_content;\n print $response_data;<\/pre>\n<\/div>\n

When you go to use this code you’ll need to replace lines 14-42 with your own access hash. There are also ways to have the script read the access hash directly from the server but that level of programming is beyond the scope of this guide.<\/p>\n

To better understand what is happening in this code, here is a line-by-line description:<\/p>\n

Lines 1-48: Creates the user agent and declares variables, including the access hash
\nLines 49-51: The actual API code
\nLine 52: The output from the code<\/p>\n

You’ll also need to make sure the script has executable permissions so it can run on the server. After you’ve saved the file with all of your changes you can issue the following command from shell to change the permissions so the script can be executed<\/p>\n

\n
chmod 755 dns-example.pl<\/pre>\n<\/div>\n

If this code were being used in a production environment the “&domain=example2.com” and the “&ip=127.0.0.1” sections would ideally be variables that get their values from data entered by a user through some type of form. However, since we’re just providing an example of how the code works outside of a working graphical interface they’ll need to be hard-coded.<\/p>\n

Executing the Script<\/span><\/b><\/p>\n

Once you have the file saved you can execute it by running the following command:
\n\/usr\/bin\/perl dns-example.pl<\/p>\n

If everything goes well you should get output that looks similar to the following:<\/p>\n

\"example_output\"<\/a><\/p>\n

Just as with the previous JSON results, seeing “1” in the output indicates that the action completed successfully. If you check your \/var\/named\/ directory you should see the DNS zone file for example2.com.db which looks like this:<\/p>\n

\"final_zone\"<\/a><\/p>\n

I hope that gives you an idea how the WHM API works and that you found these examples useful!<\/p>\n","protected":false},"excerpt":{"rendered":"

Hey everyone! This post is meant to be a brief overview for using the WHM API. We’ll explore two methods of using the API – from a browser and from a Perl script on the command line.<\/p>\n","protected":false},"author":77,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[61],"tags":[],"class_list":["post-24712","post","type-post","status-publish","format-standard","hentry","category-tips-and-tricks"],"acf":[],"yoast_head":"\nIntroduction to using the WHM API | cPanel<\/title>\n <!-- Mirrored from cpanel.net/wp-json/wp/v2/posts/24712/ by HTTrack Website Copier/3.x [XR&CO'2014], Fri, 06 Sep 2024 09:52:49 GMT --> <meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=_-2038.html \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Introduction to using the WHM API | cPanel\" \/>\n<meta property=\"og:description\" content=\"Hey everyone! This post is meant to be a brief overview for using the WHM API. We’ll explore two methods of using the API – from a browser and from a Perl script on the command line.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/devel.www.cpanel.net\/blog\/tips-and-tricks\/introduction-to-using-the-whm-api\/\" \/>\n<meta property=\"og:site_name\" content=\"cPanel\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cpanel\/\" \/>\n<meta property=\"article:published_time\" content=\"2014-12-03T19:00:00+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/blog.cpanel.com\/wp-content\/uploads\/2014\/12\/WHM_API_1_breakout-300x48.png\" \/>\n<meta name=\"author\" content=\"cPanel Community\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@cPanel\" \/>\n<meta name=\"twitter:site\" content=\"@cPanel\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"cPanel Community\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/devel.www.cpanel.net\/blog\/tips-and-tricks\/introduction-to-using-the-whm-api\/\",\"url\":\"https:\/\/devel.www.cpanel.net\/blog\/tips-and-tricks\/introduction-to-using-the-whm-api\/\",\"name\":\"Introduction to using the WHM API | cPanel\",\"isPartOf\":{\"@id\":\"https:\/\/devel.www.cpanel.net\/#website\"},\"datePublished\":\"2014-12-03T19:00:00+00:00\",\"dateModified\":\"2014-12-03T19:00:00+00:00\",\"author\":{\"@id\":\"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/8cf97408aad4fb70cf55d11a1d4f57f8\"},\"breadcrumb\":{\"@id\":\"https:\/\/devel.www.cpanel.net\/blog\/tips-and-tricks\/introduction-to-using-the-whm-api\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/devel.www.cpanel.net\/blog\/tips-and-tricks\/introduction-to-using-the-whm-api\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/devel.www.cpanel.net\/blog\/tips-and-tricks\/introduction-to-using-the-whm-api\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/devel.www.cpanel.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Introduction to using the WHM API\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/devel.www.cpanel.net\/#website\",\"url\":\"https:\/\/devel.www.cpanel.net\/\",\"name\":\"cPanel\",\"description\":\"Hosting Platform of Choices\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/devel.www.cpanel.net\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/8cf97408aad4fb70cf55d11a1d4f57f8\",\"name\":\"cPanel Community\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e1949945083b5526bb95711bd3d616b3?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e1949945083b5526bb95711bd3d616b3?s=96&d=mm&r=g\",\"caption\":\"cPanel Community\"},\"description\":\"The web hosting industry's most reliable management solution since 1997. With our first-class support and rich feature set, it's easy to see why our customers and partners make cPanel & WHM their hosting platform of choice. For more information, visit cPanel.net.\",\"sameAs\":[\"https:\/\/cpanel.net\"],\"url\":\"https:\/\/devel.www.cpanel.net\/blog\/author\/cpadmin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Introduction to using the WHM API | cPanel","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/devel.www.cpanel.net\/blog\/tips-and-tricks\/introduction-to-using-the-whm-api\/","og_locale":"en_US","og_type":"article","og_title":"Introduction to using the WHM API | cPanel","og_description":"Hey everyone! This post is meant to be a brief overview for using the WHM API. We’ll explore two methods of using the API – from a browser and from a Perl script on the command line.","og_url":"https:\/\/devel.www.cpanel.net\/blog\/tips-and-tricks\/introduction-to-using-the-whm-api\/","og_site_name":"cPanel","article_publisher":"https:\/\/www.facebook.com\/cpanel\/","article_published_time":"2014-12-03T19:00:00+00:00","og_image":[{"url":"http:\/\/blog.cpanel.com\/wp-content\/uploads\/2014\/12\/WHM_API_1_breakout-300x48.png"}],"author":"cPanel Community","twitter_card":"summary_large_image","twitter_creator":"@cPanel","twitter_site":"@cPanel","twitter_misc":{"Written by":"cPanel Community","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/devel.www.cpanel.net\/blog\/tips-and-tricks\/introduction-to-using-the-whm-api\/","url":"https:\/\/devel.www.cpanel.net\/blog\/tips-and-tricks\/introduction-to-using-the-whm-api\/","name":"Introduction to using the WHM API | cPanel","isPartOf":{"@id":"https:\/\/devel.www.cpanel.net\/#website"},"datePublished":"2014-12-03T19:00:00+00:00","dateModified":"2014-12-03T19:00:00+00:00","author":{"@id":"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/8cf97408aad4fb70cf55d11a1d4f57f8"},"breadcrumb":{"@id":"https:\/\/devel.www.cpanel.net\/blog\/tips-and-tricks\/introduction-to-using-the-whm-api\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/devel.www.cpanel.net\/blog\/tips-and-tricks\/introduction-to-using-the-whm-api\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/devel.www.cpanel.net\/blog\/tips-and-tricks\/introduction-to-using-the-whm-api\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/devel.www.cpanel.net\/"},{"@type":"ListItem","position":2,"name":"Introduction to using the WHM API"}]},{"@type":"WebSite","@id":"https:\/\/devel.www.cpanel.net\/#website","url":"https:\/\/devel.www.cpanel.net\/","name":"cPanel","description":"Hosting Platform of Choices","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/devel.www.cpanel.net\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/8cf97408aad4fb70cf55d11a1d4f57f8","name":"cPanel Community","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e1949945083b5526bb95711bd3d616b3?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e1949945083b5526bb95711bd3d616b3?s=96&d=mm&r=g","caption":"cPanel Community"},"description":"The web hosting industry's most reliable management solution since 1997. With our first-class support and rich feature set, it's easy to see why our customers and partners make cPanel & WHM their hosting platform of choice. For more information, visit cPanel.net.","sameAs":["https:\/\/cpanel.net"],"url":"https:\/\/devel.www.cpanel.net\/blog\/author\/cpadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/posts\/24712"}],"collection":[{"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/users\/77"}],"replies":[{"embeddable":true,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/comments?post=24712"}],"version-history":[{"count":0,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/posts\/24712\/revisions"}],"wp:attachment":[{"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/media?parent=24712"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/categories?post=24712"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/tags?post=24712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}