How to Make a Script Work with Security Tokens in cPanel & WHM

Security is a top priority when managing web hosting environments. cPanel & WHM include built-in security measures, such as security tokens, to help prevent cross-site request forgery (CSRF) attacks. If you are developing scripts to interact with cPanel & WHM APIs or web interfaces, you need to understand how to work with these security tokens.

This guide will explain what security tokens are, why they are important, and how you can make your script work with them effectively.

What Are Security Tokens in cPanel & WHM?

Security tokens in cPanel & WHM are session-based unique identifiers appended to URLs. They are designed to:
Prevent CSRF attacks – Ensures that actions are only performed by authenticated users.
Enhance security – Restricts unauthorized access to cPanel & WHM services.
Improve session management – Protects against hijacked sessions.

Example of a Security Token in a URL:

https://yourdomain.com:2083/cpsess1234567890/frontend/paper_lantern/index.html

In this example, cpsess1234567890 is the security token. Every session generates a new token to prevent security breaches.

How to Work with Security Tokens in Your Script

When developing a script that interacts with cPanel & WHM, you need to handle security tokens correctly to ensure compatibility and avoid authentication errors.

1. Extract the Security Token from the URL

If your script makes HTTP requests to cPanel, you must ensure that each request includes the correct security token.

Example: Manually Extracting a Security Token

If you log in to cPanel and navigate to a page, you will see the security token in the URL. Your script should capture this token and use it in future requests.

2. Automatically Retrieve the Security Token for API Calls

To interact with cPanel’s API, your script should programmatically retrieve the security token before making requests. The best way to achieve this is to use API authentication methods that don’t require tokens, such as:
cPanel API Tokens
WHM Remote API Keys
cPanel & WHM’s Session Authentication Methods

3. Using API Tokens Instead of Security Tokens

A better way to authenticate API requests is to use cPanel API Tokens, which do not require security tokens.

Steps to Create an API Token in WHM:

  1. Log in to WHM as root.

  2. Navigate to Development > Manage API Tokens.

  3. Click Create a New API Token.

  4. Assign the required permissions and generate the token.

  5. Use the token in your script for authentication.

Example API Call Using an API Token

Instead of manually handling security tokens, use API authentication like this:

This approach bypasses the need for security tokens in URL-based API requests.



4. Using Session-Based Authentication for WHM API Requests

If you still need to work with security tokens, you can authenticate using WHM’s session-based authentication method.

Example: Getting the Security Token Programmatically

  1. Log in to WHM using Basic Authentication or API Tokens.

  2. Retrieve the security token from the session.

  3. Append it to your API requests.

Here’s how to do it using cURL:

curl root:yourpassword “https://yourserver.com:2087/json-api/create_user_session?api.version=1&user=cpaneluser”

This command will return a session URL that contains the security token, which you can extract and use in subsequent requests.



Best Practices for Handling Security Tokens in Scripts

🔹 Avoid hardcoding security tokens – They expire with each session. Use authentication methods instead.
🔹 Use API Tokens – The preferred method for secure and token-free authentication.
🔹 Always check authentication errors – If a request fails, verify if the security token has changed.
🔹 Use HTTPS for secure requests – Never send security-sensitive data over HTTP.

Security tokens are a crucial part of cPanel & WHM’s security framework, preventing unauthorized access and attacks. Instead of manually handling them in your scripts, it’s best to use API Tokens or session-based authentication for seamless and secure API interactions.

By following this guide, you can ensure that your scripts work smoothly with security tokens, keeping your web applications and hosting environment secure.