Update app_metadata using Management API

Hi all,

I’m attempting to update app_metadata using the Management API. I’m new to Auth0 and I’m not sure why I’m receiving a 400 error when attempting to send this request. Here is my code:

const patchUserOrg = async (accessToken, org, user) => {
  const id = user.sub.split('|')[1];

  const config = {
    method: 'PATCH',
    url: `https://${domain}/api/v2/users/${id}`,
    headers: {
      authorization: `Bearer ${accessToken}`,
      'content-type': 'application/json',
    },
    data: {
      app_metadata: {
        active: `${org}`,
      },
    },
  };

  const res = await axios(config);
  return res.data;
};

I’m receiving the access token through the Auth0 dashboard (i.e. Applications > APIs > Auth0 Management API > API Explorer). I understand this is not the proper way to get it but I’m doing this purely for testing to ensure the request works and performs the intended action. I’m receiving the user_id by parsing the sub property in the user object provided by the Auth0 hook. For the ‘domain’, I’m using the URL listed underneath General Settings > Identifier (on the same Auth0 Management API page).

What I’m trying to do? I’m trying to utilize the ‘active’ property on the app_metadata section to manage which profile my web application should show (i.e. if its set to org1, my backend API calls will fetch all the org1 specific data; if its set to org2, my backend API calls will fetch all the org2 specific data). If anyone knows a better and more simplistic way to implement this functionality, I’m definitely open to suggestions.

Thank you for taking the time to read this.

Best,
dhpcc

Hey @dpatel :wave:

You need to pass the whole Auth0 user_id to the Management API endpoint, don’t split the user.sub value for the API request - this is causing the 400 Bad Request response.

Try it it again with simply const id = user.sub;

You can also test the API call here after setting the setting the API Token: Auth0 Management API v2

Hope this helps!

2 Likes

Thank you so much for your help. This solved my issue.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.