Today I posted cPanel::PublicAPI to github. This is a set of perl modules that allows for easy access into cPanel’s APIs from a simple object interface. This module offers several great features:

  • Auto-detection of credentials (when available)
  • Support for cPanel’s DNS Clustering API
  • Support for: cPanel, WHM, webmail and non-cPanel services.
  • Minimal dependencies
  • BSD Licensed

To get started, you can install cPanel::PublicAPI via CPAN the source is also available on our github repository if you wish to submit patches/changes. I strongly suggest reading the documentation on CPAN to get started.

The following is an example of how to query the XML-API’s listaccts() call using PublicAPI (on a cPanel server, running as root, and an accesshash exists):
#!/usr/bin/perl
use cPanel::PublicAPI ();
use Data::Dumper;
my $pubapi = cPanel::PublicAPI->new();
print Dumper $pubapi->whm_api('listaccts');

To query a cpanel account with API2 while authenticated as root, you would do something like the following:
#!/usr/bin/perl
use cPanel::PublicAPI ();
use Data::Dumper      ();
my $pubapi = cPanel::PublicAPI->new();    # Create email account
print Dumper $pubapi->cpanel_api2_request(
'whostmgr',
{
'module' => 'Email',
'func' => 'addpop',
'user' => 'someuser',
},
{
'domain' => 'myawesomedomain.com',
'email' => 'localuser',
'password' => 'AS(d0m9d0sad12ea!s',
'quota' => 42,
}
);

You may have noticed that you have to specify the service as the first parameter. It is important to note that even though you may be calling the cpanel API, if you are root or a reseller querying an account that is not the reseller account itself, you will still need to query the whostmgr service. This is because if you authenticate against the cpanel service, you will be querying your user, no mater which user you specify in the “config” hash.

If you don’t want cPanel::PublicAPI to auto-detect credentials, you can pass in the user and pass:
my $pubapi = cPanel::PublicAPI->new( 'user' => 'someuser', 'pass' => 'sdfkklkasf' );

Or you can specify an access hash:
my $pubapi = cPanel::PublicAPI->new( 'user' => 'someuser', 'accesshash' => $contents_of_homedir_slash_dot_acesshash );

There are tons of other features within this module, I suggest starting out with the POD documentation for cPanel::PublicAPI and going from there. If you have any further questions, please post on our dev forums and we will be happy to help you.