# Expired Management API Token with node-auth0

**URL:** <https://community.auth0.com/t/expired-management-api-token-with-node-auth0/181982>\
**Category:** Get Help\
**Tags:** users, management-api\
**Created:** [February 11, 2025, 9:11pm UTC](https://community.auth0.com/t/expired-management-api-token-with-node-auth0/181982 "2025-02-11T21:11:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alan.h](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alan.h/32/70826_2.png) [@alan.h](https://community.auth0.com/u/alan.h)\
**Post date:** [February 11, 2025, 9:11pm UTC](https://community.auth0.com/t/expired-management-api-token-with-node-auth0/181982/1 "2025-02-11T21:11:25Z")

</div>

Hi,

I’m having issues with the access token expiring when using the `ManagementClient` from the `node-auth0` library. Looking at the code on GitHub, it seems like the `ManagementClient` should automatically get an access token through the `TokenProviderMiddleware`, including when the current access token expires.

```auto
export class ManagementClient extends ManagementClientBase {
  constructor(options: ManagementClientOptionsWithToken);
  constructor(options: ManagementClientOptionsWithClientCredentials);
  constructor(
    options: ManagementClientOptionsWithToken | ManagementClientOptionsWithClientCredentials
  ) {
    super({
      ...options,
      baseUrl: `https://${options.domain}/api/v2`,
      middleware: [
        new TokenProviderMiddleware(options),
        ...(options.telemetry !== false ? [new TelemetryMiddleware(options)] : []),
      ],
      parseError,
    });
  }
}

```

In `TokenProvider`

```auto
  public async getAccessToken() {
    if (!this.accessToken || Date.now() > this.expiresAt - LEEWAY) {
      this.pending =
        this.pending ||
        this.authenticationClient.oauth.clientCredentialsGrant({
          audience: this.options.audience,
        });
      const {
        data: { access_token: accessToken, expires_in: expiresIn },
      } = await this.pending.finally(() => {
        delete this.pending;
      });
      this.expiresAt = Date.now() + expiresIn * 1000;
      this.accessToken = accessToken;
    }
    return this.accessToken;
  }

```

In my app, I initialize a new `ManagementClient` every time I want to hit a Management API:

```auto
export const getAuth0Client = () =>
  new ManagementClient({
      domain: process.env.X,
      clientId: process.env.Y,
      clientSecret: process.env.Z,
  })

```

I expect that each client instance should have a fresh access token. Strangely, I only hit the `ManagementApiError: Expired token received for JSON Web Token validation` error when trying to access users or roles but not invites.

Can you please help me understand what the issue might be?

Thanks!

---

<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:** [February 13, 2025, 8:37pm UTC](https://community.auth0.com/t/expired-management-api-token-with-node-auth0/181982/3 "2025-02-13T20:37:51Z")

</div>

Hi @alan.h,

Welcome to the Auth0 Community!

The error you experience happens when the access token being used has expired. I suggest referring to [this related community post](https://community.auth0.com/t/new-managementclient-refuses-to-refresh-access-token/176057/3) that addresses this issue.

Essentially, you will want to verify the `exp` claim of your Management API access token is still valid:

1. Copy the generated Management API token from your code,
2. Paste the token on [https://jwt.io/](https://jwt.io/) and copy `exp` value in the payload
3. Go to [https://www.epochconverter.com/](https://www.epochconverter.com/) and verify if the exp value is expired.

Let me also mention that you might hit the capacity for your monthly M2M authentication calls if you issue a new access token for every request. In general, you should cache the access token and renew it only once it has expired.

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![alan.h](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alan.h/32/70826_2.png) [@alan.h](https://community.auth0.com/u/alan.h)\
**Post date:** [February 18, 2025, 7:49pm UTC](https://community.auth0.com/t/expired-management-api-token-with-node-auth0/181982/4 "2025-02-18T19:49:22Z")

</div>

Hi @rueben.tiow,

I ended up making a manual access token request using an `AuthenticationClient` and the `clientCredentialsGrant` method.

```auto
    const client = new AuthenticationClient(config)

    const audience = `https://${config.domain}/api/v2/`
    const {
      data: { access_token: newToken },
    } = await client.oauth.clientCredentialsGrant({ audience }, { initOverrides: IGNORE_CACHE })

export const IGNORE_CACHE: InitOverride = ({ init }) =>
  Promise.resolve({
    cache: "no-store",
    headers: { ...init.headers, "Cache-Control": "no-store" },
  })

```

I then pass this token into the `ManagementClient` instead of relying on it internally fetching an access token, which looks to have fixed my issue. The `IGNORE_CACHE` headers are probably relevant; I had similar issues previously where my Management API request responses were getting cached.

I’m also now checking the expiration to ensure I’m not requesting a new token before the current one has expired, thanks for the callout!

My previous confusion was due to the fact that the expired token error was only being hit when I was trying to fetch users / roles, but not invites. I’d assume that if the access token is expired, it should appear expired to all endpoints (and likewise for valid access tokens).

Regardless, my issue is now resolved, thanks again!

---

<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:** [February 18, 2025, 10:51pm UTC](https://community.auth0.com/t/expired-management-api-token-with-node-auth0/181982/5 "2025-02-18T22:51:07Z")

</div>

Hi @alan.h,

Thanks for reply and sharing your approach with the rest of the Community!

Please reach out to us again if you have any additional questions.

Cheers,  
Rueben

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [March 4, 2025, 10:51pm UTC](https://community.auth0.com/t/expired-management-api-token-with-node-auth0/181982/6 "2025-03-04T22:51:24Z")

</div>

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