Eldersonar
Eldersonar20h ago

Getting all organization users by org id from clerk

hi, guys. Newby here. I have come here to ask for help. I am currently trying to get all clerk users of the same organization authenticated user is part of. Can anyone help me understand how I can achieve it? Is it a good place to ask such a question? My stack is convex/clerk/nextjs
8 Replies
Convex Bot
Convex Bot20h ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
Eldersonar
EldersonarOP20h ago
Perhaps I could set up webhooks of Clerk and listen to them on my app, then make updates to the users table in convex, but I was wondering if there is an easier, more streamlined way to directly query clerk
ballingt
ballingt20h ago
Have you done this with Clerk before outside of Convex? Clerk has APIs you cna call from Convex actions to do things like this
Eldersonar
EldersonarOP20h ago
I have not this is my initial attempt
ballingt
ballingt20h ago
here's some API stuff https://clerk.com/docs/reference/backend-api/tag/Users let's see what's a nice way to use it
ballingt
ballingt20h ago
I just asked Claude and got this
No description
ballingt
ballingt20h ago
so then the issue is "can we run that Clerk SDK inside a COnvex action" ok looks like that library is deprecated but this one https://github.com/clerk/javascript does somethin gsimilar https://clerk.com/docs/references/javascript/overview I think that library does work in Convex actions, I've used it before so instead of syncing, every time you want to talk to Clerk you can run code like this Generally I prefer the webhook syncing, but this should work
Eldersonar
EldersonarOP19h ago
looking ok, so I came up with a script that utilizes the clerk sdk and fetches users by org, however, Convex is giving me hard time. path - convex/users.ts import { query } from "./_generated/server"; import { v } from "convex/values"; import { clerkClient } from "@clerk/nextjs/server"; import type { OrganizationMembership, User } from "@clerk/nextjs/server"; export const getOrganizationMembers = query({ args: { organizationId: v.string(), }, handler: async (ctx, { organizationId }) => { try { // Get organization memberships const membershipsResponse = await clerkClient.organizations.getOrganizationMembershipList({ organizationId });
// Extract user IDs const userIds = membershipsResponse.data .map((m: OrganizationMembership) => m.publicUserData?.userId) .filter((id): id is string => id !== undefined);
if (userIds.length === 0) { return []; } // Get user details const users = await clerkClient.users.getUserList({ userId: userIds });
return users.data as User[]; } catch (error) { console.error("Failed to fetch organization members:", error); throw new Error("Failed to fetch organization members"); } } }); ⠇ Preparing Convex functions... X [ERROR] Could not resolve "node:async_hooks" node_modules/@clerk/nextjs/dist/esm/server/clerkMiddleware.js:1:34:
1 │ import { AsyncLocalStorage } from "node:async_hooks"; ╵ ~~~~~~ The package "node:async_hooks" wasn't found on the file system but is
built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error. I tried to get AI assistance and was told to add a convex.json file in the root of my directory and populate it with { "bundler": { "platform": "node" } } it didn't work. What am I doing wrong here? @Tom thoughts? using "@clerk/nextjs": "^5.7.5" is this a version incompatibility issue?

Did you find this page helpful?