Skip to content

Commit cd6ee92

Browse files
wobsorianoLekoArts
andauthored
feat(backend,react-router,nuxt,astro,nextjs,tanstack-react-start,express,fastify): Add verify webhook function (#5468)
Co-authored-by: Lennart <lekoarts@gmail.com>
1 parent 243b153 commit cd6ee92

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+744
-24
lines changed

.changeset/brave-turkeys-burn.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
'@clerk/tanstack-react-start': minor
3+
---
4+
5+
Introduce a `verifyWebhook()` function to verify incoming Clerk webhook requests and process the payload. This function handles webhook signature verification using `Svix` and is now available across all backend and fullstack SDKs.
6+
7+
To get started, install [`svix`](https://www.npmjs.com/package/svix), which Clerk uses to verify its webhooks:
8+
9+
```shell
10+
npm install svix
11+
```
12+
13+
Then in your webhook route handler, import `verifyWebhook()` from the TanStack React Start SDK:
14+
15+
```ts
16+
// pages/api/webhooks.ts
17+
import { verifyWebhook } from '@clerk/tanstack-react-start/webhooks';
18+
19+
export const APIRoute = createAPIFileRoute('/api/webhooks')({
20+
POST: async ({ request }) => {
21+
try {
22+
const evt = await verifyWebhook(req);
23+
24+
// Do something with payload
25+
const { id } = evt.data;
26+
const eventType = evt.type;
27+
console.log(`Received webhook with ID ${id} and event type of ${eventType}`);
28+
console.log('Webhook payload:', body);
29+
30+
return new Response('Webhook received', { status: 200 });
31+
} catch (err) {
32+
console.error('Error: Could not verify webhook:', err);
33+
return new Response('Error: Verification error', {
34+
status: 400,
35+
});
36+
}
37+
}
38+
})
39+
```
40+
41+
For more information on how to sync Clerk data to your app with webhooks, [see our guide](https://clerk.com/docs/webhooks/sync-data).

.changeset/bright-crabs-fold.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
'@clerk/fastify': minor
3+
---
4+
5+
Introduce a `verifyWebhook()` function to verify incoming Clerk webhook requests and process the payload. This function handles webhook signature verification using `Svix` and is now available across all backend and fullstack SDKs.
6+
7+
To get started, install [`svix`](https://www.npmjs.com/package/svix), which Clerk uses to verify its webhooks:
8+
9+
```shell
10+
npm install svix
11+
```
12+
13+
Then in your webhook route handler, import `verifyWebhook()` from the Fastify SDK:
14+
15+
```ts
16+
import { verifyWebhook } from '@clerk/fastify/webhooks';
17+
18+
fastify.post('/api/webhooks', async (request, reply) => {
19+
try {
20+
const evt = await verifyWebhook(request);
21+
22+
// Do something with payload
23+
const { id } = evt.data;
24+
const eventType = evt.type;
25+
console.log(`Received webhook with ID ${id} and event type of ${eventType}`);
26+
console.log('Webhook payload:', evt.data);
27+
28+
return reply.status(200).send('Webhook received');
29+
} catch (err) {
30+
console.log('Error: Could not verify webhook:', err);
31+
return reply.status(400).send('Error: Verification error');
32+
}
33+
})
34+
```
35+
36+
For more information on how to sync Clerk data to your app with webhooks, [see our guide](https://clerk.com/docs/webhooks/sync-data).

.changeset/curly-chicken-lick.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
'@clerk/react-router': minor
3+
---
4+
5+
Introduce a `verifyWebhook()` function to verify incoming Clerk webhook requests and process the payload. This function handles webhook signature verification using `Svix` and is now available across all backend and fullstack SDKs.
6+
7+
To get started, install [`svix`](https://www.npmjs.com/package/svix), which Clerk uses to verify its webhooks:
8+
9+
```shell
10+
npm install svix
11+
```
12+
13+
Then in your webhook route handler, import `verifyWebhook()` from the React Router SDK:
14+
15+
```ts
16+
import { verifyWebhook } from '@clerk/react-router/webhooks';
17+
18+
export const action = async ({ request }) => {
19+
try {
20+
const evt = await verifyWebhook(request);
21+
22+
// Do something with payload
23+
const { id } = evt.data;
24+
const eventType = evt.type;
25+
console.log(`Received webhook with ID ${id} and event type of ${eventType}`);
26+
console.log('Webhook payload:', evt.data);
27+
28+
return new Response('Webhook received', { status: 200 });
29+
} catch (err) {
30+
console.log('Error: Could not verify webhook:', err);
31+
return new Response('Error: Verification error', {
32+
status: 400
33+
});
34+
}
35+
};
36+
```
37+
38+
For more information on how to sync Clerk data to your app with webhooks, [see our guide](https://clerk.com/docs/webhooks/sync-data).

.changeset/forty-radios-drive.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
"@clerk/backend": minor
3+
---
4+
5+
Introduce a `verifyWebhook()` function to verify incoming Clerk webhook requests and process the payload. This function handles webhook signature verification using `Svix` and is now available across all backend and fullstack SDKs.
6+
7+
To get started, install [`svix`](https://www.npmjs.com/package/svix), which Clerk uses to verify its webhooks:
8+
9+
```shell
10+
npm install svix
11+
```
12+
13+
Then in your webhook route handler, import `verifyWebhook()` from the Backend SDK:
14+
15+
```ts
16+
// app/api/webhooks/route.ts
17+
import { verifyWebhook } from '@clerk/backend/webhooks';
18+
19+
try {
20+
const evt = await verifyWebhook(req);
21+
22+
// Do something with payload
23+
const { id } = evt.data;
24+
const eventType = evt.type;
25+
console.log(`Received webhook with ID ${id} and event type of ${eventType}`);
26+
console.log('Webhook payload:', body);
27+
} catch (err) {
28+
console.error('Error: Could not verify webhook:', err);
29+
}
30+
```
31+
32+
For more information on how to sync Clerk data to your app with webhooks, [see our guide](https://clerk.com/docs/webhooks/sync-data).

.changeset/fruity-ghosts-scream.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
"@clerk/express": minor
3+
---
4+
5+
Introduce a `verifyWebhook()` function to verify incoming Clerk webhook requests and process the payload. This function handles webhook signature verification using `Svix` and is now available across all backend and fullstack SDKs.
6+
7+
To get started, install [`svix`](https://www.npmjs.com/package/svix), which Clerk uses to verify its webhooks:
8+
9+
```shell
10+
npm install svix
11+
```
12+
13+
Then in your webhook route handler, import `verifyWebhook()` from the Express SDK:
14+
15+
```ts
16+
import { verifyWebhook } from '@clerk/express/webhooks';
17+
18+
app.post(
19+
'/api/webhooks',
20+
bodyParser.raw({ type: 'application/json' }),
21+
22+
async (req, res) => {
23+
try {
24+
const evt = await verifyWebhook(req);
25+
26+
// Do something with payload
27+
const { id } = evt.data;
28+
const eventType = evt.type;
29+
console.log(`Received webhook with ID ${id} and event type of ${eventType}`);
30+
console.log('Webhook payload:', body);
31+
32+
return res.status(200).send('Webhook received')
33+
} catch (err) {
34+
console.log('Error: Could not verify webhook:', err.message)
35+
return res.status(400).send('Error: Verification error')
36+
}
37+
},
38+
)
39+
```
40+
41+
For more information on how to sync Clerk data to your app with webhooks, [see our guide](https://clerk.com/docs/webhooks/sync-data).

.changeset/olive-apples-return.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
"@clerk/nextjs": minor
3+
---
4+
5+
Introduce a `verifyWebhook()` function to verify incoming Clerk webhook requests and process the payload. This function handles webhook signature verification using `Svix` and is now available across all backend and fullstack SDKs.
6+
7+
To get started, install [`svix`](https://www.npmjs.com/package/svix), which Clerk uses to verify its webhooks:
8+
9+
```shell
10+
npm install svix
11+
```
12+
13+
Then in your webhook route handler, import `verifyWebhook()` from the Next.js SDK:
14+
15+
```ts
16+
// app/api/webhooks/route.ts
17+
import { verifyWebhook } from '@clerk/nextjs/webhooks';
18+
19+
export async function POST(req: Request) {
20+
try {
21+
const evt = await verifyWebhook(req);
22+
23+
// Do something with payload
24+
const { id } = evt.data;
25+
const eventType = evt.type;
26+
console.log(`Received webhook with ID ${id} and event type of ${eventType}`);
27+
console.log('Webhook payload:', body);
28+
29+
return new Response('Webhook received', { status: 200 });
30+
} catch (err) {
31+
console.error('Error: Could not verify webhook:', err);
32+
return new Response('Error: Verification error', {
33+
status: 400,
34+
});
35+
}
36+
}
37+
```
38+
39+
For more information on how to sync Clerk data to your app with webhooks, [see our guide](https://clerk.com/docs/webhooks/sync-data).

.changeset/quick-jars-give.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
'@clerk/astro': minor
3+
---
4+
5+
Introduce a `verifyWebhook()` function to verify incoming Clerk webhook requests and process the payload. This function handles webhook signature verification using `Svix` and is now available across all backend and fullstack SDKs.
6+
7+
To get started, install [`svix`](https://www.npmjs.com/package/svix), which Clerk uses to verify its webhooks:
8+
9+
```shell
10+
npm install svix
11+
```
12+
13+
Then in your webhook route handler, import `verifyWebhook()` from the Astro SDK:
14+
15+
```ts
16+
// pages/api/webhooks.ts
17+
import { verifyWebhook } from '@clerk/astro/webhooks';
18+
19+
export const POST = ({ request }) => {
20+
try {
21+
const evt = await verifyWebhook(request);
22+
23+
// Do something with payload
24+
const { id } = evt.data;
25+
const eventType = evt.type;
26+
console.log(`Received webhook with ID ${id} and event type of ${eventType}`);
27+
console.log('Webhook payload:', body);
28+
29+
return new Response('Webhook received', { status: 200 });
30+
} catch (err) {
31+
console.error('Error: Could not verify webhook:', err);
32+
return new Response('Error: Verification error', {
33+
status: 400,
34+
});
35+
}
36+
}
37+
```
38+
39+
For more information on how to sync Clerk data to your app with webhooks, [see our guide](https://clerk.com/docs/webhooks/sync-data).

.changeset/upset-vans-do.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
'@clerk/nuxt': minor
3+
---
4+
5+
Introduce a `verifyWebhook()` function to verify incoming Clerk webhook requests and process the payload. This function handles webhook signature verification using `Svix` and is now available across all backend and fullstack SDKs.
6+
7+
To get started, install [`svix`](https://www.npmjs.com/package/svix), which Clerk uses to verify its webhooks:
8+
9+
```shell
10+
npm install svix
11+
```
12+
13+
Then in your webhook route handler, import `verifyWebhook()` from the Nuxt SDK:
14+
15+
```ts
16+
// server/api/webhooks.post.ts
17+
import { verifyWebhook } from '@clerk/nuxt/webhooks';
18+
19+
export default eventHandler(async (event) => {
20+
try {
21+
const evt = await verifyWebhook(event);
22+
23+
// Do something with payload
24+
const { id } = evt.data;
25+
const eventType = evt.type;
26+
console.log(`Received webhook with ID ${id} and event type of ${eventType}`);
27+
console.log('Webhook payload:', body);
28+
29+
return 'Webhook received'
30+
} catch (err) {
31+
console.error('Error: Could not verify webhook:', err);
32+
setResponseStatus(event, 400)
33+
return 'Error: Verification error';
34+
}
35+
})
36+
```
37+
38+
For more information on how to sync Clerk data to your app with webhooks, [see our guide](https://clerk.com/docs/webhooks/sync-data).

packages/astro/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
"types": "./dist/server/index.d.ts",
5656
"default": "./dist/server/index.js"
5757
},
58+
"./webhooks": {
59+
"types": "./dist/webhooks.d.ts",
60+
"default": "./dist/webhooks.js"
61+
},
5862
"./env": "./env.d.ts",
5963
"./components": "./components/index.ts",
6064
"./package.json": "./package.json"
@@ -67,6 +71,7 @@
6771
"server",
6872
"internal",
6973
"components",
74+
"webhooks",
7075
"env.d.ts",
7176
"types.ts"
7277
],

packages/astro/src/webhooks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '@clerk/backend/webhooks';

0 commit comments

Comments
 (0)