Struggling to send trade requests with APIs in NodeJS

Hey! I’ve been messing around with the Roblox Trade API and I’m struggling with it. I’ve looked up as many devforum posts as I can with no luck.

This is my code:

const queue = require("./queue.json")
const config = require("./config.json")
const noblox = require("noblox.js")

const userid = config.UserId

require("dotenv").config()

noblox.getGeneralToken(process.env.COOKIE).then((token) => {
    console.log(token)
    async function loop() {
        var trade = queue[0]
        var id = trade[0]
        var send = trade[1]
        var recieve = trade[2]
        console.log(id,send,recieve)
        axios(`https://trades.roblox.com/v1/trades/send`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': token, cookie: '.ROBLOSECURITY=' + process.env.COOKIE + ';' },
            body: JSON.stringify({
                offers: [
                    {
                        userId: id,
                        userAssetIds: send,
                        robux: 0,
                    },
                    {
                        userId: userid,
                        userAssetIds: recieve,
                        robux: 0,
                    },
                ],
            }),
        }).then((res) => {
            console.log(res.data)
            console.log(trade)
            for (e in queue) {
                if (queue[e][0] == trade) {
                    queue.splice(e, 1)
                    console.log(queue)
                }
            }
        }).catch((err) => {
            console.log(err)
        })
        
        setTimeout(loop, 10000)
    }
    
    loop()
}).catch((err) => {
    console.log(err)
})


process.on("uncaughtException", (err) => {
    console.log(err)
    setTimeout(loop, 10000)
})

And I’m getting error 400s, can anyone help and tell me what I’m doing wrong?

Looking at the trade api status code 400 can be 1 of 15 things so can you try logging reason for it and sending it?

Will do, give me a second to try.

image
Does this help with anything? Or do I need to dig deeper.

Go deeper its under the error section of the response body.
Looks something like this

"errors": [
    {
      "code": 0,
      "message": "reason",
      "userFacingMessage": "message"
    }
]


There you go, I’ve got that before, logged above is what gets sent to the api.

I may be wrong about this one but are you sure that what you’re sending formatted correctly?
The api says that send requests should look like this:

{
  "offers": [
    {
      "userId": 0,
      "userAssetIds": [
        0
      ],
      "robux": 0
    }
  ]
}

Which yours doesn’t but it might just not be formatted in your console output.

Edit: The only way I was able to get ‘The trade request should include offers’ was by improperly formatted(or omitting) that.

This is what I’m currently sending:

body: JSON.stringify({
                offers: [
                    {
                        userId: id,
                        userAssetIds: send,
                        robux: 0,
                    },
                    {
                        userId: userid,
                        userAssetIds: recieve,
                        robux: 0,
                    },
                ],
            }),

And the full request here:

axios(`https://trades.roblox.com/v1/trades/send`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': token, cookie: '.ROBLOSECURITY=' + process.env.COOKIE + ';' },
            body: JSON.stringify({
                offers: [
                    {
                        userId: id,
                        userAssetIds: send,
                        robux: 0,
                    },
                    {
                        userId: userid,
                        userAssetIds: recieve,
                        robux: 0,
                    },
                ],
            }),
        }).then((res) => {
            console.log(res.data)
            console.log(trade)
            for (e in queue) {
                if (queue[e][0] == trade) {
                    queue.splice(e, 1)
                    console.log(queue)
                }
            }
        }).catch((err) => {
            console.log(err.response.data)
        })

Are you able to log what comes out of JSON.stringify?
I’ve been trying to get the same error but the only way I can get it is if I just straight up don’t send the offers array.

This is what I get:

929192511 { userAssetIds: [ 124204818211 ] } { userAssetIds: [ 112772059377 ] }
{"offers":[{"userId":929192511,"userAssetIds":{"userAssetIds":[124204818211]},"robux":0},{"userId":1297451021,"userAssetIds":{"userAssetIds":[112772059377]},"robux":0}]}
{
  errors: [
    {
      code: 8,
      message: 'The trade request should include offers.',
      userFacingMessage: 'Something went wrong'
    }
  ]
}

2nd line is the JSON.stringify, I think I see the problem.

Changed it a bit, still getting the error:

What did you change it to?


Well the first you sent wasn’t correct bc it had AssetIds that looked like this.

"userAssetIds":{
            "userAssetIds":[
               124204818211
            ]
         },

But this one is correct and still getting the same error, Which is definitely shouldn’t be giving you that error.

Yeah, that’s what I thought, any ideas?

None.
It shouldn’t be giving that error unless something later in the code after you log it changes it but I don’t see any of that.
I would expect error 9, 7 or 10 if it does go wrong.
but I’ve only been able to get 8 if I just send {}
Which makes sense since there are no offers lol.

Yeah it’s strange, can you send your code?

how isn’t it? it’s about the roblox trading api

@anthropomorphic_dev Could you send your code so I can compare the two and make sure I just haven’t written something wrong haha.

Oh sorry its pretty much the same except I manually typed out most of it.

--// I use my own http, json and console lib
http(`https://trades.roblox.com/v1/trades/send`, {
	method: "POST",
	headers: {
		"Content-Type": "application/json",
		"X-CSRF-TOKEN": "token",
		"cookie": ".ROBLOSECURITY=can't show this lel;",
	},
	body: json.parse({
		offers: [
			{
				userId: 0000000, //friends id
				userAssetsId: [
					000000, // some random item Id
				],
				robux: 0,
			},
		],
	}),
}).then((response: string) => {
	Cconsole.printJSON(response);
});

Edit: In case you were curious to get error code 8 I have to do this:

http(`https://trades.roblox.com/v1/trades/send`, {
	method: "POST",
	headers: {
		"Content-Type": "application/json",
		"X-CSRF-TOKEN": "token",
		"cookie": ".ROBLOSECURITY=I DEF CANNOT SHOW THIS;",
	},
	body: json.parse({}),
}).then((response: JSONString) => {
	Cconsole.printJSON(response);
});