Create a Clerk application
You need to create a Clerk application in your Clerk Dashboard before you can set up Clerk Node.js. For more information, check out our Set up your application guide.
Install @clerk/clerk-sdk-node
Once a Clerk application has been created, you can install and then start using Clerk Node.js in your application. An ESM module for the Clerk Node SDK is available under the @clerk/clerk-sdk-node
npm package.
Set environment variables
Below is an example of an .env.local
file.
Pro tip! If you are signed into your Clerk Dashboard, your secret key should become visible by clicking on the eye icon. Otherwise, you can find your keys in the Clerk Dashboard on the API Keys page.
Available methods
All resource operations are mounted as sub-APIs on the clerkClient
object. To access the resource operations, you must first instantiate a clerkClient
instance.
Multi-session applications
If Clerk is running in multi-session mode, it's important to ensure your frontend sends the Session ID that is making the request.
Our middleware will look for a query string parameter named _clerk_session_id
. If this parameter is not found, the middleware will instead choose the last active session, which may be subject to race conditions and should not be relied on for authenticating actions.
Connect/Express middlewares
The Clerk Node SDK offers two middlewares to authenticate your backend endpoints.
Manual authentication
Authenticate a particular session
Authenticate the last active session
Using the last active session is appropriate when determining the user after a navigation.
Error handling
Node SDK functions do not throw errors when something goes wrong. Instead, they return a ClerkBackendApiResponse
object, just as they would if there were no errors. You can check for errors on the .errors
property of the response. For example:
This approach to error handling allows Clerk to produce strongly-typed errors for those using TypeScript, and can reduce the need for try/catch
blocks in your code.
See the guide on using Clerk with Node.js and Express to learn about Express error handling.
Feedback
Last updated on