Site Members API & Roles Management

Question:
I want to remotely manage the roles and information of the members registered on my site using a webhook. I want to achieve this by providing the user ID and the role ID I want to assign them to.

How can I accomplish this using Wix API or Velo?

Product:
I have used Velo Editor to execute some backend methods and _functions access. Additionally, I have tried performing these actions via the Wix API.

What are you trying to achieve:
I can manage the roles that site members can be assigned to. I want to find a user by their ID and add them to an additional role within the existing roles they are already assigned to.

I can retrieve the role’s ID (roleId) from the Role Management page.


All I want to do is change a user’s role via a webhook. Updating role statuses on my sites based on changes happening in a different system shouldn’t be this difficult.

What have you already tried:

import { ok, serverError } from 'wix-http-functions';
import { fetch } from 'wix-fetch';

export function post_assignRole(request) {
    return request.body.text()
        .then(bodyText => {
            const body = JSON.parse(bodyText);
            const { roleId, memberId } = body;

            const apiUrl = `https://www.wixapis.com/roles-management/contributor/change/role`;

            return fetch(apiUrl, {
                method: "PATCH",
                headers: {
                    "Authorization": `Bearer YOUR_WIX_API_KEY`,
                    "Content-Type": "application/json"
                },
                body: JSON.stringify({
                    "accountId": memberId,
                    "newRoles": [{ "roleId": roleId }]
                })
            })
            .then(response => response.json())
            .then(data => ok({ body: data }))
            .catch(error => serverError({ body: { error: error.message } }));
        });
}

I don’t understand why we are making API requests to ourselves within the backend code, but the examples I found online were like this. However, most of my requests are returning a 404 response.

import { Permissions, webMethod } from "wix-web-module";
import { authorization } from "wix-members-backend";
import { elevate } from "wix-auth";

/**
 * Üyeye rol atama fonksiyonu (Wix Frontend ve HTTP için)
 */
export const assignRoleBackend = webMethod(
    Permissions.Anyone, 
    async (roleId, memberId) => {
        try {
            const elevatedAssignRole = elevate(authorization.assignRole);
            const options = { suppressAuth: false };

            await elevatedAssignRole(roleId, memberId, options);
            return { success: true, message: "Rol başarıyla atandı." };
        } catch (error) {
            return { success: false, error: error.message };
        }
    }
);

I created a file named roles.web.js, assuming it could be secured within the backend, and started testing the code live from there. However, I still couldn’t manage to control the roles shown in the locations I demonstrated in the screenshots.

Additional information:
Also, when I clicked the test button to try the API endpoints provided in the documentation, I noticed that the body parameters were incorrectly documented. This documentation has confused me about which endpoint requires what kind of data exactly.

Hi! It looks like you are mixing up a few things here.

If you are trying to create an endpoint from an existing site to be accessed outside of Wix (testing in Postman for example) then you do need to create an http function but you will not need the other file or the wix api (which is not relevant in this case)

I suggest first getting a working example of an http function with test data - you can use this tutorial Write an HTTP Function

When it is working, you would be able to call the API from postman or curl

Once you have a working http function, then you will want to use this API for updating roles. Assign Role | Velo (which is similar to your second code snippet, but you will need to create it so your http func is calling it - you will also need suppressAuth = true as you are calling this from outside the site.

Finally - i would consider security as you will be accessing and modifying member data on your site and unless you secure the API you create, it will be accessible to anyone who has the endpoint.

I know that’s alot of information, but I would start with the http function tutorial to make sure you understand how those work on Wix first then move on to the admin function of updating the roles.

Edit: Also, depending on what you are doing another potential option is Wix Headless which you can explore here About Wix Headless

This is relevant if you are building outsdie the Wix ecosystem in something like NextJS for example but need to communicate with the wix business solutions. This would be more complex than creating one http-function so it will depend on your requirements