About webhooks
Integrations
While webhooks will work with any Next.js setup, this guide uses Next.js 14 with app router and the app
directory. You may need to alter some code samples if you have a different setup, but the core instructions remain the same.
In order to use webhooks, you need a publicly available URL. To test locally you need to use a proxy to expose your local instance on the internet. Here are a couple of free tools to help you to do this.
—subdomain
attribute to fix the issued subdomain.Each of these services expose a local port though a public URL that can be set as your web socket. Follow their instructions to run your local Next.js application and you will receive a URL that you need later in this guide.
Kinde sends webhooks as JWT’s to make them both easy and secure. In this example we will leverage 2 libraries to parse the JWT and verify the signature.
Create the file app/api/kinde-webhook/route.ts
. The route.ts
file is a specific file convention in Next.js that marks the route as an API route, rather than a page.
Whenever an event occurs in Kinde, a POST request is sent via this route to the specified endpoint, so that your project can react to the event. For example, refreshing a token or updating data in your database.
Note that the endpoint needs to be publicly available, with no route protection.
Install the dependencies.
Update your file as follows:
As per the sample above, the JWKs file is fetched from Kinde, the token is decoded, and the signature is compared against your keys file. This is how you know you can trust the request has come from Kinde.
In the example above, we used a switch statement. But you could also set up an endpoint per event type, group them into related endpoints, use a map, or any other method for splitting and managing the events.
Start your server so it’s ready and listening out for the next step. With most Next.js applications, run npm run dev
in the terminal.
In the Kinde, go to Settings > Environment > Webhooks.
Select Add webhook.
Give the webhook a name and description.
Enter the Next.js endpoint we set up earlier in your project. For example <your_app_url>/api/kinde-webhook
. If you are using an external tool to test the endpoint locally, enter the endpoint URL specified in the tool.
Testing will fail if the endpoint URL in Kinde does not match the site URL where you are testing the webhook.
Select Add events.
In the dialog that opens, select the events you want. For example, user events, organization events, etc.
Select Save.
In the webhook window, select Save.