• Hi, Pleskians! We are running a UX testing of our upcoming product intended for server management and monitoring.
    We would like to invite you to have a call with us and have some fun checking our prototype. The agenda is pretty simple - we bring new design and some scenarios that you need to walk through and succeed. We will be watching and taking insights for further development of the design.
    If you would like to participate, please use this link to book a meeting. We will sent the link to the clickable prototype at the meeting.
  • (Plesk for Windows):
    MySQL Connector/ODBC 3.51, 5.1, and 5.3 are no longer shipped with Plesk because they have reached end of life. MariaDB Connector/ODBC 64-bit 3.2.4 is now used instead.
  • The Horde webmail has been deprecated. Its complete removal is scheduled for April 2025. For details and recommended actions, see the Feature and Deprecation Plan.

Question Connexion by API-REST

Pierre Ringenbach

New Pleskian
Server operating system version
Ubuntu 22.04
Plesk version and microupdate number
18.0.66 #2
Hi,
I'm learning how to manage my plesk users with the API (to do it automatically from another site), but, if I run my query, the 401 error still appears.
Do you have any ideas to solve this?

This is my request PHP :

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];

// Générer un mot de passe aléatoire
$password = bin2hex(random_bytes(8)); // Génère un mot de passe de 16 caractères

// Informations de l'API Plesk
$plesk_api_url = "https://domain:8443/api/v2/clients";
$api_key = "Key_API";

// Données à envoyer à l'API Plesk
$data = [
"name" => $name,
"email" => $email,
"password" => $password
];

// Configuration de la requête cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $plesk_api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/json",
"X-API-Key: $api_key"
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

// Exécution de la requête
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($http_code == 201) {
echo "Utilisateur créé avec succès!<br>";
echo "Nom: $name<br>";
echo "Email: $email<br>";
echo "Mot de passe: $password<br>";
} else {
echo "Erreur lors de la création de l'utilisateur. Code HTTP: $http_code<br>";
echo "Réponse: $response";
}

curl_close($ch);
}
?>

And here is the site's response :
1741254308376.png
I have nothing in the F12 console

Thank you for your help
 
A 401 error means that authentication failed. Pay attention when generating an API that you specify the right IP address, which should be the IP from which you're trying to access the Plesk server. Lastly make sure there are no access restrictions for the API (there are non by default), more information can be found here: Restricting Remote Access via Plesk API

Hope this helps.
 
Yes, but I don't have other restrictions for the API :
1741268205023.png

For this line "$plesk_api_url = "https://domain:8443/api/v2/clients";" I use a Domain name pointed at IP address, it's necessary write an IPv4 of server ?

The Domain name used is a domain name use of plesk panel, is good ?
 
when I search a little how to find my key to control, they bring me back here where I can regenerate my key.
However if we look here, I only have this interface. Do you know how I can regenerate my key from here?
1741275470379.png
for the moment I was looking for my key with this command, but this key changes every time I do the command and when refreshing my code, the error remains there.
 
Last edited by a moderator:
@Pierre Ringenbach please be careful with any information you post here! Your previous post your screenshot showed the root login credentials for your server (a moderator removed the screenshot for you). Please remember that this is a public forum, anyone can view anything that's gets posted here. As security precaution I highly recommend that you change your servers root password.

Could the key be the reason for the error?
Yes, that's very likely the issue.

Where can I check which one is correct?
You can view all current API Secret keys via SSH running the plesk bin secret_key --list command. Alternatively you can also use the Keychain for API Secret Keys extension to manage your secret keys.

Note that (like I mentioned in my previous post) if you generate a API secret key, you need to supply the IP address from which the API call to the Plesk server is made. The API secret keys are bound to the request IP address. If you need unrestricted access, you can use basic auth instead (but that's a less secure method).
 
Back
Top