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.