Does the Auth0 ManagementClient (from the auth0 npm package) automatically renew tokens if used as a singleton?

I’m currently using the Auth0 ManagementClient from the auth0 npm package in my Next.js app in a singleton pattern like this:

import { ManagementClient } from 'auth0';

let managementClient: ManagementClient | null = null;

const getManagementClient = () => {
  if (!managementClient) {
    managementClient = new ManagementClient({
      domain: process.env.AUTH0_MANAGEMENT_API_DOMAIN,
      clientId: process.env.AUTH0_CLIENT_ID,
      clientSecret: process.env.AUTH0_CLIENT_SECRET,
    });
  }
  return managementClient;
};

My question is:

Does the ManagementClient automatically handle access token renewal after the token expires (e.g., after the default 24 hours)? Or do I need to handle the token renewal manually if I’m using a singleton like this?

If manual renewal is needed, what’s the recommended best practice for this?

Thanks in advance!

Hi @IlyaMarvinIlyashyk

Welcome to the Auth0 Community!

The management client should automatically handle the access token renewal as long as it is initiated correctly within your application.

Alternatively, the management client can be initialized this way as well:

import { ManagementClient } from 'auth0';

var management = new ManagementClient({
  domain: '{YOUR_TENANT_AND REGION}.auth0.com',
  token: '{YOUR_API_V2_TOKEN}',
});

If you have any other questions, please let me know!

Kind Regards,
Nik

1 Like