JavaScript Backend SDK
Clerk's JavaScript Backend SDK exposes the Backend API resources and low-level authentication utilities for JavaScript environments.
For example, if you wanted to get a list of all users in your application, instead of creating a fetch to https://api.clerk.com/v1/users
endpoint, you can use the users.getUserList()
method provided by the JavaScript Backend SDK.
Installation
If you are using the JavaScript Backend SDK on its own, you can install it using the following command:
Clerk SDKs expose an instance of the JavaScript Backend SDK for use in server environments, so there is no need to install it separately.
Usage
All resource operations are mounted as sub-APIs on the clerkClient
object. For example, if you would like to get a list of all of your application's users, you can use the getUserList()
method on the users
sub-API. You can find the full list of available sub-APIs and their methods in the sidebar.
To access a resource, you must first instantiate a clerkClient
instance.
To instantiate a clerkClient
instance, you must call createClerkClient()
and pass in options
.
Instantiate a default clerkClient
instance
You can use the default instance of clerkClient
provided by whichever SDK you are using, and skip the need to pass configuration options, unless you are using Remix. For Remix, see the following section.
To use the default clerkClient
instance, set your CLERK_SECRET_KEY
environment variable and then import the clerkClient
instance from the SDK as shown in the following example:
If you are using Remix, see the following section for how to instantiate clerkClient
.
Instantiate a custom clerkClient
instance
If you would like to customize the behavior of the JavaScript Backend SDK, you can instantiate a clerkClient
instance yourself by calling createClerkClient()
and passing in options
.
If you are using Remix, you must instantiate clerkClient
by calling the createClerkClient()
function and passing in options
.
Use the following tabs to see examples of how to use the Backend SDK in Remix Loader and Action functions.
If you are using Astro, you must pass the endpoint context when invoking the clerkClient
function.
Error handling
Backend SDK functions throw errors (ClerkAPIResponseError
) when something goes wrong. You'll need to catch them in a try/catch
block and handle them gracefully. For example:
createClerkClient({ options })
The createClerkClient()
function requires an options
object. It is recommended to set these options as environment variables where possible, and then pass them to the function. For example, you can set the secretKey
option using the CLERK_SECRET_KEY
environment variable, and then pass it to the function like this: createClerkClient({ secretKey: process.env.CLERK_SECRET_KEY })
.
The following options are available:
- Name
secretKey
(required)- Type
string
- Description
The Clerk secret key from the API Keys page in the Clerk Dashboard.
- Name
jwtKey?
- Type
string
- Description
The PEM public key from the API Keys page -> Show JWT public key -> PEM Public Key section of the Clerk Dashboard. For more information, refer to Manual JWT verification.
- Name
publishableKey?
- Type
string
- Description
The Clerk publishable key from the API Keys page in the Clerk Dashboard.
- Name
domain?
- Type
string
- Description
The domain of a satellite application in a multi-domain setup.
- Name
isSatellite?
- Type
boolean
- Description
Whether the instance is a satellite domain in a multi-domain setup. Defaults to
false
.
- Name
proxyUrl?
- Type
string
- Description
The proxy URL from a multi-domain setup.
- Name
sdkMetadata?
- Type
{ name: string, version: string }
- Description
Metadata about the SDK.
- Name
telemetry?
- Type
{ disabled: boolean, debug: boolean }
- Description
Telemetry configuration.
- Name
userAgent?
- Type
string
- Description
The User-Agent request header passed to the Clerk API.
- Name
apiUrl?
- Type
string
- Description
The Clerk Backend API endpoint. Defaults to
'https://api.clerk.com'
.
- Name
apiVersion?
- Type
string
- Description
The version passed to the Clerk API. Defaults to
'v1'
.
- Name
audience?
- Type
string | string[]
- Description
A string or list of audiences.
Get the userId
and other properties
To access the properties of the authenticated user, such as their userId
or sessionId
, see the following examples:
The following example uses the Next.js auth()
helper to access the userId
.
The following example uses req.auth
to access the userId
.
For some backend SDKs, such as Fastify, you will need to use getAuth()
instead of req.auth
.
Feedback
Last updated on