Skip to content

Add Domain to cPanel using cPanel API

You will first need to add a cPanel API token to your cPanel account.

Once you have a token you have a variety of options to use it. In all examples we will use the following values:

  • yourdomain.com:2083 - Your cPanel address
  • yourusername - Your cPanel username
  • yourapitoken - Your cPanel API token
  • exampledomain.com - The domain you want to add

In our examples we assume you wish to have the directory name match the domain name but you can change the dir value to any directory name you prefer.

Terminal window
curl -X POST \
-H 'Authorization: cpanel yourusername:yourapitoken' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=AddonDomain&cpanel_jsonapi_func=addaddondomain&subdomain=exampledomain.com&newdomain=exampledomain.com&ftp_is_optional=1&dir=exampledomain.com' \
'https://yourdomain.com:2083/json-api/cpanel'
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://yourdomain.com:2083/json-api/cpanel',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=AddonDomain&cpanel_jsonapi_func=addaddondomain&subdomain=exampledomain.com&newdomain=exampledomain.com&ftp_is_optional=1&dir=exampledomain.com',
CURLOPT_HTTPHEADER => array(
'Authorization: cpanel yourusername:yourapitoken',
'Content-Type: application/x-www-form-urlencoded'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>