Skip to main content
Docs

getToken()

The getToken() method retrieves the user's session token or a custom JWT template.

This method uses a cache so a network request will only be made if the token in memory has expired. The TTL for a Clerk token is one minute.

Tokens can only be generated if the user is signed in.

Session.kt
suspend fun Session.fetchToken(
  options: SessionGetTokenOptions = SessionGetTokenOptions()
): TokenResource? {
  return SessionTokenFetcher().getToken(this, options)
}
  • Name
    options
    Type
    Description

    Options that can be passed as parameters to the getToken() function.

  • Name
    template
    Type
    String?
    Description

    The name of the JWT template from the Clerk Dashboard to generate a new token from. For example: 'firebase', 'grafbase', or your custom template's name.

  • Name
    expirationBuffer
    Type
    Long
    Description

    If the cached token will expire within {expirationBuffer} seconds, fetch a new token instead. Max is 60 seconds. Defaults to 10 seconds.

  • Name
    skipCache
    Type
    Boolean
    Description

    Whether to skip the cache lookup and force a refresh of the token instead. Useful if the token claims are time-sensitive or depend on data that can be updated (e.g. user fields). Defaults to false.

val session = Clerk.session

scope.launch {
    val token = session?.fetchToken()?.onSuccess { tokenResource ->
        headers["Authorization"] = "Bearer ${tokenResource.jwt}"
    }
}

Feedback

What did you think of this content?

Last updated on