Skip to content

Rotating WHM API tokens without breaking your automation

A WHM API token rotation runbook — generate scoped tokens, roll them on a schedule, audit usage, and avoid the 401 storm that takes down billing or provisioning.

You inherit a WHM server with two API tokens named billing and provisioning. Neither has an owner, neither has been rotated in three years, and one of them is hard-coded into a cron job somewhere on a billing server you also inherited. You want to rotate both — but the moment you regenerate provisioning, your customer-onboarding pipeline starts returning HTTP 401 and the provisioning queue backs up. Token rotation on WHM is not “click regenerate, paste the new value, done.” It’s a coordinated operation across every integration that holds the secret, with overlap windows, audit trails, and a rollback path that doesn’t require a panicked vendor call.

What’s happening

A WHM API token is a long-lived bearer credential scoped to a specific WHM user (usually root or a reseller). It authorizes calls to /json-api/ and /cpsess.../json-api/ endpoints with whatever ACLs the owning user has. WHM does not version tokens — when you regenerate, the previous secret stops working immediately. Any integration still holding the old value will get 401 Access denied on the next call. The way out is to create a new token alongside the old one, migrate every consumer to the new token, then revoke the old token. That requires you to know who holds the secret today, which is usually the question nobody can answer until the rotation fails.

WHM supports scoping tokens to a subset of ACLs, which is the second piece nobody uses. A token labeled billing does not need kill-process or restart-service — it needs list-accts, accountsummary, and the suspended-account ACLs. A provisioning token needs create-acct, modify-acct, passwd, and the package ACLs. Scoping limits the blast radius of a leaked token and makes rotation safer because you can verify the new token works for the exact endpoints the consumer calls.

How to verify

List existing tokens and their scopes:

whmapi1 --output=jsonpretty api_token_list_tokens | jq '.data.tokens[] | {name: .name, has_full_access: .has_full_access, acls: .acls, last_used: .last_used}'

If has_full_access is 1, the token is effectively a root credential — that’s the one to scope down before rotating. last_used (Unix epoch) tells you whether anyone still calls it.

Look for token usage in WHM’s access log to identify which IP is calling which token:

grep -a 'cpanel_jsonapi_user=root' /usr/local/cpanel/logs/access_log | \
  awk '{print $1, $7}' | sort -u | head -50

Cross-reference with /var/cpanel/logs/api_tokens_log (if enabled):

tail -n 200 /var/cpanel/logs/api_tokens_log | jq -c '{ts: .timestamp, ip: .remote_ip, token: .token_name, endpoint: .endpoint}'

If api_tokens_log does not exist, enable structured token logging before you rotate anything — flying blind during a rotation is how you take down billing:

whmapi1 set_tweaksetting key=enable_api_log value=1
whmapi1 set_tweaksetting key=enable_api_tokens_log value=1

Confirm a candidate new token actually works for the endpoints the consumer uses, with curl from the consumer’s host:

curl -sH "Authorization: whm root:NEW_TOKEN_VALUE" \
  "https://whm.example.com:2087/json-api/listaccts?api.version=1" | jq '.metadata.result'

result: 1 means the token is accepted and authorized. result: 0 with reason: Access denied means the ACL is too narrow; expand it before cutting over.

The fix

The rotation runbook is six phases. Plan a 7-day window per token — long enough that even monthly cron jobs hit the new token before you revoke the old one.

Phase 1 — inventory. Document every integration that holds a WHM token. For each one, capture: hostname/service, IP, token name, ACL set in use, owner, rotation cadence, where the secret is stored (env var, vault path, config file). Stop here if you cannot find an owner; you don’t rotate a token whose consumer you can’t identify, because you can’t migrate it and you can’t watch for failure.

Phase 2 — generate the replacement. Create the new token with explicit ACLs (never --has-full-access=1 unless you’ve proven the consumer needs it). For a provisioning consumer:

whmapi1 api_token_create_token \
  token_name=provisioning-2026-05 \
  acl-1=create-acct \
  acl-2=modify-acct \
  acl-3=passwd \
  acl-4=remove-acct \
  acl-5=suspend-acct \
  acl-6=unsuspend-acct \
  acl-7=list-accts \
  acl-8=list-pkgs \
  acl-9=accountsummary

WHM prints the token value once. Pipe it directly into your secret store; do not write it to disk:

TOKEN=$(whmapi1 api_token_create_token token_name=provisioning-2026-05 acl-1=create-acct ... | jq -r '.data.token')
vault kv put secret/whm/provisioning/2026-05 value="$TOKEN"
unset TOKEN

For a read-only billing/reporting consumer:

