For this example we'll use the /set_pixel
endpoint to set a specific pixel on the canvas. Firstly we want to import our HTTP library, for now we'll use the requests (pip install requests) library.
This guide will use the requests
library but you can use httpx
, aiohttp
or any client side HTTP library of your preference to interact with pixels.
You'll need to follow the Authentication guide and get to a stage where you can access your token in your Python script.
We need to create the authorization header for making API requests. We'll simply create a dictionary:
# Where "token" is your token from earlier.
HEADERS = {
"Authorization": f"Bearer {token}"
}
Next, we will create the actual data we're going to send to the API, in this case we will set the pixel at (123, 12)
to the hex value #87CEEB
:
data = {
"x": 123,
"y": 12,
"rgb": "87CEEB",
}
Finally, we can make the API request itself, for which we'll need to use the post
method of requests to send a HTTP POST
to the pixels webserver:
result = requests.post(
"<https://pixels.pythondiscord.com/set_pixel>",
json=data,
headers=HEADERS
)
# raise_for_status will cause Requests to throw an error if the Pixels API responds with a non-200 (success) code.
result.raise_for_status()
To check that the request went through correctly we'll print out the confirmation message that the API provides us with:
print(result.json()["message"])
Which will print, in this case, added pixel at x=123,y=12 of color 87CEEB
.