Automating cPanel Account Provisioning with WHM API 2: A Practical Guide
Learn how to automate cPanel account provisioning with WHM API 2. This guide covers token creation, a Bash script to create an account, DNS, and SSL, plus trade‑offs and verification steps.
23 Jul 2026, 05:48 UTC

Why Automate cPanel?
Managing dozens of cPanel accounts by hand is tedious and error‑prone. With WHM API 2 you can create, modify, and delete accounts, configure DNS, and set up mailboxes all from code. This blog walks through a real‑world example: automatically provisioning a new user with a custom domain, DNS zone, and SSL certificate.
Getting Started: Token Creation
All API calls require a token. Log into WHM as root or a reseller, navigate to WHM API Tokens, and click Create New Token. Give the token a descriptive name (e.g., automation‑token), set an IP restriction if desired, and specify the minimal permissions needed – for this example we’ll enable account, dns, and ssl scopes. Copy the token value; you’ll use it in the Authorization header.
Example: Token Generation via WHM
https://yourhost:2087/cpsessXXXXXX/api2/json/createtoken
POST payload:
{
"name": "automation-token",
"permissions": {
"account": "*",
"dns": "*",
"ssl": "*"
},
"ip": "192.0.2.10"
}
Note: Replace cpsessXXXXXX with a valid session ID or use https://yourhost:2087/json-api/createtoken with basic auth.
The Core Task: Create an Account with DNS and SSL
Below is a minimal Bash script that performs the following steps:
- Creates a new cPanel account.
- Adds a DNS zone for the user’s domain.
- Requests an SSL certificate via the cPanel API.
- Verifies the responses.
Prerequisites
- cPanel & WHM version 11.80 or newer (API 2 stable).
- curl installed on the machine running the script.
- HTTPS access to
https://yourhost:2087from the script host. - Token with
account,dns, andsslscopes.
Script
#!/usr/bin/env bash
set -euo pipefail
# Configuration
WHM_HOST="yourhost.example.com"
TOKEN="YOUR_API_TOKEN"
DOMAIN="example.com"
USER="john"
PASS="StrongPass!23"
EMAIL="[contact removed]"
# Helper: API call wrapper
api_call() {
local endpoint=$1
local payload=$2
curl -sS \
"https://${WHM_HOST}:2087/cpsessXXXXXX/api2/json/${endpoint}" \
-H "Authorization: WHM root:${TOKEN}" \
-H "Content-Type: application/json" \
--data-raw "${payload}"
}
# 1. Create account
CREATE_ACCT_PAYLOAD=$(cat <
Replace cpsessXXXXXX with a real session ID or use the json-api endpoint with basic authentication. The script uses jq for JSON parsing; install it with apt-get install jq or equivalent.
Trade‑Offs and Limitations
- Rate Limits: cPanel enforces 1000 API calls per hour per token. For high‑volume provisioning, rotate tokens or batch requests.
- Permission Scope: Tokens are long‑lived. Store them securely (e.g., HashiCorp Vault, AWS Secrets Manager). Use least‑privilege scopes to minimize damage if compromised.
- Legacy Endpoints: Some older email or backup APIs are deprecated. Stick to the documented API 2 endpoints to avoid future breakage.
- Network Accessibility: The script must reach WHM over HTTPS. Ensure firewalls allow outbound 2087 traffic.
Verification Checklist
| Check | How to Verify |
|---|---|
| Account exists | Log into cPanel as the new user; domain appears in the dashboard. |
| DNS zone created | Run dig ${DOMAIN} NS and confirm NS records match. |
| SSL installed | Open https://${DOMAIN} in a browser; verify a valid certificate. |
| API responses OK | Check result field equals 1 in JSON payloads. |
Next Steps
With the basics in place you can:
- Automate bulk user onboarding by looping over a CSV of user data.
- Integrate with your CI/CD pipeline to spin up temporary environments.
- Use the
modifyacctanddeleteacctendpoints for lifecycle management.
Remember to monitor token usage and rotate them periodically. Once you’re comfortable, consider wrapping the API calls in a higher‑level language library (Python requests, Node.js axios, etc.) for richer error handling.
Conclusion
WHM API 2 turns cPanel from a manual interface into a programmable platform. By securing a token, scripting account creation, and handling responses carefully, you can reduce administrative overhead, eliminate human error, and scale your hosting operations reliably.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.