Backend-only SDK
When creating a backend-only SDK, you have two options for implementing the BAPI endpoints: either develop a backend SDK that encompasses all BAPI endpoints or create an SDK tailored for an existing backend framework.
The source of truth for all BAPI endpoints is the BAPI reference docs. For Node.js backend frameworks, you can build on top of the JavaScript Backend SDK.
Expected features
- User only needs to provide their Secret Key
- Centralized request authentication (e.g. in a middleware or plugin)
- Give access to the instance of BAPI client (so that users can use all methods)
- User should be able to limit access to routes by checking for roles and permissions
If you're using @clerk/backend
to build an SDK for an existing framework, these additional features are expected:
- User should be able to use all
@clerk/backend
options
Optional features
- User should be able to enforce authentication on individual routes (e.g. with a
requireAuth
helper) - Use singleton pattern to only create a pre-configured instance of Clerk backend client
Implementation: BAPI
You can manually create a wrapper library around the BAPI OpenAPI or use one the many automatic SDK generation tools that take in OpenAPI definitions.
Implementation: Node.js backend framework
@clerk/backend
is built for Node.js/V8 isolates (Cloudflare Workers, Vercel Edge Runtime, etc.). It’s the foundational package for all JavaScript Backend SDKs and works across all JavaScript runtimes. By using @clerk/backend
you can be sure to communicate with Clerk’s BAPI in a correct and secure way.
Create a Clerk client
Use createClerkClient
from @clerk/backend
to create your default Clerk client which will be used for the middleware.
Create your middleware/plugin
Inside the middleware, you’ll use the user-provided Clerk client (or use the default one created in the previous step) and authenticate the request. authenticateRequest
returns Promise<RequestState>
. The middleware should set requestState.toAuth()
into its context as this will contain the resolved signed-in/signed-out Auth
object. This way other helpers can access it later in the chain.
Create a getAuth
helper
This utility will access the stored requestState
(in the example above saved as clerkAuth
) and return it.
Your end-users can use this utility in cases like these:
Create a requireAuth
helper
This utility will require auth requests for user authenticated or authorized requests. An HTTP 401
status code is returned for unauthorized requests.
Your end-users can use this utility in cases like these:
Feedback
Last updated on