To reduce bundle size (e.g., for Cloudflare Workers), import specific resource clients (like UsersClient or ClientsClient) from their own entry points instead of the full ManagementClient.
To avoid repeating authentication logic, use createManagementAuth from auth0/management. This utility handles token fetching and refreshing via client credentials and returns an options object that can be spread into any sub-client. The token is cached and shared across clients.
Best Practices for Small Bundles:
- Import clients as values from specific entry points (e.g.,
auth0/users). - Import request/response types using
import type { Management } from "auth0" to ensure they are erased at compile time. - Reuse client instances rather than constructing them per request.
import { createManagementAuth } from "auth0/management";
import { ClientsClient } from "auth0/clients";
import { UsersClient } from "auth0/users";
// Configure auth once and reuse it across clients.
const auth = createManagementAuth({
domain: "{YOUR_TENANT_AND_REGION}.auth0.com",
clientId: "{YOUR_CLIENT_ID}",
clientSecret: "{YOUR_CLIENT_SECRET}",
});
// Create each sub-client once and reuse the instances throughout your app.
export const clients = new ClientsClient(auth.clientOptions);
export const users = new UsersClient(auth.clientOptions);
await users.list({ page: 0, per_page: 10 });