How do I send a roblox message with python?

I am trying to send a roblox message with python, but I can’t get the api to work, what should I do?

here is what I have so far on the json:

{
  "userId": 839071378,
  "subject": "testtest",
  "body": "This is a test, lol",
  "recipientId": 2494645060,
  "replyMessageId": 0,
  "includePreviousMessage": false
}

(btw it uses with api: PrivateMessages Api)

All help wanted :slight_smile:

1 Like

Your post isn’t very descriptive – you hadn’t really delved into the issue you are having, where you even are exactly, or what exactly you’re trying to achieve so I’m just going to start at the beginning and assume you’re trying to send a simple, original message to someone. The aforementioned problems are essential to avoid the “XY problem”:


You first begin with authentication. If you’re not authorised, you end get up getting an error from the api endpoint. Two things are needed here for authentication:

  • .ROBLOSECURITY cookie: This is a token that exists on a per session basis, meaning that when you log in, a new cookie gets created and it gets destroyed when you logout. This you can get by opening your browser’s dev tools on a page that is part of roblox’s domain by pressing F12 > Application > Cookies then click on the page that matches the url. There you will find the value for your .ROBLOSECURITY cookie

  • X-CSRF-TOKEN: This is a token used to prevent CSRF (cross-site request forgery) attacks. How that is obtained is described below

In order to get your x-csrf-token, you want to make a request to the “logout” endpoint of the auth api with your ROBLOSECURITY cookie used as a cookie in the request.

import requests

cookie = YOUR ROBLOSECURITY COOKIE
auth_response = requests.post("https://auth.roblox.com/v1/logout", headers = {"cookie": f".ROBLOSECURITY={cookie}"})

if auth_response.status_code == 403:
    if "x-csrf-token" in auth_response.headers:
        token = auth_response.headers["x-csrf-token"]

Now that we obtained the token, we are able to be properly authorised. Now we can send a request to the PrivateMessages API to send a message with our cookie and x-csrf-token:

import requests

cookie = YOUR ROBLOSECURITY COOKIE
auth_response = requests.post("https://auth.roblox.com/v1/logout", headers = {"cookie": f".ROBLOSECURITY={cookie}"})

if auth_response.status_code == 403:
    if "x-csrf-token" in auth_response.headers:
        token = auth_response.headers["x-csrf-token"]

headers = {
    "cookie": f".ROBLOSECURITY={cookie}",
    "x-csrf-token": token
}

data = {
    "userId": 839071378, # your user id
    "subject": "testtest",
    "body": "This is a test, lol",
    "recipientId": 2494645060 # recipient's user Id
}

message_response = requests.post("https://privatemessages.roblox.com/v1/messages/send", headers = headers, data = data)
print(message_response.json())    
6 Likes

I have not tried this, but thank you every much for taking the time to write this, it is really helpful! Thank you! :slight_smile: I will tell you if I have any problems.