How to update user role with a function?


I’m trying to setup a netlify function that will change a user’s role as a response to a successful payment on site, which means it’s a function that I trigger based on some event that happens.

User logs in via Netlify Identity => User pays => trigger to netlify function called update-user.js => function updates the user’s role to “subscriber”.

The problem: I seem not to have the right privilges to edit the user (need admin) and I don’t get the user’s ID in the context although I should have it.
This is my function:

const fetch = require('node-fetch');

exports.handler = function(event, context, callback) {
  const {identity, user} = context.clientContext;
    var user_data={
      roles: ['subscribed'],
      user_metadata: {
        some_more_data: 'this was added by update-user.js'

  fetch(`${identity.url}/admin/users`, {
      method: 'POST',
      headers: { Authorization: `Bearer ${identity.token}` },
      body: JSON.stringify(user_data)

  callback(null, {
    statusCode: 200,
    body: JSON.stringify(user_data),

and this is how I call it from app.js based on some logic that happens before:

      .then(response => response.json())
      .then(json => console.log(json))

Obviously it doesn’t work, I’m a newbie with this, so take me step by step if possible. What am I missing?

Hi, I think the body you are sending might not be structured correctly. There’s an example here: Let me know if that helps.