Changing users rank in roblox group

is it possible to change Users rank that’s in your Roblox group using open cloud API

2 Likes

Not as far as I can tell!

This is in fact possible! Throughout this post, I’ll use JavaScript and it’s fetch to show you how.

First, you’ll need an API key. It won’t work as a group API key, so make it under your user and make sure you have the permissions! Also give the API key groups:read and groups:write permissions.

Next, you need to get the role ID of the rank you want to promote the user to. You can send a GET request to this endpoint to get them:

`https://apis.roblox.com/cloud/v2/groups/${GROUP_ID}/roles`

Which would look like this in JavaScript:

const apiKey = "your API key";
const groupId = //your group ID
const targetUserId = //target UserId
const targetRole = "name of the role you want the user to be updated to";

async function getGroupRoleId(targetRole) {
    //send the GET request
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/roles?maxPageSize=20`, {
        method: "GET",
        headers: {
            "x-api-key": apiKey
        }
    });

    let data = await response.json();
    for (let role of data) { //check each role's ID
        if (role.displayName === targetRole) { //if it's the role we want
            return Number(role.id); //return that reference
        }
    }
};

Now, we need to list all the user memberships and check each one to see if it matches with our target’s UserId. We can send a GET request to another endpoint which lists all the user memberships. A membership represents a user being in a group, and is established when the user joins the group.

async function getUserMembershipId(targetUserId) {
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/memberships?maxPageSize=50`, {
        method: "GET",
        headers: {
            "x-api-key": apiKey
        }
    });

    //don't forget you can make multiple requests and use the pageToken request parameter
    //to get pages of data after the current one!

    let data = await response.json();

    for (let membership of data.groupMemberships) {
        //check to see if it's the UserId of the user we want
        let splitted = membership.user.split("/");
        let userId = splitted[splitted.length - 1];
        
        if (userId == targetUserId) {
            //the UserIds match! Now, return the membership ID
            let membershipId = membership.path.split("/")[3];
            return membershipId, userId;
        }
    }
};

Great! Now, for the last step: updating the membership. We can send a PATCH request to yet another endpoint in order to update it.

async function updateUserRole(targetRoleId, targetMembershipId) {
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/memberships/${targetMembershipId}`, {
        method: "PATCH",
        headers: {
            "Content-Type": "application/json",
            "x-api-key": apiKey
        },
        body: JSON.stringify({
            "role": `groups/${groupId}/roles/${targetRoleId}`
        })
    });
};

so, after all of that, you should just be able to:

async function updateRoleProcess(userId, roleName, groupId) {
    let roleId = await getGroupRoleId(roleName);
    let membershipId = await getUserMembershipId(userId);
    await updateUserRole(roleId, membershipId);
};

updateRoleProcess(targetUserId, roleName, groupId).catch(error => console.log(error));
3 Likes

I get an error when I run the code saying userId is not defined

My code snippets were just supposed to be a template. Change the first parameter passed to updateRoleProcess to targetUserId, and if that doesn’t work, send your code and I’ll take a look.

“I’m now getting an error saying: ‘TypeError: data is not iterable at getGroupRoleId’, and my role name is ‘collect’.”

I tested those code snippets within my own script and group and it worked with no problem at all.

There’s probably an issue with your API key. Make sure:

  • your key has group:read and group:write permissions
  • your key hasn’t expired
  • you are requesting from an allowed IP address for your key

(Ima sleep now so may take a few hours to respond)

everything is right on my open cloud API key I’m still getting that error

You’re 100% sure? Try again, this is the full code snippet, just change the constants at the top.

const apiKey = "";
const groupId = 0;
const targetRole = "Role";
const targetUserId = 0;

async function getGroupRoleId(targetRole) {
    //send the GET request
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/roles?maxPageSize=20`, {
        method: "GET",
        headers: {
            "x-api-key": apiKey
        }
    });

    let data = await response.json();
    for (let role of data.groupRoles) { //check each role's ID
        if (role.displayName === targetRole) { //if it's the role we want
            return Number(role.id); //return that reference
        }
    }
};

