How to set up MySQL database through cPanel XML API

I’m working on automating database creation through cPanel’s XML API but running into issues. When I try to execute my script, I keep getting an error about a missing parameter. Here’s what I’m currently using:

$username = 'myuser';
$password = 'mypass';
$host = 'example.com';
$api_client = new \xmlapi($host);
$api_client->set_output('json');
$api_client->set_port(2083);
$api_client->password_auth($username, $password);
$api_client->set_debug(1);
$response = $api_client->api1_query($username, 'Mysql', 'adddb', array($database_name));
print_r($response);

The error message I’m getting says:

{"cpanelresult":{"apiversion":"2","error":"The cpanel_jsonapi_module parameter is required.","data":{"reason":"The cpanel_jsonapi_module parameter is required.","result":"0"},"type":"text"}}

What am I missing in my API call? Any help would be great.

Had this exact problem last month with cPanel’s XML API. You’re mixing API versions - using api1_query but setting JSON output, which needs different parameter handling. I switched to UAPI and it fixed everything. Try this instead: $response = $api_client->uapi('Mysql', 'create_database', array('name' => $database_name)); UAPI handles JSON responses way better and you don’t need that cpanel_jsonapi_module parameter. Also check if your hosting provider actually lets you create databases through the API - some block it even with proper auth.

You’re using the wrong API method for JSON output. When you call set_output('json') with api1_query, cPanel needs the cpanel_jsonapi_module parameter explicitly defined. I hit this same issue migrating our automated provisioning system. Easy fix: just remove the set_output('json') line if you’re sticking with api1_query - it defaults to a usable format anyway. Or switch to api2_query which handles JSON output better. Also, don’t forget the database name needs your cPanel username prefix. So if you’re ‘myuser’ wanting database ‘testdb’, pass ‘myuser_testdb’ as the parameter.

u’re using api1 but the error wants the jsonapi module param. Switch to api2 - use api2_query with module ‘Mysql’ and function ‘create_database’. Make sure u pass the db name as a proper array key like array('name' => $database_name) not just an array value.