Node.js Available Methods
All resource operations are mounted as sub-APIs on the clerkClient
object. You can find the full list of available operations in the Clerk Backend SDK documentation. To access a resource, you must first instantiate a clerkClient
instance.
Instantiate a default clerkClient
instance
If you would like to use the default instance of clerkClient
provided by the SDK, you can provide the CLERK_SECRET_KEY
as an environment variable and instantiate clerkClient
without passing configuration options.
import { clerkClient } from '@clerk/clerk-sdk-node'; const userList = await clerkClient.users.getUserList();
If you are interested in a certain resource, you can destructure clerkClient
to access just that resource.:
import { sessions } from '@clerk/clerk-sdk-node'; const sessionList = await sessions.getSessionList();
const pkg = require('@clerk/clerk-sdk-node'); const clerkClient = pkg.default; clerkClient.sessions .getSessionList() .then((sessions) => console.log(sessions)) .catch((error) => console.error(error));
Or if you prefer a resource sub-api directly:
const pkg = require('@clerk/clerk-sdk-node'); const { clients } = pkg; clients .getClient(clientId) .then((client) => console.log(client)) .catch((error) => console.error(error));
Instantiate a custom clerkClient
instance
If you would like to customize the behavior of the SDK, you can instantiate a clerkClient
instance yourself by calling createClerkClient
and passing in options.
The example below shows how to use createClerkClient
to create a clerkClient
instance and pass a Clerk secret key instead of setting a CLERK_SECRET_KEY
environment variable.
import { createClerkClient } from '@clerk/clerk-sdk-node'; const clerkClient = createClerkClient({ secretKey: '{{secret}}' }); const clientList = await clerkClient.clients.getClientList();
const Clerk = require('@clerk/clerk-sdk-node/cjs/instance').default; const clerkClient = Clerk({ secretKey: '{{secret}}' }); clerkClient.sessions .getSessionList() .then((sessions) => console.log(sessions)) .catch((error) => console.error(error));
Customizing resources
The following options are available for you to customize the behaviour of the clerkClient
object.
Most options can also be set as environment variables so that you don't need to pass anything to the constructor.
Option | Description | Default | Environment variable |
---|---|---|---|
secretKey | Server key for api.clerk.com . Can be found in your Clerk Dashboard on the API Keys(opens in a new tab) page. | none | CLERK_SECRET_KEY |
apiVersion | API version to use | v4 | CLERK_API_VERSION |
apiUrl | For debugging | https://api.clerk.com | CLERK_API_URL |
httpOptions | HTTP client options | {} | N/A |
For every option, the resolution is as follows, in order of descending precedence:
- option passed
- environment variable (if applicable)
- default
This means that if you pass an option to the constructor, it will always take precedence over an environment variable.
Another available environment variable is CLERK_LOGGING
. You can set its value to true
to enable additional logging that may be of use when debugging an issue.