Managing databases efficiently is crucial for web development, especially when dealing with multiple applications or environments. Automating database creation can save you time and reduce manual errors. In this blog post, we’ll explore a PHP script that automates the creation of databases and database users using the cPanel API. This guide is perfect for developers and system administrators looking to streamline their database management processes.
What is a cPanel API?
The cPanel API allows you to interact programmatically with cPanel’s features, enabling automation of various tasks such as database management, email configuration, and more. By using the API, you can perform actions that would typically require manual intervention through the cPanel interface.
Creating an API Token from cPanel
Before using the API, you need to generate an API token in cPanel for secure authentication. Here’s how you can create one:
- Log in to cPanel
- Navigate to API Tokens:
- Look for the “Security” section in cPanel.
- Click on “Manage API Tokens.”
- Create a New API Token:
- Click the “Create” button.
- Enter a name for the token (e.g., “Addon Domain Script”).
- Copy the API Token:
- After creating the token, copy it. You will need this token for authentication in your PHP script.

PHP Script to Create a Database and User
Below is a PHP script designed to automate the creation of a database, a database user, and grant the user privileges on the database. This script uses the cPanel API to perform these tasks.
<?php ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); function createDatabase($dbName, $dbUser, $dbPass) { $cpanelUser = 'your_cpanel_username'; $cpanelApiToken = 'your_cpanel_api_token'; $cpanelHost = 'your_cpanel_host'; // Automatically add the cPanel username prefix to the database and user names $dbName = $cpanelUser . '_' . $dbName; $dbUser = $cpanelUser . '_' . $dbUser; // Step 1: Create the database $query = "https://$cpanelHost:2083/json-api/cpanel?cpanel_jsonapi_user=$cpanelUser&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=MysqlFE&cpanel_jsonapi_func=createdb&db=$dbName"; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_SSL_VERIFYPEER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => array( 'Authorization: cpanel ' . $cpanelUser . ':' . $cpanelApiKey ), CURLOPT_URL => $query )); $result = curl_exec($curl); $error = curl_error($curl); $errno = curl_errno($curl); curl_close($curl); if ($error) { return array('error' => "cURL Error ($errno): $error"); } // Step 2: Create the database user $query = "https://$cpanelHost:2083/json-api/cpanel?cpanel_jsonapi_user=$cpanelUser&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=MysqlFE&cpanel_jsonapi_func=createdbuser&dbuser=$dbUser&password=$dbPass"; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_SSL_VERIFYPEER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => array( 'Authorization: cpanel ' . $cpanelUser . ':' . $cpanelApiKey ), CURLOPT_URL => $query )); $result = curl_exec($curl); $error = curl_error($curl); $errno = curl_errno($curl); curl_close($curl); if ($error) { return array('error' => "cURL Error ($errno): $error"); } // Step 3: Grant all privileges to the user on the database $query = "https://$cpanelHost:2083/json-api/cpanel?cpanel_jsonapi_user=$cpanelUser&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=MysqlFE&cpanel_jsonapi_func=setdbuserprivileges&privileges=All%20PRIVILEGES&dbuser=$dbUser&db=$dbName"; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_SSL_VERIFYPEER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => array( 'Authorization: cpanel ' . $cpanelUser . ':' . $cpanelApiKey ), CURLOPT_URL => $query )); $result = curl_exec($curl); $error = curl_error($curl); $errno = curl_errno($curl); curl_close($curl); if ($error) { return array('error' => "cURL Error ($errno): $error"); } return json_decode($result, true); } // Example usage $dbName = 'example'; // Replace with your desired database name $dbUser = 'example'; // Replace with your desired database username $dbPass = '2@@1TesT1@'; // Replace with your desired database password $response = createDatabase($dbName, $dbUser, $dbPass); echo '<pre>'; print_r($response); echo '</pre>'; if (isset($response['error'])) { echo "Error: " . $response['error']; } else { echo "Database and user created successfully!"; } ?>