When new users sign up with Clerk, they are not being created in your database, causing 404 errors on API calls that require a database user record.
The Clerk webhook that should create users in your database is either:
- Not configured in the Clerk dashboard
- Not firing properly
- Failing silently
- Added better error handling and logging
- Added support for
user.updatedevents - Added detailed logging for debugging
- Created a new API endpoint that ensures users exist in the database
- Called automatically when users first access protected pages
- Fetches user info from Clerk and creates/updates database record
- Added calls to
/api/users/ensurein all protected pages:- Analytics page
- Leads page
- My Magnets page
- Create Magnet page
- Templates page
- Go to your Clerk Dashboard
- Navigate to Webhooks in the left sidebar
- Click Add Endpoint
- Set the endpoint URL to:
https://yourdomain.com/api/webhooks/clerk - Select these events:
user.createduser.updated
- Copy the signing secret and add it to your
.env.local:CLERK_WEBHOOK_SECRET=your_webhook_secret_here
- Use the test page:
http://localhost:3003/test-webhook.html - Or run the test script:
# Set your Clerk secret key export CLERK_SECRET_KEY=your_clerk_secret_key # Run the test node scripts/test-webhook.js
Look for these log messages in your server console:
🔔 Clerk webhook received✅ Webhook verified, event type: user.created✅ User created/updated in database: [user_id] [email] [role]
Check if users are being created in your database:
SELECT * FROM "User" ORDER BY "createdAt" DESC LIMIT 5;Make sure these are set in your .env.local:
CLERK_SECRET_KEY=your_clerk_secret_key
CLERK_WEBHOOK_SECRET=your_webhook_secret_here
DATABASE_URL=your_database_connection_string
- Sign up as a new user in your app
- Check server logs for webhook activity
- Check database for new user record
- Navigate to analytics/leads pages - should work without 404s
Even if the webhook fails, the new ensure endpoint will:
- Create the user when they first access a protected page
- Prevent 404 errors from occurring
- Ensure all API calls work properly
- Check Clerk dashboard webhook configuration
- Verify endpoint URL is correct
- Check if webhook secret matches
- Look for webhook delivery failures in Clerk dashboard
- Verify
DATABASE_URLis correct - Check if Prisma can connect to database
- Run
npx prisma db pushto ensure schema is up to date
- Ensure Clerk secret key has proper permissions
- Check if database user has write access
- Verify Clerk webhook can reach your endpoint
The enhanced webhook now logs:
- Webhook receipt and verification
- User creation/update success/failure
- Detailed error information
- Database operation results
Check your server console for these logs to monitor webhook health.