How to send messages in discord through the api with python!

Advaith S
4 min readJan 18, 2022

What:

What is this? This is a python code that will send messages through your discord account with less than 10 lines of code

a simple example of the code

Why:

But why would you want to use the api when you can use the discord app or website anyways? Well imagine a case where its your best friends birthday tomorrow and you are travelling to new york that day so you cant message him at midnight… But you are his best friend so you want to do it but don't know how

Or think of a case where a discord spam war is going on and you want to participate but don't have time to do it, then you can run this code in the background in repl.it and do your work at the same time.

How?

Perquisites-

Python 3+

Mac OS:$ brew install pythonWindows:Python Website

The requests module-

$ pip3 install requests

A discord account

Procedure-

Open up your code editor and import requests first.

import requests

Now firsts lets understand what is going to happen-

When you send a message using the discord app or website, Discord calls its api which then carries out the process of sending the message to the server then to the user.

What we will do is, we will grab the api and call the api in python instead of the discord app/website

To get the api we have to go to the discord website first and open you chat in the browser

Then select some and right click it to select the inspect element option

Or

Use the shortcut-

command+option+I for mac

ctrl+shift+I for windows

Click the delete button if there are any requests in the network tab.

Now with the network tab open, send a message in the server

and a request call messages should pop up, click on it.

And select the headers option in the new dialogue box.

Now copy the request url…

and lets go back to the code editor.

after the import make three new variables- URL, DATA, HEADER

and in the URL variable paste the request url that we copied earlier and in the DATA variable and the HEADER variable paste the code shown below

import requestsURL = https://discord.com/api/v9/channels/932838134163603488/messages
DATA = {"content": "<the text you want to send>"}
HEADERS = {"authorization": "<your discord token>"}

If your wondering where to get your discord token, then just scroll a bit lower in the headers section where we got our request url and you will find something call authorization and it will have your accounts discord token.

now we just need to use the variables data and call the api with the requests module like so:

import requestsURL = https://discord.com/api/v9/channels/932838134163603488/messages
DATA = {"content": "<the text you want to send>"}
HEADERS = {"authorization": "<your discord token>"}
requests.post(URL, data=DATA, headers=HEADERS)

Thats all, DONE!!!

run the code and you will see that it worked!!!

If you want to send the message in an other channel the just right click on the channel and click copy id, then paste the ID instead of the 18–20 digit number in the request url

https://discord.com/api/v9/channels/<right here!!!>/messages

Viola, thats it

I will provide the full code down below and the code for a spam message sending with while loops

Full code:

import requestsURL = https://discord.com/api/v9/channels/<channel_id>/messages
DATA = {"content": "<the text you want to send>"}
HEADERS = {"authorization": "<your discord token>"}
requests.post(URL, data=DATA, headers=HEADERS)

Full code for spam:

import requestsURL = https://discord.com/api/v9/channels/932838134163603488/messages
DATA = {"content": "<the text you want to send>"}
HEADERS = {"authorization": "<your discord token>"}
while True:
requests.post(URL, data=DATA, headers=HEADERS)

Full code for spam with 1 second delay:

import requests
import time
URL = https://discord.com/api/v9/channels/932838134163603488/messages
DATA = {"content": "<the text you want to send>"}
HEADERS = {"authorization": "<your discord token>"}
while True:
requests.post(URL, data=DATA, headers=HEADERS)
time.sleep(1)

Thank You!!

Hope You Liked It

--

--