Child pages
  • cPanel API 2 Functions - SubDomain::addsubdomain
Skip to end of metadata
Go to start of metadata

Description

This function creates a subdomain.


Warning:

We strongly recommend that you use UAPI instead of cPanel API 2. However, no equivalent UAPI function exists.

Important:

In cPanel & WHM version 76 and later, when you disable the WebServer role, the system disables this function. For more information, read our How to Use Server Profiles documentation.

Examples


 WHM API (JSON)
https://hostname.example.com:2087/cpsess##########/json-api/cpanel?cpanel_jsonapi_user=user&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_func=addsubdomain&domain=subdomain&rootdomain=example.com&dir=%2Fpublic_html%2Fdirectory_name&disallowdot=1


Note:

 For more information, read our Calls from the WHM API documentation.

 LiveAPI PHP Class
$cpanel = new CPANEL(); // Connect to cPanel - only do this once.
 
// Create a subdomain.
$addsubdomain = $cpanel->api2(
    'SubDomain', 'addsubdomain', 
        array(
        'domain'                => 'subdomain',
        'rootdomain'            => 'example.com',
        'dir'              		=> '/public_html/directory_name',
        'disallowdot'           => '1',
    )
);


Note:

For more information, read our Guide to the LiveAPI System.

 

 LiveAPI Perl Module
my $cpliveapi = Cpanel::LiveAPI->new(); # Connect to cPanel - only do this once.
 
# Create a subdomain.
my $addsubdomain = $cpliveapi->api2(
    'SubDomain', 'addsubdomain',
 	{
        'domain'                => 'subdomain',
        'rootdomain'            => 'example.com',
        'dir'              		=> '/public_html/directory_name',
        'disallowdot'           => '1',
    } 

);


Note:

For more information, read our Guide to the LiveAPI System.

 

 cPanel Tag System (deprecated)


Warnings:

  • In cPanel & WHM version 11.30 and later, cPanel tags are deprecated. We strongly recommend that you only use the LiveAPI system to call the cPanel APIs. Examples are only present in order to help developers move from the old cPanel tag system to our LiveAPI.
  • cPanel API 2 calls that use cPanel tags vary in code syntax and in their output.
  • For more information, read our Deprecated cPanel Tag Usage documentation.

 Command Line
cpapi2 --user=username SubDomain addsubdomain domain=subdomain rootdomain=example.com dir=%2Fpublic_html%2Fdirectory_name disallowdot=1


Notes:

  • You must URI-encode values.
  • username represents your account-level username.
  • You must run the --user=username command.
  • For more information and additional output options, read our Guide to cPanel API 2 documentation or run the cpapi2 --help command.
  • If you run CloudLinux™, you must use the full path of the cpapi2 command:

    /usr/local/cpanel/bin/cpapi2

 Output (JSON)
{
  "cpanelresult": {
    "apiversion": 2,
    "func": "addsubdomain",
    "data": [
      {
        "reason": "The subdomain "subdomain.example.com" has been added.",
        "result": 1
      }
    ],
    "event": {
      "result": 1
    },
    "module": "SubDomain"
  }
}


Note:

Use cPanel's API Shell interface (cPanel >> Home >> Advanced >> API Shell) to directly test cPanel API calls.

Parameters

ParameterTypeDescriptionPossible valuesExample
domainstring

Required

The subdomain name to create.

A valid subdomain name.

subdomain
rootdomainstring

Required

The domain on which to create the new subdomain.

A domain that already exists on the cPanel account.example.com
canoffBoolean

Whether to create a wildcard subdomain.

This value defaults to 0.

  • 1 — Create a wildcard subdomain.
  • 0 — Create a normal subdomain.
0
dirstring

The subdomain's document root within your home directory.

This parameter's default depends on the server's settings:

  • cPanel & WHM version 58 and higher:
  • cPanel & WHM version 56 and earlier:
    • If the public_html subdomains only value is On, the parameter defaults to the public_html/subdomain_name path, where subdomain_name is the subdomain's name .
    • If the public_html subdomains only value is Off, the parameter defaults to the subdomain_name path, where subdomain_name is the subdomain's name.

A valid directory path, relative to the user's home directory.

Note:

You cannot use the following directories as a document root:
 Click here to expand...
  • The account's home directory itself ( / )
  • Directories outside of the account's home directory ( ./ and ../ )
  • .cpanel
  • .trash
  • etc
  • mail
  • ssl
  • tmp
  • logs
  • .cphorde
  • .spamassassin
  • .htpasswds
  • var
  • cgi-bin
  • .ssh
  • perl5

/public_html/directory_name
disallowdotBoolean

Whether to remove the dots (.) from the domain value.

This value defaults to 0.

  • 1 — Remove dots from the domain.
  • 0 — Do not remove dots.
1

Returns

ReturnTypeDescriptionPossible valuesExample
reasonstring

A message of success or a reason for failure.

A string that describes the success or error.

This is an error message.
resultBoolean

Whether the function succeeded.

  • 1 — The function succeeded.
  • 0 — The function failed.
1