Retrieve a session token
A server needs a token for an identified Clerk session.
Clerk · Pipe
Retrieves a token for a Clerk session or creates a JWT from a specified Dashboard template. The backend method accepts a session ID and returns the generated token asynchronously.
The backend Sessions API method accepts a session ID and can optionally use a Dashboard-configured JWT template. It returns a promise containing the generated token; the SDK delegates this operation to Clerk’s session-token endpoint.
function getToken(sessionId: string, template?: string, expiresInSeconds?: number): Promise<Token>| Name | Type | Description |
|---|---|---|
sessionId | string | The ID of the session to get the token for. |
template? | string | The name of the JWT template configured in the Clerk Dashboard to generate a new token from. |
expiresInSeconds? | number | The number of seconds for which the generated token should be valid. |
Promise<Token> containing the generated token.
sessionId identifies the target session.template selects a Dashboard-configured JWT template.const sessionId = 'sess_123'
const template = 'test'
const response = await clerkClient.sessions.getToken(sessionId, template)
console.log(response)Generates a token for the specified session using the JWT template named test. Create that template in the Clerk Dashboard before running this code.
A server needs a token for an identified Clerk session.
A backend integration expects claims defined by a Clerk JWT template.
A service needs to control how long a generated token remains valid.
getToken method do?getToken takes a required session ID, plus optional template and expiration arguments, and returns a Promise<Token>. It either retrieves a session token or creates a JWT from the selected Clerk Dashboard template.
Follow-up: What must be configured before using a template? A JWT template with the requested name must exist in the Clerk Dashboard.
The first argument identifies the session whose token is requested. The optional template argument selects the Dashboard JWT template used to generate a custom token.
Follow-up: What happens if the session ID is invalid? The method cannot generate the requested session token because the operation targets that specific session.
The SDK method wraps Clerk’s BAPI session-token route, using the session ID and, when supplied, the template name. The asynchronous result is the generated token.
Follow-up: How would you obtain a token for a different session? Pass that session’s ID as the first argument, subject to the backend authorization and session validity required by Clerk.
JWT templatesclerkClient.sessionsSessionTokenBAPI endpoint