Articles on: Discord Bot Hosting

How to use the Pterodactyl API using Python

How to use the Pterodactyl API using Python



Firstly, we need to create an API key. We will need this to interact with the panel. To do this, Click “API Credentials” on the left-hand side of the panel.

Set the description and the Allowed IPs. This is useful if you would like to secure the usage of the API key. Leave it blank if you would like to allow any IP address to the API key.



The API key is now shown.

Note: Keep a backup of your API key. You will not be able to see it after you close this box!



We are now ready to start coding! You will require an HTTP library. In this case, we are going to use requests. You are able to view all the pterodactyl endpoints here. Please note that the documentation page is not official!

In this example, We will cover the most useful endpoints.

How to check server info

To check the server status, you will need to make a request to /api/client/servers/<server id>. Of course, replace <server id> with your server id.

Here is an example of a request:

import requests
import json
ApiKey = "My API Key"

response = requests.get("https://control.sparkedhost.us/api/client/servers/<server id>", headers={
    'Accept': 'application/json',
    'content-type': 'application/json',
    'Authorization': 'Bearer ' + ApiKey
}).json()

print(json.dumps(response, indent=1))



This is the response we get back from the panel:



How to receive server stats (e.g CPU, RAM, disk & network usage)

In this case, you will need to make a request to /api/client/servers/<server id>/resources. Of course, replace <server id> with your server id. You are able to use the following code:

import requests
import json
ApiKey = "My API Key"

response = requests.get("https://control.sparkedhost.us/api/client/servers/f7a7faee/resources", headers={
    'Accept': 'application/json',
    'content-type': 'application/json',
    'Authorization': 'Bearer ' + ApiKey
}).json()

print(json.dumps(response, indent=1))

This is the response we get back:





If you require any further assistance, please create a ticket here.


Created By: Julián M.

Updated on: 19/03/2023

Was this article helpful?

Share your feedback

Cancel

Thank you!