Overview
ZapDev uses Clerk webhooks to receive real-time notifications about user authentication events, profile updates, and organization changes. This ensures the application stays synchronized with Clerk’s user management system.Endpoint URL
Configuration
Environment Variables
Add this to your.env.local file:
Clerk Dashboard Setup
- Log in to your Clerk Dashboard
- Select your application
- Navigate to Webhooks in the sidebar
- Click Add Endpoint
- Enter your endpoint URL:
https://yourdomain.com/api/webhooks/clerk - Select the events you want to receive:
user.createduser.updatedorganization.createdorganization.updated
- Copy the Signing Secret to your environment variables
Webhook Verification
All incoming webhooks are verified using the Svix library (Clerk’s webhook provider):src/app/api/webhooks/clerk/route.ts:7-44.
Supported Events
User Events
user.created
Fired when a new user signs up or is created in Clerk.
Payload Example:
src/app/api/webhooks/clerk/route.ts:52-56.
Note: User data is currently logged only. Future implementations may sync user profiles to Convex database or trigger onboarding workflows.
user.updated
Fired when user profile information changes (name, email, avatar, etc.).
Payload Example:
src/app/api/webhooks/clerk/route.ts:52-56.
Organization Events
organization.created
Fired when a new organization is created.
Payload Example:
src/app/api/webhooks/clerk/route.ts:59-63.
organization.updated
Fired when organization details change (name, settings, etc.).
Payload Example:
src/app/api/webhooks/clerk/route.ts:59-63.
Event Processing
The webhook handler uses a switch statement to route events:src/app/api/webhooks/clerk/route.ts:46-75.
Testing Webhooks
Using Clerk Dashboard
- Go to Webhooks in your Clerk Dashboard
- Click on your webhook endpoint
- Navigate to the Testing tab
- Select an event type (e.g.,
user.created) - Click Send Example
- Verify the webhook was received in your application logs
Local Development with ngrok
For local testing, expose your development server:- Copy the ngrok HTTPS URL (e.g.,
https://abc123.ngrok.io) - Update your Clerk webhook endpoint to
https://abc123.ngrok.io/api/webhooks/clerk - Trigger test events from Clerk Dashboard
- Monitor your local server logs
Manual Testing with curl
Error Handling
The webhook handler includes comprehensive error handling:Missing Signature Headers
Verification Failure
Processing Errors
Security Best Practices
- Always verify signatures - Never process webhooks without signature verification
- Use HTTPS - Clerk requires HTTPS endpoints in production
- Validate event types - Only process expected event types
- Log errors - Monitor webhook failures for debugging
- Idempotency - Design event handlers to be idempotent (safe to retry)
- Rate limiting - Consider implementing rate limits on your webhook endpoint
Common Issues
Webhook Secret Not Found
Error:CLERK_WEBHOOK_SECRET to your environment variables.
Signature Verification Failed
Error:- Verify the webhook secret matches your Clerk Dashboard
- Check that the request body is not modified before verification
- Ensure you’re using the raw request body (not parsed JSON)
Missing Svix Headers
Error:svix-id, svix-timestamp, svix-signature).
Future Enhancements
Potential improvements to the webhook handler:- User Profile Sync - Store user data in Convex for faster access
- Organization Management - Sync organization data and team memberships
- Event Logging - Store webhook events using
api.webhooks.logWebhookEvent - Onboarding Triggers - Trigger welcome emails or onboarding flows for new users
- Analytics - Track user signup and profile update metrics
- Polar Integration - Link Clerk users with Polar customers for subscription management
Related Files
- Webhook handler:
src/app/api/webhooks/clerk/route.ts - Clerk configuration:
src/middleware.ts(authentication middleware) - Event logging utilities:
convex/webhooks.ts