Can't create user API token using WHM/cPanel API

cPanel & WHM Version
112.0.7

Yuriy Revido

Member
Sep 1, 2023
9
3
3
Bali
cPanel Access Level
Reseller Owner
Hi everybody.

We're developing a 3rd part app using whm/cpanel API. We have to set up a fully automated process of creating cPanel accounts and generating API tokens to be able to trigger cpanel functions via API. We faced the issue of generating token, here are the steps we're trying to accomplish (GET):

1. That successfully creates a cpanel user (whm API):
https://{{example_hostname}}.servconfig.com:2087/json-api/createacct?api.version=1&username={{exampleuser}}&domain={{example_user}}.com

2. That successfully creates cpanel user session (whm API):
https://{{example_hostname}}.servconfig.com:2087/create_user_session ?api.version=1&user={{example_user}}&service=cpaneld

3. This is where we got stuck on trying to create an API token (cpanel API):
https://{{example_hostname}}.servconfig.com:2083/{{cpsess1234567890 - generated from previous step}}/execute/Tokens/create_full_access?name={{example_token_name}}

Trying to use this cpsess1234567890 instead of BasicAuth as there's no API token generated yet, but getting nothing (user login html) in response.

Hope for your help guys
 
Last edited by a moderator:

cPanelThomas

Developer
Feb 16, 2023
32
23
83
cPanel
cPanel Access Level
Root Administrator
While I'm not immediately sure of why this might be failing for you, there's certainly another option for generating this token. Have you tried executing this cPanel API call via WHM's API?

Certainly since the first two calls are being done WHM side, it sounds like you already have what you need on that end to authenticate and execute API calls, so why not stick with that to generate the cPanel API token instead of using create_user_session first? This also is nice in that it would be one less HTTP request to have to execute in your process.

Probably your call would look *similar* to the below:
Code:
https://some.host.test:2087/json-api/cpanel?cpanel_jsonapi_apiversion=3&cpanel_jsonapi_user=cptest&cpanel_jsonapi_module=Tokens&cpanel_jsonapi_func=create_full_access&name=testToken
Hope this helps!
 
  • Like
Reactions: cPRex

Yuriy Revido

Member
Sep 1, 2023
9
3
3
Bali
cPanel Access Level
Reseller Owner
While I'm not immediately sure of why this might be failing for you, there's certainly another option for generating this token. Have you tried executing this cPanel API call via WHM's API?

Certainly since the first two calls are being done WHM side, it sounds like you already have what you need on that end to authenticate and execute API calls, so why not stick with that to generate the cPanel API token instead of using create_user_session first? This also is nice in that it would be one less HTTP request to have to execute in your process.

Probably your call would look *similar* to the below:
Code:
https://some.host.test:2087/json-api/cpanel?cpanel_jsonapi_apiversion=3&cpanel_jsonapi_user=cptest&cpanel_jsonapi_module=Tokens&cpanel_jsonapi_func=create_full_access&name=testToken
Hope this helps!

Hi Thomas

Thanks a lot, that works and is really what we needed!

But where can I find this API doc version? I've never seen that endpoint structure mentioned somewhere in the current API docs
 
  • Like
Reactions: cPRex

cPanelThomas

Developer
Feb 16, 2023
32
23
83
cPanel
cPanel Access Level
Root Administrator
I did have a link for the relevant API docs in my earlier reply, but it did format it in a way that maybe led you to not click on it? Here it is without the (bad) preview:
Code:
https://api.docs.cpanel.net/whm/use-whm-api-to-call-cpanel-api-and-uapi/
 
  • Like
Reactions: cPRex

Yuriy Revido

Member
Sep 1, 2023
9
3
3
Bali
cPanel Access Level
Reseller Owner
Hey all, hey Thomas! Thanks again for the previous reply, cPanel tokens are being generated correctly

Now got stuck with the Token creation but on the WHM level :( Can you advise please on how to generate that token before the WHM is created?

But let me explain in more detailed first. We're actually creating cPanel account under WHM level (using 'createacct' API) with the "reseller" privileges so that WHM account has been created automatically (if I understand that correctly). And then we need to generate an API token to use regular WHM API endpoints for that account, but I don't understand what auth parameters I should use. "cp_security_token" token doesn't work either

And the same for WHM ROOT API token. Is this possible to generate both with that type of WHM?