Skip to content

Instantly share code, notes, and snippets.

@Bilka2
Last active October 23, 2025 05:37
Show Gist options
  • Select an option

  • Save Bilka2/5dd2ca2b6e9f3573e0c2defe5d3031b2 to your computer and use it in GitHub Desktop.

Select an option

Save Bilka2/5dd2ca2b6e9f3573e0c2defe5d3031b2 to your computer and use it in GitHub Desktop.
Simple discord webhook with python
import requests # dependency
url = "<your url>" # webhook url, from here: https://i.imgur.com/f9XnAew.png
# for all params, see https://discordapp.com/developers/docs/resources/webhook#execute-webhook
data = {
"content" : "message content",
"username" : "custom username"
}
# leave this out if you dont want an embed
# for all params, see https://discordapp.com/developers/docs/resources/channel#embed-object
data["embeds"] = [
{
"description" : "text in embed",
"title" : "embed title"
}
]
result = requests.post(url, json = data)
try:
result.raise_for_status()
except requests.exceptions.HTTPError as err:
print(err)
else:
print(f"Payload delivered successfully, code {result.status_code}.")
# result: https://i.imgur.com/DRqXQzA.png
@HGStyle
Copy link

HGStyle commented May 29, 2024

how would one attach a file? do we just use .read() for the contents? and where does the file name go?

@Hypurrnating I think you can use this:
requests.post("<your webhook url>", json = {"content": "<your message here (you can just set to an empty string)>"}, files = {"myfile": open("<your file path here>", "rb")})
Note that you can change myfile to anything other, and so, add multiple items into the dictionnary passed to files by changing the name.

@tjb0607
Copy link

tjb0607 commented Oct 6, 2025

To attach an image alongside message text, it's important to name the json data "payload_json" and the image attachment "files[0]", otherwise you will get a 400 error.

import json
import requests

url = "<webhook url>"
img = "/path/to/image.png"
filename = "image.png"

# for all params, see https://discordapp.com/developers/docs/resources/webhook#execute-webhook
# for embeds: https://discord.com/developers/docs/resources/message#embed-object
# for attachments: https://discord.com/developers/docs/reference#uploading-files
data = {
    "username": "custom username",
    "content": "message content",
    # leave this out if you don't want an embed
    # (the image will still be attached either way)
    "embeds": [
        {
            "title": "embed title",
            "description": "embed text",
            "image": {
                "url": f"attachment://{filename}"
            }
        }
    ],
    "attachments": [
        {
            "id": "0",
            "filename": filename
        }
    ]
}

files = {
    "payload_json": (None, json.dumps(data)),
    "files[0]": open(img, 'rb')
}

result = requests.post(url, files=files)

try:
    result.raise_for_status()
except requests.exceptions.HTTPError as err:
    print(err)
else:
    print(f"Payload delivered successfully, code {result.status_code}.")

Doing it this way also allows you to edit the message with requests.patch() without duplicating your attachments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment