Clerk Express SDK
The Clerk Express SDK is the recommended method for integrating Clerk into your Express application.
See the quickstart to get started.
Methods
clerkMiddleware()
The clerkMiddleware()
function checks the request's cookies and headers for a session JWT and, if found, attaches the Auth
object to the request object under the auth
key.
Options
The clerkMiddleware()
function accepts these options plus the following:
- Name
clerkClient
- Type
ClerkClient
- Description
An instance of the ClerkClient class. This is used to interact with the Clerk API.
- Name
debug
- Type
boolean
- Description
A flag to enable debug mode. When enabled, the middleware will log debug information to the console.
- Name
enableHandshake
- Type
boolean
- Description
Enables Clerk's handshake flow, which helps verify the session state when a session JWT has expired. It issues a 307 redirect to refresh the session JWT if the user is still logged in. Defaults to
true
.
requireAuth()
The requireAuth()
middleware functions similarly to clerkMiddleware()
, but also protects your routes by redirecting unauthenticated users to the sign-in page. It will look for a CLERK_SIGN_IN_URL
environment variable and use that by default. If not present, you must pass a sign in URL to the middleware as an option.
This middleware accepts the same options as clerkMiddleware()
.
getAuth()
The getAuth()
helper retrieves authentication state from the request object. See the Next.js reference documentation for more information on how to use the returned auth
object.
The following example uses requireAuth()
to protect the route based on authentication status, and then uses getAuth()
to protect the route based on authorization status.
clerkClient
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 the https://api.clerk.com/v1/users
endpoint, you can use the users.getUserList()
method provided by the JavaScript Backend SDK.
All resource operations are mounted as sub-APIs on the clerkClient
object. See the reference documentation for more information.
Feedback
Last updated on