As you familiarize yourself with all that is available in cPanel, you inevitably may decide there are opportunities to remotely work with it. For example you may want 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 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 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. There you can see that the following information is available to you:
Right at the top there is 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 there are a list of parameters for your API call. Anything required will be denoted there (in this case the subdomain you're creating and the root domain to create it from) as well as other optional parameters and example values for input.
Finally you 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 you go back to your cPanel interface now you'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. In this way you can continue to explore the functions and ways that cPanel provides access to their services via APIs.