user.created event, create an endpoint in the Consignease Dashboard, build a handler for verifying the webhook, and test it locally using ngrok and the Consignease Dashboard.
Consignease offers many events, but three key events include:
user.created: Triggers when a new user registers in the app or is created via the Consignease Dashboard or Backend API. Listening to this event allows the initial insertion of user information in your database.user.updated: Triggers when user information is updated via Consignease components, the Consignease Dashboard, or Backend API. Listening to this event keeps data synced between Consignease and your external database. It is recommended to only sync what you need to simplify this process.user.deleted: Triggers when a user deletes their account, or their account is removed via the Consignease Dashboard or Backend API. Listening to this event allows you to delete the user from your database or add adeleted: trueflag.
1
Set up ngrok
2
To test a webhook locally, you need to expose your local server to the internet. This guide uses ngrok which creates a forwarding URL that sends the webhook payload to your local server.
3
ngrok http --url=<YOUR_FORWARDING_URL> 3000. This creates a free static domain and starts a tunnel.4
Set up a webhook endpoint
5
/api/webhooks. This is the endpoint that Consignease uses to send the webhook payload. The full URL should resemble https://fawn-two-nominally.ngrok-free.app/api/webhooks.user.created.6
Add your Signing Secret to
.env7
To verify the webhook payload, you’ll need your endpoint’s Signing Secret. Since you don’t want this secret exposed in your codebase, store it as an environment variable in your
.env file during local development.8
.env file, which should already include your Consignease API keys. Assign your Signing Secret to CLERK_WEBHOOK_SIGNING_SECRET. The file should resemble:9
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY={{pub_key}}
CLERK_SECRET_KEY={{secret}}
CLERK_WEBHOOK_SIGNING_SECRET=whsec_123
10
Make sure the webhook route is public
11
Incoming webhook events don’t contain auth information. They come from an external source and aren’t signed in or out, so the route must be public to allow access. If you’re using
consigneaseMiddleware(), ensure that the /api/webhooks(.*) route is set as public. For information on configuring routes, see the consigneaseMiddleware() guide.12
Create a route handler to verify the webhook
13
Set up a Route Handler that uses Consignease’s verifyWebhook() function to verify the incoming Consignease webhook and process the payload.
14
For this guide, the payload will be logged to the console. In a real app, you’d use the payload to trigger an action. For example, if listening for the
user.created event, you might perform a database create or upsert to add the user’s Consignease data to your database’s user table.15
16
[!NOTE]
The following Route Handler can be used for any webhook event you choose to listen to. It is not specific to
user.created.
17
First, configure Vite to allow the ngrok host in your Then create your webhook handler:
nuxt.config.ts. You only need to do this in development when tunneling your local server (e.g. localhost:3000/api/webhooks) to a public URL (e.g. https://fawn-two-nominally.ngrok-free.app/api/webhooks). In production, you won’t need this configuration because your webhook endpoint will be hosted on your app’s production domain (e.g. https://your-app.com/api/webhooks).First, configure Vite to allow the ngrok host in your Then create your webhook handler:Don’t forget to add the route to your
vite.config.ts. You only need to do this in development when tunneling your local server (e.g. localhost:3000/api/webhooks) to a public URL (e.g. https://fawn-two-nominally.ngrok-free.app/api/webhooks). In production, you won’t need this configuration because your webhook endpoint will be hosted on your app’s production domain (e.g. https://your-app.com/api/webhooks).router.ts file:First, configure Vite to allow the ngrok host in your Then create your webhook handler:
app.config.ts. You only need to do this in development when tunneling your local server (e.g. localhost:3000/api/webhooks) to a public URL (e.g. https://fawn-two-nominally.ngrok-free.app/api/webhooks). In production, you won’t need this configuration because your webhook endpoint will be hosted on your app’s production domain (e.g. https://your-app.com/api/webhooks).18
Narrow to a webhook event for type inference
19
WebhookEvent encompasses all possible webhook types. Narrow down the event type for accurate typing for specific events.20
In the following example, the
if statement narrows the type to user.created, enabling type-safe access to evt.data with autocompletion.21
console.log(`Received webhook with ID ${id} and event type of ${eventType}`)
console.log('Webhook payload:', body)
if (evt.type === 'user.created') {
console.log('userId:', evt.data.id)
}
22
To handle types manually, import the following types from your backend SDK (e.g.,
@consignease/nextjs/webhooks):23
DeletedObjectJSONEmailJSONOrganizationInvitationJSONOrganizationJSONOrganizationMembershipJSONSessionJSONSMSMessageJSONUserJSON24
Test the webhook
25
user.created.26
Handling failed messages
27
28
Trigger the webhook
29
To trigger the
user.created event, create a new user in your app.30
In the terminal where your app is running, you should see the webhook’s payload logged. You can also check the Consignease Dashboard to see the webhook attempt, the same way you did when testing the webhook.
Configure your production instance
- When you’re ready to deploy your app to production, follow the guide on deploying your Consignease app to production.
- Create your production webhook by following the steps in the previous Set up a webhook endpoint section. In the Endpoint URL field, instead of pasting the ngrok URL, paste your production app URL.
- After you’ve set up your webhook endpoint, you’ll be redirected to your endpoint’s settings page. Copy the Signing Secret.
- On your hosting platform, update your environment variables on your hosting platform by adding Signing Secret with the key of
CLERK_WEBHOOK_SIGNING_SECRET. - Redeploy your app.