Skip to content

feat(backend): Add event_attributes to Webhook type #6162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 `Webhook`.
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 @@ -90,5 +90,6 @@ export async function verifyWebhook(request: Request, options: VerifyWebhookOpti
type: payload.type,
object: 'event',
data: payload.data,
event_attributes: payload.event_attributes,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add null safety for event_attributes extraction.

The code assumes payload.event_attributes exists, but it should handle cases where this property might be missing from older webhook payloads to maintain backward compatibility.

Apply this diff to add null safety:

-    event_attributes: payload.event_attributes,
+    event_attributes: payload.event_attributes || { http_request: { client_ip: '', user_agent: '' } },

Or make the property optional in the type definition if it's acceptable for it to be undefined.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
event_attributes: payload.event_attributes,
event_attributes: payload.event_attributes || { http_request: { client_ip: '', user_agent: '' } },
🤖 Prompt for AI Agents
In packages/backend/src/webhooks.ts at line 93, the code accesses
payload.event_attributes without checking if it exists, which can cause errors
with older webhook payloads. To fix this, add a null check or use optional
chaining when extracting event_attributes to safely handle cases where it might
be undefined. Alternatively, update the type definition to make event_attributes
optional if that aligns with the expected data structure.

} as WebhookEvent;
}