rae.ki
rae.ki2mo ago

Setting up Convex with Clerk

I followed the guide here: https://docs.convex.dev/auth/clerk#get-started but in my Clerk webhooks dashboard, I keep getting failed status. It also doesn't remove the users from the DB when they are deleted. I attached a video of this flow, sorry about the quality. This is the relevant code snippets:
http.route({
path: "/clerk-users-webhook",
method: "POST",
handler: httpAction(async (ctx, request) => {
const event = await validateRequest(request);
if (!event) {
return new Response("Error occured", { status: 400 });
}
switch (event.type) {
...
case "user.deleted": {
const clerkUserId = event.data.id!;
await ctx.runMutation(internal.users.deleteFromClerk, { clerkUserId });
break;
}
...
return new Response(null, { status: 200 });
}),
});
...
export const deleteFromClerk = internalMutation({
args: { clerkUserId: v.string() },
async handler(ctx, { clerkUserId }) {
const user = await userByExternalId(ctx, clerkUserId);

if (user !== null) {
await ctx.db.delete(user._id);
return;
} else {
console.warn(
`Can't delete user, there is none for Clerk user ID: ${clerkUserId}`,
);
return;
}
},
});
http.route({
path: "/clerk-users-webhook",
method: "POST",
handler: httpAction(async (ctx, request) => {
const event = await validateRequest(request);
if (!event) {
return new Response("Error occured", { status: 400 });
}
switch (event.type) {
...
case "user.deleted": {
const clerkUserId = event.data.id!;
await ctx.runMutation(internal.users.deleteFromClerk, { clerkUserId });
break;
}
...
return new Response(null, { status: 200 });
}),
});
...
export const deleteFromClerk = internalMutation({
args: { clerkUserId: v.string() },
async handler(ctx, { clerkUserId }) {
const user = await userByExternalId(ctx, clerkUserId);

if (user !== null) {
await ctx.db.delete(user._id);
return;
} else {
console.warn(
`Can't delete user, there is none for Clerk user ID: ${clerkUserId}`,
);
return;
}
},
});
4 Replies
Sorin
Sorin2mo ago
if you get the failed status for the requests in the Clerk dash you should first tshoot that. 1. put a console log in httpAction BEFORE the validate call. > this will validate the requests gets into convex. if you get nothing, check domains and paths. 2. put a log after validation. maybe you did not add the webhook secret from clerk into conved env vars. 3. only after you see the log after the validation go after deletion issues. i see that the user gets created, but you still get a error in clerk dash. didi you also chek convex logs?
rae.ki
rae.kiOP2mo ago
I hvae the env vars set up and in convex as well, I tried putting console logs and when I run the functions from convex, it doesn't show much. am I checking the wrong place? I fixed it by following this guide instead: https://clerk.com/docs/webhooks/sync-data I think the issue was this "To test a webhook locally, you need to expose your local server to the internet."
re
re2w ago
Hi, did you figure thhis out? Did you sync your Clerk users to your Convex DB? @rae.ki
rae.ki
rae.kiOP2w ago
Yes I have see this message here

Did you find this page helpful?