async function getUserMembershipId(targetUserId) {
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/memberships?maxPageSize=50`, {
        method: "GET",
        headers: {
            "x-api-key": apiKey
        }
    });
    console.log(response.status!==200?String(response.Status) + response.statusText:"Request ok. Code 200.");

    //don't forget you can make multiple requests and use the pageToken request parameter
    //to get pages of data after the current one!

    let data = await response.json();

    for (let membership of data.groupMemberships) {
        //check to see if it's the UserId of the user we want
        let splitted = membership.user.split("/");
        let userId = splitted[splitted.length - 1];
        
        if (userId == targetUserId) {
            //the UserIds match! Now, return the membership ID
            let membershipId = membership.path.split("/")[3];
            return membershipId, userId;
        }
    }
};

async function updateUserRole(targetRoleId, targetMembershipId) {
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/memberships/${targetMembershipId}`, {
        method: "PATCH",
        headers: {
            "Content-Type": "application/json",
            "x-api-key": apiKey
        },
        body: JSON.stringify({
            "role": `groups/${groupId}/roles/${targetRoleId}`
        })
    });
    console.log(response.status!==200?String(response.Status) + response.statusText + "(from second request)":"Request ok. Code 200.");
};

async function updateRoleProcess(userId, roleName, groupId) {
    let roleId = await getGroupRoleId(roleName);
    let membershipId = await getUserMembershipId(userId);
    await updateUserRole(roleId, membershipId);
};

updateRoleProcess(targetUserId, targetRole, groupId).catch(error => console.log(error));

so it doesn’t print out any errors but it’s not ranking anyone but it does print out 200
44649633 when you run it

1 Like

I added some debugging console.log statements, could you run the updated code snippet and tell me what outputs?

Bare in mind you can’t rank anyone not in the group, you can’t assign Guest or Owner ranks and you can’t rank anyone higher than you.

1 Like

so it printed out Request ok. Code 200.
undefinedBad Request(from second request)

1 Like

Odd. Did you:

  • Spell the role name correctly?
  • Put the correct UserId in?
  • Make sure the API key is definitely set up correctly and hasn’t expired?

It’s likely one of those things, because the code works fine for me.

If that doesn’t work, can you send the second response’s errors from the response body?

it’s not working where do I find the second response’s errors from the response body? for i can send it to you

If you run this code snippet after the request:

let data = await response.json();
for (let error of data.errors) {
    console.log("Code:", error.code, "Message:", error.message);
}

this is what I’m getting i can send you haft of the code that doesn’t include my
Screenshot 2025-01-05 032818
API key if you want

Well, it said “bad request” before, at it seems to be the same. To find out what’s wrong with the request, we need to look at the errors.

Can you try using my code snippet from above in the updateUserRole function after the request? Unless you have already, but the screenshot you sent looks the same as before.

here you go

async function getUserMembershipId(targetUserId) {
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/memberships?maxPageSize=50`, {
        method: "GET",
        headers: {
            "x-api-key": apiKey
        }
    });
    console.log(response.status!==200?String(response.Status) + response.statusText:"Request ok. Code 200.");

    //don't forget you can make multiple requests and use the pageToken request parameter
    //to get pages of data after the current one!

    let data = await response.json();

    for (let membership of data.groupMemberships) {
        //check to see if it's the UserId of the user we want
        let splitted = membership.user.split("/");
        let userId = splitted[splitted.length - 1];
        
        if (userId == targetUserId) {
            //the UserIds match! Now, return the membership ID
            let membershipId = membership.path.split("/")[3];
            return membershipId, userId;
        }
    }
};

async function updateUserRole(targetRoleId, targetMembershipId) {
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/memberships/${targetMembershipId}`, {
        method: "PATCH",
        headers: {
            "Content-Type": "application/json",
            "x-api-key": apiKey
        },
        body: JSON.stringify({
            "role": `groups/${groupId}/roles/${targetRoleId}`
        })
    });
    console.log(response.status!==200?String(response.Status) + response.statusText + "(from second request)":"Request ok. Code 200.");
};
1 Like

That appears to just be my code snippet again. Try replacing getUserMembershipId with this and tell me what outputs.

async function getUserMembershipId(targetUserId) {
    let response = await fetch(`https://apis.roblox.com/cloud/v2/groups/${groupId}/memberships?maxPageSize=50`, {
        method: "GET",
        headers: {
            "x-api-key": apiKey
        }
    });
    console.log(response.status!==200?String(response.Status) + response.statusText:"Request ok. Code 200.");

    let data = await response.json();
    for (let error of data.errors) {
        console.log("Code:", error.code, "Message:", error.message);
    }
};

I’m getting an error now saying SyntaxError: await is only valid in async functions and the top-level bodies of modules