
DirectAdmin API is a set of commands that lets you control hosting accounts through code or scripts instead of manually clicking through the control panel. It's ideal for server administrators or anyone needing to automate hosting tasks. This guide walks you through using DirectAdmin API step-by-step, with practical PHP scripts and cURL examples you can use immediately.
What Is DirectAdmin API?
DirectAdmin API is an interface that communicates with DirectAdmin control panel via HTTP/HTTPS requests without accessing the web interface directly. This lets you write scripts to automate repetitive tasks, reduce errors, and integrate with your own custom applications.
Common use cases for DirectAdmin API include: automatically creating subdomains for new customers, scheduling database backups, provisioning email accounts, restarting services, and managing domains — all without manually logging into the control panel.
DirectAdmin API Base URL and Authentication
All DirectAdmin API endpoints share a common base URL structure:
https://[hostname]:[port]/api/[endpoint]
The default port is 2222 (HTTPS), though some hosting providers use alternative ports like 2087, 8443, or 443.
You can authenticate in two ways: using username/password or a login key (token), which is more secure.
Example Base URL: If your server is `hosting.example.com` on port 2222 and you want to create a user, the URL would be: `https://hosting.example.com:2222/api/admin?action=user_create`
Getting Started: Obtain a Login Key (API Token) from DirectAdmin
Before using the API, you need a login key — a secure token that acts as a substitute for your password. To retrieve your login key:
- Log in to DirectAdmin control panel with your admin account
- Go to Administrator → Settings
- Look for the Login Keys or API Token section
- Copy an existing key or create a new one
- Store this key securely and never share it with others
Information You'll Need Before Making API Calls
Gather the following details before writing your script:
- Hostname: Server name or domain (e.g., `hosting.example.com` or IP address)
- Port: DirectAdmin port number (typically 2222)
- Admin Username: Your admin account name (e.g., `admin`)
- Login Key: The API token you prepared above
- Target User Account: The hosting account you want to manage (e.g., `mysite1`, `mysite2`)
Common API Endpoints and Basic Usage
DirectAdmin API has numerous endpoints. Here are the most frequently used ones:
| Action | Endpoint | Method |
|---|---|---|
| Get user information | /api/user?action=list |
POST |
| Create a subdomain | /api/admin?action=domain_add |
POST |
| Create email account | /api/admin?action=email_add |
POST |
| Modify password | /api/admin?action=user_modify |
POST |
| Delete a user | /api/admin?action=user_delete |
POST |
| Perform backup | /api/admin?action=backup |
POST |
Example PHP Script: Using the DirectAdmin API
Here's a PHP script that creates a subdomain using DirectAdmin API:
<?php
// DirectAdmin configuration
$hostname = 'hosting.example.com';
$port = 2222;
$admin_user = 'admin';
$login_key = 'YOUR_API_KEY_HERE'; // Replace with your actual login key
$username = 'mysite1'; // Target user account
// API endpoint URL
$url = "https://$hostname:$port/api/admin?action=domain_add";
// Request parameters
$data = http_build_query([
'user' => $username,
'domain' => 'blog.mysite.com', // subdomain to create
'ip' => '1.2.3.4', // IP address (optional)
]);
// Initialize cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$admin_user:$login_key");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // only for testing!
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Send request
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// Display result
if ($http_code === 200) {
echo "✅ Subdomain created successfully\n";
echo $response;
} else {
echo "❌ Error occurred. HTTP $http_code\n";
echo $response;
}
?>
⚠️ Important: The example includes `CURLOPT_SSL_VERIFYPEER => false` for testing only. In production, set this to true or use a valid certificate.
Example cURL Command from Terminal
To test the API directly from the terminal (via SSH), use this cURL command:
curl -X POST https://admin:[email protected]:2222/api/admin?action=domain_add \ -d "user=mysite1&domain=blog.mysite.com"
On success, you'll get a response like:
output=success text=Domain Added
Creating Email Accounts Automatically
Here's an example of creating a new email account via API:
<?php
$hostname = 'hosting.example.com';
$port = 2222;
$admin_user = 'admin';
$login_key = 'YOUR_API_KEY_HERE';
$username = 'mysite1';
$url = "https://$hostname:$port/api/admin?action=email_add";
$data = http_build_query([
'user' => $username,
'domain' => 'mysite.com',
'email' => '[email protected]', // email to create
'passwd' => 'StrongPass@123', // password
'passwd2' => 'StrongPass@123', // confirm password
'quota' => '100M', // quota (e.g., 100MB)
]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$admin_user:$login_key");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>
Common Errors and Troubleshooting
When using DirectAdmin API, you might encounter various errors. Here are the most common ones and their solutions:
| Error Message | Cause | Solution |
|---|---|---|
| Invalid login | Wrong username/password/key | Verify username, login key, and port are correct |
| User already exists | Username being created is already in use | Choose a different username |
| Domain already exists | Subdomain is already set up | Use a different subdomain name |
| Connection refused | Cannot reach the server | Check hostname, port, and firewall settings |
| SSL verification failed | Certificate issue | Set SSL_VERIFYPEER to false (testing only) or use valid certificate |
Best Practices for Secure DirectAdmin API Usage
To use DirectAdmin API securely and reliably, follow these guidelines:
- Always use HTTPS: Never use plain HTTP. Your API key must be encrypted in transit.
- Secure your API key: Never hardcode login keys in source files that others can see. Use environment variables or separate config files.
- Check the response: Don't assume an API call succeeded just because HTTP 200 was returned. Verify the response body contains `output=success`.
- Implement error handling: Handle connection failures, timeouts, invalid input, and other error conditions gracefully.
- Set timeouts: Configure cURL with a reasonable timeout (e.g., 30 seconds) to prevent requests from hanging indefinitely.
Finding More DirectAdmin API Documentation
DirectAdmin maintains comprehensive official documentation covering all endpoints, parameters, and example responses at `https://www.directadmin.com/features.php?id=228` and `https://docs.directadmin.com/`. The API documentation section is particularly thorough.
Hosting with Full DirectAdmin API Support
AsiaGB Hosting runs DirectAdmin control panel with full API support and login key authentication. Features include SSD storage, 99% uptime, and starting from 500 THB/year.
View Hosting Plans