Examples
General info
Here are some of the examples on how the API could be queried. While over the years directadmin has old and new endpoints, you should always prioritize the new ones. Pririoty list would be the follwoing:
- New API. Currently working to port all the functionality there. If a function exists in new API - it's autoincluded into the swagger documentation. It's the recommended method and should always be prioritized
- Legacy API. Either fully legacy codebase or something ported just be be backwards compatible. Most of the endpoints listed manually in this section. You should use it ONLY if you cannot find the wanted action in new API.
- Debugged action. If you cannot find documentation about some endpoint, you can use debug mode to discover it. However, If documentation doesn't have info about this endpoint - it can change anytime. So it should be used for temporary actions when you want to automate something. This should be considered as unstable for production use.
Please note, that you do not need to generate new access details for each user to execute user level actions, you can use administrative account with impersonation.
List user databases
User level action.
If you want to make an API call from a script or shell, without using a more complicated class, curl can do it for you. Here is a sample request to make an API request to a mysql endpoint to get the database data:
Curl one-liner
$ curl -s --user "demouser:demo" "https://demo.directadmin.com:2222/api/db-show/databases"
[{"database":"demouser_demo","sizeBytes":0,"userCount":2,"tableCount":0,"definerIssues":0},{"database":"demouser_wpTQMTcNn","sizeBytes":753664,"userCount":2,"tableCount":12,"definerIssues":0}]
this gives json data output.
You can pipe the json data to jq
program to select the specific data that you need. For example, gather the database names of the user:
$ curl -s --user "demouser:demo" "https://demo.directadmin.com:2222/api/db-show/databases" | jq .[].database -r
demouser_demo
demouser_wpTQMTcNn
Next examples will be regular scripts instead of oneliners.
Bash
#!/bin/bash
server=https://demo.directadmin.com:2222
user=demouser
password=demo
endpoint=/api/db-show/databases
json_data=$(curl -s --user "${user}:${password}" "${server}${endpoint}")
data=$(echo "${json_data}" | jq .[].database -r)
for item in ${data}; do
echo "Database: ${item}"
done
PHP
<?php
$server = 'https://demo.directadmin.com:2222';
$user = 'demouser';
$password = 'demo';
$endpoint = '/api/db-show/databases';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $server . $endpoint);
curl_setopt($curl, CURLOPT_USERPWD, $user . ":" . $password);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
$response_json = curl_exec($curl);
curl_close($curl);
$data = json_decode($response_json, true);
foreach ($data as $item){
if (isset($item['database'])) {
echo "Database: {$item['database']}\n";
};
}
Python
#!/usr/bin/env python3
import requests
server = "https://demo.directadmin.com:2222"
user = "demouser"
password = "demo"
endpoint = "/api/db-show/databases"
response = requests.get(server + endpoint, auth=(user, password))
json_data = response.json()
for key in json_data:
print("Database: %s" % key['database'])
List all users (legacy API)
Admin level action.
Do not forget that legacy API gives url encoded values by default. So in order to have json in response, we need to add ?json=yes
in url query.
Bash
#!/bin/bash
server=https://demo.directadmin.com:2222
user=demoadmin
password=demo
endpoint="/CMD_API_SHOW_ALL_USERS?json=yes"
json_data=$(curl -s --user "${user}:${password}" "${server}${endpoint}")
data=$(echo "${json_data}" | jq .[])
for item in ${data}; do
echo "User: ${item}"
done
PHP
<?php
$server = 'https://demo.directadmin.com:2222';
$user = 'demoadmin';
$password = 'demo';
$endpoint = '/CMD_API_SHOW_ALL_USERS?json=yes';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $server . $endpoint);
curl_setopt($curl, CURLOPT_USERPWD, $user . ":" . $password);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
$response_json = curl_exec($curl);
curl_close($curl);
$data = json_decode($response_json, true);
foreach ($data as $item){
if (isset($item)) {
echo "User: {$item}\n";
};
}
Python
#!/usr/bin/env python3
import requests
server = "https://demo.directadmin.com:2222"
user = "demoadmin"
password = "demo"
endpoint = "/CMD_API_SHOW_ALL_USERS?json=yes"
response = requests.get(server + endpoint, auth=(user, password))
json_data = response.json()
for key in json_data:
print("User: %s" % key)
Install a new wordpress
User level action
Bash
#!/bin/bash
server=https://demo.directadmin.com:2222
user=demouser
password=demo
endpoint="/api/wordpress/install-quick"
wp_json=$(cat <<- EOF
{
"adminEmail": "admin@somedomain.tld",
"adminName": "MyName",
"adminPass": "Sup3rStr0ngPas5w0rd",
"filePath": "domains/demouser.directadmin.com/public_html",
"title": "My New Website!"
}
EOF
)
curl -s --user "${user}:${password}" "${server}${endpoint}" --request "POST" --data "${wp_json}"
PHP
<?php
$server = 'https://demo.directadmin.com:2222';
$user = 'demouser';
$password = 'demo';
$endpoint="/api/wordpress/install-quick"
$wp_json = <<<JSON
{
"adminEmail": "admin@somedomain.tld",
"adminName": "MyName",
"adminPass": "Sup3rStr0ngPas5w0rd",
"filePath": "domains/demouser.directadmin.com/public_html",
"title": "My New Website!"
}
JSON;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $server . $endpoint);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $wp_json);
curl_setopt($curl, CURLOPT_USERPWD, $user . ":" . $password);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
if(curl_exec($curl) === false)
{
echo 'Curl error: ' . curl_error($curl);
}
curl_close($curl);
Python
#!/usr/bin/env python3
import requests
server = "https://demo.directadmin.com:2222"
user = "demouser"
password = "demo"
endpoint="/api/wordpress/install-quick"
wp_json = {
"adminEmail": "admin@somedomain.tld",
"adminName": "MyName",
"adminPass": "Sup3rStr0ngPas5w0rd",
"filePath": "domains/demouser.directadmin.com/public_html",
"title": "My New Website!"
}
response = requests.post(server + endpoint, auth=(user, password), json=wp_json)
print(response.text)
Add a new user (legacy API)
Admin level action
Code examples for adding a new user to a DirectAdmin server
Bash
#!/bin/bash
server=https://demo.directadmin.com:2222
user=demoadmin
password=demo
endpoint="/CMD_ACCOUNT_USER?json=yes"
new_user_json=$(cat <<- EOF
{
"username":"newusername",
"email":"user@email.tld",
"passwd":"Sup3rStr0ngPas5w0rd",
"passwd2":"Sup3rStr0ngPas5w0rd",
"domain":"userdomain.tld",
"package":"demouserpackage",
"ip":"138.201.187.115",
"notify":"yes",
"json":"yes",
"add":"yes",
"action":"create"
}
EOF
)
curl -s --user "${user}:${password}" "${server}${endpoint}" --request "POST" --data "${new_user_json}"
PHP
<?php
$server = 'https://demo.directadmin.com:2222';
$user = 'demoadmin';
$password = 'demo';
$endpoint = '/CMD_ACCOUNT_USER?json=yes';
$new_user_json = <<<JSON
{
"username":"newusername",
"email":"user@email.tld",
"passwd":"Sup3rStr0ngPas5w0rd",
"passwd2":"Sup3rStr0ngPas5w0rd",
"domain":"userdomain.tld",
"package":"demouserpackage",
"ip":"138.201.187.115",
"notify":"yes",
"json":"yes",
"add":"yes",
"action":"create"
}
JSON;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $server . $endpoint);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $new_user_json);
curl_setopt($curl, CURLOPT_USERPWD, $user . ":" . $password);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
if(curl_exec($curl) === false)
{
echo 'Curl error: ' . curl_error($curl);
}
curl_close($curl);
Python
#!/usr/bin/env python3
import requests
server = "https://demo.directadmin.com:2222"
user = "demoadmin"
password = "demo"
endpoint = "/CMD_ACCOUNT_USER?json=yes"
new_user_json = {
"username":"newusername",
"email":"user@email.tld",
"passwd":"Sup3rStr0ngPas5w0rd",
"passwd2":"Sup3rStr0ngPas5w0rd",
"domain":"userdomain.tld",
"package":"demouserpackage",
"ip":"138.201.187.115",
"notify":"yes",
"json":"yes",
"add":"yes",
"action":"create"
}
response = requests.post(server + endpoint, auth=(user, password), json=new_user_json)
print(response.text)
Discovering an endpoint
All the new endpoints are actually documented using swagger, so none of the new endpoints should be missing documentation. However, if you need some action that your GUI does but it's not in new API nor in OLD api documentation - a browser with developer tools could be used to discover the endpoint.
Evolution skin could be treated as a browser client to a DirectAdmin backend API. Any action that you do in your browser - sends a request to a backend to do something.
Lets assume we could not find the email account creation page.
- Navigate to the exact part right before pressing a button to send a request and press F12 to open browser developer tools. Open network tab and optionally you can press the "trash" icon to clean out the previous requests.
- Press a button to send the request. You'll see that one of the requests is POST request - press on it to open more info. On the right side - the url endpoint is visible.
- Choose the "Request" tab, to see the "json" data that was sent. You can toggle the "Raw" switch on the right to switch to a copyable in that window.
- That's it, you have the endpoint and the json data to send. Craft it with the examples above to automate your actions.