As you familiarize yourself with all that is available in cPanel, you inevitably may decide there are opportunities to remotely work with it. Imagine the ability to create subdomains, backup an account, or create an email address without ever accessing the cPanel user interface? In fact, the cPanel user interface is using the same APIs available to you to work with most all of its functions.
cPanel API functions are available through a few different libraries, the two most common being the cPanel API 2 and the Universal API (UAPI). The UAPI is the preferred method but not all functions are yet available to it so you may need to still use the older API 2.
The first step to using the API is of course to figure out what function you’re trying to accomplish. Let’s say you want to create a subdomain. A quick search leads us to Subdomain::addsubdomain being the best function for the job. Let’s look over what information is available to us:
Right at the top we start with a description of what the function does and a few examples of how to call it. Whether you’re using PHP, a JSON API call remotely, or working directly from the command line on the server these examples can be incredibly useful to get you started.
Next we have a list of parameters for our API call. Anything required will be denoted there (in this case the subdomain we’re creating and the root domain to create it from) as well as other optional parameters and example values for input.
Finally we are given information on what the API call returns and examples. One of the easiest ways to try this API call is using the command line example since you can do that by simply opening SSH in terminal and pasting the command after logging in. Here is the example code provided by cPanel for the command line function (cPanel examples include both required and optional parameters):
cpapi2 –user=username SubDomain addsubdomain domain=subdomain rootdomain=example.com dir=%2Fpublic_html%2Fdirectory_name disallowdot=1
after modifying it with options related to a test account we execute it like so:
Success! If we go back to our cPanel interface now we’ll see it has been created as well
Now you can start to see how powerful this can be to automate tasks where a large number of accounts might need to be created or when you want to manage a domain externally. Continue to explore the functions and ways that cPanel provides access to their services via APIs.