How to Use cPanel’s API for Automation

From: Features and How-Tos Last Updated: 9 months ago

 

cPanel’s API allows you to automate various tasks, making server management more efficient and reducing the need for manual intervention. Here’s a guide on how to use cPanel’s API for automation.

Prerequisites

Ensure you have access to cPanel and basic knowledge of scripting languages like PHP, Python, or Perl.

Accessing the API

  1. Log in to cPanel: Use your cPanel credentials to log in to your cPanel account.

  2. Locate API Tools: In the "Software" section of the cPanel dashboard, click on the "APIs" icon.

Using the UAPI (User API)

The UAPI allows you to access and modify cPanel account data and settings.

  1. Access UAPI: Use the command-line utility uapi to interact with the UAPI.

    bash
    /usr/local/cpanel/bin/uapi
    
  2. List Domains: To list all domains associated with a cPanel account, use the following command:

    bash
    uapi DomainInfo list_domains --user=$USERNAME
    

    Replace $USERNAME with the cPanel username.

  3. Create a New Domain: To create a new domain, use the following command:

    bash
    uapi DomainInfo create_domain --user=$USERNAME --domain=$DOMAIN --rootdomain=$ROOTDOMAIN
    

    Replace $USERNAME, $DOMAIN, and $ROOTDOMAIN with the appropriate values.

Using the WHM API

The WHM API allows you to perform server administration tasks.

  1. Access WHM API: Use the command-line utility whmapi1 to interact with the WHM API.

    bash
    /usr/local/cpanel/bin/whmapi1
    
  2. List Users: To list all users on the server, use the following command:

    bash
    whmapi1 list_users
    
  3. Create a New User: To create a new user, use the following command:

    bash
    whmapi1 createacct --user=$USERNAME --password=$PASSWORD --domain=$DOMAIN
    

    Replace $USERNAME, $PASSWORD, and $DOMAIN with the appropriate values.

Automating Tasks with Scripts

  1. Write a Script: Use a scripting language like PHP or Python to automate tasks. For example, a simple PHP script to list domains might look like this:

    php
    <?php
    $username = 'your_username';
    $command = "/usr/local/cpanel/bin/uapi DomainInfo list_domains --user=$username";
    $output = shell_exec($command);
    echo $output;
    ?>
    
  2. Schedule Tasks: Use cron jobs to schedule your scripts to run at specific times. For example, to run a script daily at midnight, add the following cron job:

    bash
    0 0 * * * /path/to/your/script.php
    

Best Practices

  1. Error Handling: Implement error handling in your scripts to manage any issues that arise during execution.

  2. Security: Ensure that your scripts are secure and that sensitive information, such as API keys and passwords, is protected.

  3. Logging: Add logging to your scripts to keep track of their execution and any errors that occur.

 

Using cPanel’s API for automation allows you to streamline server management tasks, save time, and reduce the risk of human error. By following the steps outlined above, you can effectively automate various tasks and improve your server administration process.

Bottom ad position