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.
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
Last updated on