# Nextjs-auth0: How to read and create app\_metadata

**URL:** <https://community.auth0.com/t/nextjs-auth0-how-to-read-and-create-app-metadata/80759>\
**Category:** Get Help\
**Created:** [March 30, 2022, 3:50am UTC](https://community.auth0.com/t/nextjs-auth0-how-to-read-and-create-app-metadata/80759 "2022-03-30T03:50:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fnl](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/fnl/32/30398_2.png) [@fnl](https://community.auth0.com/u/fnl)\
**Post date:** [March 30, 2022, 3:50am UTC](https://community.auth0.com/t/nextjs-auth0-how-to-read-and-create-app-metadata/80759/1 "2022-03-30T03:50:49Z")

</div>

I have a NextJs application using the @auth0/nextjs-auth0 library. I am trying to store some metadata within user’s profile ( e.g. app\_metadata : { id: 123} ) with an axios call. How can I get the management\_api\_key?

I have manually put some metadata using the dashboard → User Management → Users → Metadata. And I have set a AUTH0\_SCOPE=‘openid email profile read:current\_user’ in my .env.local within my NextJs project. However when i console log the “user” using the useUser hook, I cannot see the user\_metadata.

So how can I create and read user metadata in my NextJs application? Thank you \<3

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [March 30, 2022, 8:25pm UTC](https://community.auth0.com/t/nextjs-auth0-how-to-read-and-create-app-metadata/80759/3 "2022-03-30T20:25:43Z")

</div>

Hi @fnl,

Welcome to the Auth0 Community!

> [@fnl](#):
>
> How can I get the management\_api\_key?

There are a couple of options when getting an Access Token for the Management API.

1. You could request an Access Token for the Management API by using a Machine-to-Machine application to perform a **Client Credentials** grant flow.

or

1. Using the [Auth0 Node.js Library](https://www.npmjs.com/package/auth0) to automatically get Access tokens for the Management API using the `ManagementClient` class.

Once you have obtained the token, you can call the Management API’s [Get a User endpoint](https://auth0.com/docs/api/management/v2#!/Users/get_users_by_id) to read the user’s app\_metadata.

Please see our [Get Management API Access Tokens for Production](https://auth0.com/docs/secure/tokens/access-tokens/get-management-api-access-tokens-for-production) documentation on getting an Access Token for the Management API.

Hoped this helps!

Please let me know if you have any further questions.

Thank you.

---

<div class="post-metadata">

**Author:** ![fnl](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/fnl/32/30398_2.png) [@fnl](https://community.auth0.com/u/fnl)\
**Post date:** [March 31, 2022, 4:34am UTC](https://community.auth0.com/t/nextjs-auth0-how-to-read-and-create-app-metadata/80759/4 "2022-03-31T04:34:44Z")

</div>

I am sorry, I am still very new to this. Would you be able to give me some more instruction please? Which method, out of those two, do you recommend more? Also am I supposed to fetch the access token within my NextJs app under pages/api/auth, or I should be setting up a backend route and call that api from the front end?

I have also read a post ([Yes, you can update user metadata with Next.js + Auth0 — build it nice](https://write.as/build-it-nice/yes-you-can-update-user-metadata-with-next-js-auth0)) saying I should put the below code within the pages/api/auth/[…auth0].ts. The post mentioned for some reason the scope is not read, therefore the code snippet in below is required

```auto
export default handleAuth({
  async login(req, res) {
    try {
      await handleLogin(req, res, {
        authorizationParams: {
          audience: 'https://api.example.com/products',
          scope: 'openid profile email read:current_user_metadata'
        }
      });
    } catch (error: any) {
      res.status(error.status || 400).end(error.message);
    }
  },
});

```

instead of just

```auto
export default handleAuth();

```

Thank you for your help

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [April 1, 2022, 11:29pm UTC](https://community.auth0.com/t/nextjs-auth0-how-to-read-and-create-app-metadata/80759/5 "2022-04-01T23:29:16Z")

</div>

Hi @fnl,

Thank you for your response.

> [@fnl](#):
>
> Which method, out of those two, do you recommend more?

I would suggest option 2, to use the Auth0 Node.js Library to automatically get Access tokens for the Management API.

> [@fnl](#):
>
> Also am I supposed to fetch the access token within my NextJs app under pages/api/auth, or I should be setting up a backend route and call that api from the front end?

The former. Using the Auth0 Node.js library, you can make requests to read and create app\_metadata without being concerned with getting a token. After instantiating the `ManagementClient` class, you will automatically have an access token that you can make requests with the Management API.

Lastly, please make sure your `audience` value matches the Management API identifier found on your Auth0 Dashboard. Generally in the format `https://YOUR_DOMAIN.REGION.auth0.com/api/v2/`.

Please let me know if there’s anything else I can do to help.

Thank you.

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [December 21, 2022, 5:22pm UTC](https://community.auth0.com/t/nextjs-auth0-how-to-read-and-create-app-metadata/80759/6 "2022-12-21T17:22:39Z")

</div>


