npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@brianmmdev/clerk-webhooks-handler

v1.0.5

Published

A package to streamline the configuration of Clerk webhooks in a Next.js application.

Downloads

765

Readme

Clerk Webhooks Handler

A package to streamline the configuration of Clerk webhooks.

⚠ This project is not officially supported by Clerk. Use at your own discretion.

Project goals

The goal of this package is to make it easier to work with Clerk webhooks by abstracting away the process of parsing the incoming event type and payload, as well as automatically validating the signature of the incoming request.

The result is a single method that uses callback functions and passes through fully-typed versions of the supported payloads, making it simpler to perform operations such as updating database records, sending custom notifications, and any other custom logic that you'd like to build.

Installation

This package is deployed to NPM and can be installed with the following command:

npm install @brianmmdev/clerk-webhooks-handler

Environment Variables

To run this project, you will need to add the following environment variables to your .env file:

  • WEBHOOK_SECRET &emdash; The value obtained from the Clerk dashboard when registering a new endpoint.

Usage/Examples

While this package can theoretically be used with any JavaScript web framework, it has only been tested with Next.js as of now, so this example will be using Next.js.

  1. Create a route handler at your preferred location.
  2. Create an instance of createWebhooksHandler, passing in a configuration object with a series of callback functions.
  3. Export the handler's POST method as POST so Next.js will pass web requests into it.

The following example shows what this route handler would look like with the onUserUpdated which fires when a user is updated in Clerk, and onSessionCreated when a session is created.

import { createWebhooksHandler } from "@brianmmdev/clerk-webhooks-handler";

const handler = createWebhooksHandler({
  // Add/remove optional handlers to get access to the data.
  onUserUpdated: async (payload: UserJSON) => {
    // Handle the payload...
  },
  onSessionCreated: async (payload: SessionJSON) => {
    // Handle the payload...
  }
})

export const POST = handler.POST

The WebhookRegistrationConfig object defines all supported callbacks, which in turn correspond to their webhook event in Clerk:

export type WebhookRegistrationConfig = {
  secret?: string
  onUserCreated?: HandlerFn<UserJSON>;
  onUserUpdated?: HandlerFn<UserJSON>;
  onUserDeleted?: HandlerFn<DeletedObjectJSON>;
  onEmailCreated?: HandlerFn<EmailJSON>;
  onSmsCreated?: HandlerFn<SMSMessageJSON>;
  onSessionCreated?: HandlerFn<SessionJSON>;
  onSessionEnded?: HandlerFn<SessionJSON>;
  onSessionRemoved?: HandlerFn<SessionJSON>;
  onSessionRevoked?: HandlerFn<SessionJSON>;
  onOrganizationCreated?: HandlerFn<OrganizationJSON>;
  onOrganizationUpdated?: HandlerFn<OrganizationJSON>;
  onOrganizationDeleted?: HandlerFn<DeletedObjectJSON>;
  onOrganizationMembershipCreated?: HandlerFn<OrganizationMembershipJSON>;
  onOrganizationMembershipDeleted?: HandlerFn<OrganizationMembershipJSON>;
  onOrganizationMembershipUpdated?: HandlerFn<OrganizationMembershipJSON>;
  onOrganizationInvitationAccepted?: HandlerFn<OrganizationInvitationJSON>;
  onOrganizationInvitationCreated?: HandlerFn<OrganizationInvitationJSON>;
  onOrganizationInvitationRevoked?: HandlerFn<OrganizationInvitationJSON>;
}

In the event that a callback is not defined for the corresponding webhook, the package will respond with a 404 status to the caller.

Contributing

Contributions are always welcome! To contribute to this project, fork it into your own GitHub account or organization, make the necessary changes, and create a pull request into this repository.

If I do not respond in a timely manner, feel free to ping or DM me on Twitter: @brianmmdev

Feedback

If you have any feedback, please reach out to me on Twitter: @brianmmdev

To report issues or suggest improvements, feel free to create an issue.