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!