Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Clerk · Pipe

getToken

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.

How it works

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.

Syntax

function getToken(sessionId: string, template?: string, expiresInSeconds?: number): Promise<Token>

Parameters

NameTypeDescription
sessionIdstringThe ID of the session to get the token for.
template?stringThe name of the JWT template configured in the Clerk Dashboard to generate a new token from.
expiresInSeconds?numberThe number of seconds for which the generated token should be valid.

Returns

Promise<Token> containing the generated token.

Key points

  • Use it either to obtain a session token or to create a JWT from a named Clerk template.
  • The required sessionId identifies the target session.
  • The optional template selects a Dashboard-configured JWT template.
  • The asynchronous result contains the generated token.
  • Its declared signature supports a session ID, optional template, and optional expiration duration.

Common mistakes

  • A referenced JWT template must already exist in the Clerk Dashboard, or template-based generation will not work.
  • This is a backend SDK operation tied to Clerk’s session-token endpoint, not a client-side-only helper.

Example: Generate a session token

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.

When to use it

Retrieve a session token

A server needs a token for an identified Clerk session.

Generate a custom JWT

A backend integration expects claims defined by a Clerk JWT template.

Set token expiration

A service needs to control how long a generated token remains valid.

Interview questions

QWhat does the backend 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.

QWhat are the method's main arguments used for?

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.

QHow does this SDK method map to Clerk’s API?

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 templates
  • clerkClient.sessions
  • Session
  • Token
  • BAPI endpoint

Source: getToken() - Sessions | Clerk Docs. Checked 2026-10-15.