Docs

getToken()

Retrieves the user's session token for the default Clerk token or the given 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
func getToken(
  _ options: GetTokenOptions = .init()
) async throws -> TokenResource?
  • Name
    options
    Type
    GetTokenOptions
    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
    Double
    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
    Bool
    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.

let session = Clerk.shared.session

if let token = try await session?.getToken()?.jwt { // => "eyJhbGciOiJSUzI1NiIsImtpZC..."
  headers["Authorization"] = "Bearer \(token)"
}

Feedback

What did you think of this content?

Last updated on