Search K
Appearance
Appearance
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:
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.
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 -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_wpTQMTcNnNext examples will be regular scripts instead of oneliners.
#!/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
$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";
};
}#!/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'])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.
#!/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
$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";
};
}#!/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)User level action
#!/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
$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);#!/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)Admin level action
Code examples for adding a new user to a DirectAdmin server
#!/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
$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);#!/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)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.


