Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sixty-regions-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/backend': patch
---

Add `event_attributes` to the `Webhook` type.
31 changes: 31 additions & 0 deletions packages/backend/src/__tests__/webhooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,35 @@ describe('verifyWebhook', () => {
// All should be treated as missing
await expect(verifyWebhook(mockRequest)).rejects.toThrow('svix-id, svix-timestamp, svix-signature');
});

it('should parse event_attributes', async () => {
const clerkPayload = JSON.stringify({
type: 'user.created',
data: { id: 'user_123', email: '[email protected]' },
event_attributes: {
http_request: {
client_ip: '127.0.0.1',
user_agent: 'Mozilla/5.0 (Test)',
},
},
});
const svixId = 'msg_123';
const svixTimestamp = (Date.now() / 1000).toString();
const validSignature = createValidSignature(svixId, svixTimestamp, clerkPayload);

const mockRequest = new Request('https://clerk.com/webhooks', {
method: 'POST',
body: clerkPayload,
headers: new Headers({
'svix-id': svixId,
'svix-timestamp': svixTimestamp,
'svix-signature': validSignature,
}),
});

const result = await verifyWebhook(mockRequest, { signingSecret: mockSecret });
expect(result).toHaveProperty('type', 'user.created');
expect(result).toHaveProperty('event_attributes.http_request.client_ip', '127.0.0.1');
expect(result).toHaveProperty('event_attributes.http_request.user_agent', 'Mozilla/5.0 (Test)');
});
});
9 changes: 8 additions & 1 deletion packages/backend/src/api/resources/Webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ import type {
WaitlistEntryJSON,
} from './JSON';

type Webhook<EvtType, Data> = { type: EvtType; object: 'event'; data: Data };
type WebhookEventAttributes = {
http_request: {
client_ip: string;
user_agent: string;
};
};

type Webhook<EvtType, Data> = { type: EvtType; object: 'event'; data: Data; event_attributes: WebhookEventAttributes };

export type UserWebhookEvent =
| Webhook<'user.created' | 'user.updated', UserJSON>
Expand Down
1 change: 1 addition & 0 deletions packages/backend/src/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export async function verifyWebhook(request: Request, options: VerifyWebhookOpti
type: payload.type,
object: 'event',
data: payload.data,
event_attributes: payload.event_attributes,
} as WebhookEvent;
} catch (e) {
return errorThrower.throw(`Unable to verify incoming webhook: ${e instanceof Error ? e.message : 'Unknown error'}`);
Expand Down