whmapi1 api_token_create_token \
  token_name=billing-readonly-2026-05 \
  acl-1=list-accts \
  acl-2=accountsummary \
  acl-3=list-pkgs \
  acl-4=show-bandwidth-usage

Phase 3 — deploy to the consumer. Push the new token into the consumer’s secret store, update its config to reference the new path, and restart only that consumer. Both tokens are now valid simultaneously. This is the overlap window — neither secret is yet revoked.

Phase 4 — observe. Watch the API tokens log for traffic on both names:

tail -F /var/cpanel/logs/api_tokens_log | \
  jq -c 'select(.token_name | test("provisioning")) | {ts: .timestamp, ip: .remote_ip, token: .token_name, endpoint: .endpoint}'

You should see the new token receiving traffic from the consumer’s IP within minutes. The old token should taper off. If the old token is still seeing calls 48 hours later, somebody — usually a forgotten cron job or a second consumer nobody documented in Phase 1 — is still holding it.

A helper that prints the last 24 hours of traffic per token name:

#!/usr/bin/env bash
# /usr/local/bin/whm-token-usage
LOG=/var/cpanel/logs/api_tokens_log
SINCE=$(date -d '24 hours ago' +%s)
jq -r --argjson since "$SINCE" \
  'select(.timestamp >= $since) | "\(.token_name) \(.remote_ip)"' "$LOG" | \
  sort | uniq -c | sort -rn

Phase 5 — revoke the old token. Once the old token has been quiet for at least 72 hours and you’ve manually triggered any monthly jobs you know about, revoke it:

whmapi1 api_token_revoke_token token_name=provisioning

Then immediately verify the consumer is still healthy. Hit your provisioning queue’s /health endpoint, run a no-op create-account call against a staging customer, watch the metrics.

Phase 6 — audit trail. Log the rotation event somewhere durable. A minimal change-log entry:

event: whm_token_rotation
date: 2026-05-12
operator: ops-bot
old_token: provisioning
new_token: provisioning-2026-05
consumers_migrated:
  - svc-provisioner-01 (10.40.0.21)
  - svc-billing-export (10.40.0.34)
overlap_window: 7d
revocation_verified: true
next_rotation_due: 2026-11-12

Schedule the next rotation now — drop a calendar event or a at job 90 days from revocation. Tokens that nobody owns rotate themselves into “we’ll do it next quarter” purgatory; the only defense is a forcing function.

A reusable wrapper that combines create + vault-store + overlap-window-tracking:

#!/usr/bin/env bash
# /usr/local/bin/whm-rotate-token
set -euo pipefail

TOKEN_BASE_NAME="${1:?usage: whm-rotate-token <base-name> <acl1,acl2,...> <vault-path>}"
ACLS="${2:?}"
VAULT_PATH="${3:?}"

DATE_TAG=$(date +%Y-%m)
NEW_NAME="${TOKEN_BASE_NAME}-${DATE_TAG}"

# Build ACL args
i=1
ACL_ARGS=()
IFS=',' read -ra A <<< "$ACLS"
for acl in "${A[@]}"; do
  ACL_ARGS+=("acl-${i}=${acl}")
  i=$((i+1))
done

echo "==> Creating WHM token: $NEW_NAME"
RESP=$(whmapi1 --output=jsonpretty api_token_create_token \
  token_name="$NEW_NAME" "${ACL_ARGS[@]}")

TOKEN=$(echo "$RESP" | jq -r '.data.token')
if [[ -z "$TOKEN" || "$TOKEN" == "null" ]]; then
  echo "ERROR: token create failed"
  echo "$RESP" | jq -r '.metadata.reason'
  exit 1
fi

echo "==> Storing in vault at $VAULT_PATH"
vault kv put "$VAULT_PATH" value="$TOKEN" created_at="$(date -u +%FT%TZ)" \
  token_name="$NEW_NAME" acls="$ACLS"

echo "==> Token created and stored. Overlap window starts now."
echo "    Old tokens with prefix '$TOKEN_BASE_NAME' should be revoked in 7d."
echo
echo "    Watch traffic:"
echo "    /usr/local/bin/whm-token-usage | grep $TOKEN_BASE_NAME"
echo
echo "    Revoke old:"
echo "    whmapi1 api_token_revoke_token token_name=<old-name>"

Two rotation rules that protect you in production: never rotate the root-equivalent token and the per-consumer scoped tokens in the same window — if something breaks, you want to know which class of credential moved. And never delete the old token in the same change-window as the new one; the overlap is the safety net, not optional polish.

For WHM fleets where token rotation, scoping, and audit are a permanent operational concern, our team runs them under Managed Cloud Ops with monitoring on both api_tokens_log and the /json-api/ access log — or book a security review of your existing token hygiene.