API Key/Roblox Cloud API Help

So I’m trying to create a bot that automatically ranks people in my Roblox roleplay game, and in order to do so, I created a user API in Roblox, then created a cloud flare worker to post to that API, and then my game posts to that cloud flare API. But it always prints out in cloud flare workers “ROBLOX RESPONSE 400 { “code”: “INVALID_ARGUMENT”, “message”: “Failed to parse resource path and its identifiers - .” }”

Here’s my cloud flare worker code

export default {
	async fetch(request, env) {
		try {
			if (request.method !== "POST") {
				return new Response(
					JSON.stringify({
						success: false,
						error: "Method not allowed"
					}),
					{
						status: 405,
						headers: {
							"Content-Type": "application/json"
						}
					}
				)
			}

			const body = await request.json()

			const userId = body.userId

			if (!userId) {
				return new Response(
					JSON.stringify({
						success: false,
						error: "Missing userId"
					}),
					{
						status: 400,
						headers: {
							"Content-Type": "application/json"
						}
					}
				)
			}

			const GROUP_ID = 0
			const PRISON_GUARD_ROLE_ID = 0
      console.log("USER ID:", userId)
      console.log("GROUP:", GROUP_ID)
      console.log("ROLE:", PRISON_GUARD_ROLE_ID)
      console.log("API KEY EXISTS:", !!env.ROBLOX_API_KEY)
			const robloxResponse = await fetch(
				`https://apis.roblox.com/cloud/v2/groups/${GROUP_ID}/memberships/${userId}`,
				{
					method: "PATCH",
					headers: {
						"Content-Type": "application/json",
						"x-api-key": env.ROBLOX_API_KEY
					},
					body: JSON.stringify({
						roleId: PRISON_GUARD_ROLE_ID
					})
				}
			)

			const responseText = await robloxResponse.text()

console.log(
	"ROBLOX RESPONSE",
	robloxResponse.status,
	responseText
)

			return new Response(
				JSON.stringify({
					success: robloxResponse.ok,
					status: robloxResponse.status,
					body: responseText
				}),
				{
					status: robloxResponse.status,
					headers: {
						"Content-Type": "application/json"
					}
				}
			)
		}
		catch (err) {
			return new Response(
				JSON.stringify({
					success: false,
					error: err.toString()
				}),
				{
					status: 500,
					headers: {
						"Content-Type": "application/json"
					}
				}
			)
		}
	}
}

just an fyi, the endpoint you’re using is deprecated

Deprecated. Use AssignGroupRole and UnassignGroupRole instead. Updates the group membership for a particular group member.

and what you’re giving for the body is incorrect


to set a role, you have to give
"role": "groups/7/roles/99513316"

example using non-deprecated endpoint

const response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${group_id}/memberships/${membership_id}:assignRole`, {
    method: "POST",
    headers: { "Content-Type": "application/json", "x-api-key": env.ROBLOX_API_KEY },
    body: JSON.stringify({ role: `groups/${group_id}/roles/${role_id}` })
})
1 Like

I ended up figuring it out, thank you

1 Like