Clerk Webhook
Webhooks
Clerk Webhook
Handle user lifecycle events from Clerk authentication
POST
Clerk Webhook
Overview
This webhook endpoint receives user lifecycle events from Clerk authentication service. It automatically syncs user data between Clerk and the SkillRise database.Authentication
Webhook Signature Verification
Clerk uses Svix for webhook delivery. All requests are verified using HMAC signatures. Required Headers:svix-id: Unique message identifiersvix-timestamp: Unix timestamp when the webhook was sentsvix-signature: HMAC signature for verification
CLERK_WEBHOOK_SECRET environment variable:
Event Types
user.created
Fired when a new user signs up through Clerk. Action: Creates a new user record in the SkillRise database. Payload Example:_id: Clerk user IDemail: Primary email addressname: Concatenation of first_name and last_nameimageUrl: Profile image URL
user.updated
Fired when a user updates their profile in Clerk. Action: Updates the existing user record in the database. Payload Example:emailnameimageUrl
user.deleted
Fired when a user account is deleted in Clerk. Action: Deletes the corresponding user record from the database. Payload Example:Request Format
Headers
| Header | Type | Required | Description |
|---|---|---|---|
svix-id | string | Yes | Unique message identifier |
svix-timestamp | string | Yes | Unix timestamp of webhook delivery |
svix-signature | string | Yes | HMAC signature for verification |
Content-Type | string | Yes | Must be application/json |
Body
Response Format
Success Response
Status Code:200 OK
Error Response
Status Code:500 Internal Server Error
Error Handling
The endpoint implements comprehensive error handling:- Signature Verification Failure: If the Svix signature verification fails, an error is thrown and caught by the error handler
- Database Errors: Any database operation failures are caught and return a 500 error
- Unknown Event Types: Events with unrecognized types are silently acknowledged with an empty response
Unknown event types return a successful empty response to prevent Clerk from retrying the webhook.
Security Best Practices
- Environment Variables: Store
CLERK_WEBHOOK_SECRETsecurely in environment variables - Signature Verification: Always verify the Svix signature before processing any webhook data
- HTTPS Only: Configure Clerk to only send webhooks to HTTPS endpoints in production
- Idempotency: The endpoint handles duplicate webhooks gracefully (e.g., update operations are idempotent)
Configuration
Clerk Dashboard Setup
- Navigate to Webhooks in your Clerk dashboard
- Click Add Endpoint
- Enter your endpoint URL:
https://yourdomain.com/clerk - Select events to subscribe to:
user.createduser.updateduser.deleted
- Copy the Signing Secret and set it as
CLERK_WEBHOOK_SECRET
Environment Variables
Implementation Reference
Location:server/controllers/webhooks.js:8