Read roles for every user using auth0 SDK and not API

We have to read roles for every user using SDK. To read users using auth0 SDK from Nodejs the following statement works. users = await auth0.users.getAll(params); But users.getuserroles or readroles doesnt work.

We are using auth0 SDK and all the help are on auth0 API. What is the method name to read roles associated with users using SDK?

Hi @sathya.srinivasan,

Welcome to the Auth0 Community and thank you for your post!

In order order to retrieve the users roles using the pre-build methods of the NodeJs SDK, instead of trying “users.getuserroles” you can use the “users.getRoles” function. You can check out more about this from the NodeJs documentation on GetRoles from inside the UsersManager property.

I hope this helps!
Thanks,
Remus

Ty for your quick response. “message”: “{"message":"user.getRoles is not a function"}” . I tried and doesnt work. Do you have an example of a working code for auth0 SDK to get roles for a user? Thanks again!

Hi @sathya.srinivasan,

Essentially this would be the nodejs code presented in the GET /api/v2/users/{id}/roles endpoint as well.

import { ManagementClient } from 'auth0';

const management = new ManagementClient({
  client_id: <APPLICATION_CLIENT_ID>,
  client_secret: <APPLICATION_CLIENT_SECRET>,
  domain: <APPLICATION_DOMAIN>
});

const result = await management.users.getRoles({ id: userId });

You could also put this in a try and catch block for a better structure.

Hope this helps and if you have further questions, feel free to ask!
Kind regards,
Remus

1 Like

Thank you so much Remus! That worked. Appreciate your prompt response & help.

You’re more than welcome @sathya.srinivasan,

I’m glad this helped!
Best wishes,
Remus

Hi @remus.ivan

I notice the error below when i read 50 users (only 17 users exist in lower environment). Please help me understand the rate limits in different environments.
For every user, roles call is made.
Error ManagementApiError: Global limit has been reached
at UsersManager.parseError (file:///var/task/node_modules/auth0/dist/esm/management/management-client.js:29:16)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async UsersManager.request (file:///var/task/node_modules/auth0/dist/esm/lib/runtime.js:102:23)
at async UsersManager.getRoles (file:///var/task/node_modules/auth0/dist/esm/management/__generated/managers/users-manager.js:359:26)
at async file:///var/task/readUser.mjs:254:23
at async Promise.all (index 6)
at async Runtime.readUsers (file:///var/task/readUser.mjs:252:28) {
errorCode: ‘too_many_requests’,
error: ‘Too Many Requests’,
statusCode: 429,
body: ‘{“statusCode”:429,“error”:“Too Many Requests”,“message”:“Global limit has been reached”,“errorCode”:“too_many_requests”}’,
headers: Headers {
date: ‘Tue, 29 Apr 2025 01:00:17 GMT’,
‘content-type’: ‘application/json; charset=utf-8’,
‘content-length’: ‘120’,
connection: ‘keep-alive’,
‘cf-ray’: ‘937adc936a92b298-PDX’,
‘cf-cache-status’: ‘DYNAMIC’,
‘retry-after’: ‘1’,
‘cache-control’: ‘no-cache’,
‘strict-transport-security’: ‘max-age=31536000; includeSubDomains’,
vary: ‘origin, Accept-Encoding’,
‘access-control-expose-headers’: ‘WWW-Authenticate,Server-Authorization’,
‘x-auth0-l’: ‘0.008’,
‘x-content-type-options’: ‘nosniff’,
‘x-ratelimit-limit’: ‘10’,
‘x-ratelimit-remaining’: ‘0’,
‘x-ratelimit-reset’: ‘1745888419’,
server: ‘cloudflare’,
‘alt-svc’: ‘h3=“:443”; ma=86400’
},
msg: ‘Global limit has been reached’
}

Thanks in advance!