Back to APIs

Manage user accounts, authentication and more.

Using the Clerk API with Trigger.dev

You can use Trigger.dev with any existing Node SDK or even just using fetch. Using io.runTask makes your Clerk background job resumable and appear in our dashboard.

Use io.runTask() and the official SDK or fetch.

Use our HTTP endpoint to subscribe to webhooks

Example code using Clerk

Below is a working code example of how you can use Clerk with Trigger.dev. These samples are open source and maintained by the community, you can copy and paste them into your own projects.

1
import { TriggerClient } from "@trigger.dev/sdk";
2
import { Webhook, WebhookVerificationError } from "svix";
3
4
export const clerk = client.defineHttpEndpoint({
5
id: "clerk.com",
6
title: "Clerk",
7
source: "clerk.com",
8
icon: "clerk",
9
verify: async (
10
request: Request
11
): Promise<{ success: boolean; reason?: string }> => {
12
const body = await request.text();
13
const svixId = request.headers.get("svix-id") ?? "";
14
const svixIdTimeStamp = request.headers.get("svix-timestamp") ?? "";
15
const svixSignature = request.headers.get("svix-signature") ?? "";
16
17
if (!svixId || !svixIdTimeStamp || !svixSignature) {
18
return {
19
success: false,
20
reason: "Missing svix headers",
21
};
22
}
23
24
const svixHeaders = {
25
"svix-id": svixId,
26
"svix-timestamp": svixIdTimeStamp,
27
"svix-signature": svixSignature,
28
};
29
30
const wh = new Webhook(process.env.CLERK_WEBHOOK_SIGNING_SECRET as string);
31
32
type WebhookEvent = string;
33
34
try {
35
wh.verify(body, svixHeaders) as WebhookEvent;
36
37
return {
38
success: true,
39
};
40
} catch (err: unknown) {
41
console.log(`❌ Error message: ${(err as Error).message}`);
42
43
if (err instanceof WebhookVerificationError) {
44
return {
45
success: false,
46
reason: err.message,
47
};
48
}
49
50
return {
51
success: false,
52
reason: "Unknown error",
53
};
54
}
55
},
56
});
57
58
// Job that runs when the HTTP endpoint is called from Clerk
59
// When a contact is created or deleted
60
client.defineJob({
61
id: "http-clerk",
62
name: "HTTP Clerk",
63
version: "1.0.0",
64
enabled: true,
65
66
// Create a trigger from the HTTP endpoint
67
trigger: clerk.onRequest(),
68
run: async (request, io, ctx) => {
69
const body = await request.json();
70
await io.logger.info(`Body`, body);
71
},
72
});