Conventions
Ideally, when a user switches from one Clerk SDK to another, the general concepts, feature set, names, and exports should be consistent. By following certain conventions, you can develop an SDK that achieves this ideal state, making it easier for users to quickly start a new project.
SDK name
Add “Clerk” as a prefix or postfix into the name as it’ll make the SDK easier to discover. Here are some real world examples:
vue-clerk
(Vue)clerk-rs
(Rust)@hono/clerk-auth
(Hono)Clerk.Net
(C#)
Environment variables
Any environment variable used in the context of Clerk should be prefixed with CLERK_
. Learn more about Clerk environment variables.
Required
You should always support these environment variables:
CLERK_PUBLISHABLE_KEY
(Frontend-only, fullstack)CLERK_SECRET_KEY
(Backend-only, fullstack)
Additionally, you need to make the environment variables for API and SDK configuration available to your users.
Optional
Clerk recommends implementing the sign-in and sign-up redirect environment variables.
README
The README is most often the first thing a user sees of your SDK, so it's paramount to include all the crucial information.
Use the template below to setup your own README.
Publishing to npm
While this reference can't cover the whole surface area of publishing a package to npm, there are a couple of conventions Clerk uses for their own SDKs to deliver a great developer experience.
Provide TypeScript types for your SDK
This is the biggest benefit you can provide to your users, as they'll get IntelliSense autocompletion in their IDE.
You can achieve this by authoring your SDK in TypeScript or providing a hand-written file. Enable the declaration
setting in your tsconfig.json
.
When only defining a main entry point, use the types
key. Otherwise, for conditional exports, use the types
key inside the exports
field.
Use subpath exports
Use subpath exports to provide API boundaries, enable more efficient code-splitting, and split code between client & server.
Here's how the @clerk/astro
package is using subpath exports:
@clerk/astro/react
- Import Clerk's prebuilt React components@clerk/astro/client
- Access to useful stores like$authStore
@clerk/astro/server
- Import Clerk's middleware
This setup ensures that functions that only work on the server are not bundled into client-side code. It also makes it easier for the bundler to tree-shake any unused code.
CommonJS/ES Module
Authoring dual packages is challenging, but not impossible. Before deciding to publish in both CommonJS (CJS) and ES Module (ESM) formats, consider whether you can publish solely in the ESM format. Most modern frameworks support ESM-only packages.
Build tools that can help you build dual and ESM-only packages:
Define peerDependencies
and engines
Your SDK will require your users to have Node.js installed and use the framework the SDK is built for. To ensure compatibility, define peerDependencies
and engines
in your package.json
.
Generate provenance statements
Security is important at Clerk and thus all Clerk SDKs are published with provenance statements. This allows you to publicly establish where a package was built and who published a package, which can increase supply-chain security for your packages.
Read npm's guide on generating provenance statements.
Feedback
Last updated on