getAccessToken is a client-side function that retrieves an access token for the currently authenticated user. It fetches tokens from the /auth/access-token endpoint, which handles token refresh automatically if needed.
This is the client-side version. For server-side token retrieval, see getAccessToken (Server).
Usage
Basic Usage
Multi-API Applications
When calling multiple APIs with different audiences, specify theaudience parameter:
Request Additional Scopes
Get Full Token Response
Signature
Parameters
AccessTokenOptions
Options for fetching the access token.
Return Value
When includeFullResponse is false (default)
Returns a Promise<string> containing the access token.
When includeFullResponse is true
Returns a Promise<AccessTokenResponse> with the following properties:
string
required
The access token string.
string
The scopes granted for this token.
number
Unix timestamp (in seconds) when the token expires.
number
Number of seconds until the token expires.
string
The token type (e.g.,
"Bearer", "DPoP").Error Handling
Configuration
By default, the function fetches from/auth/access-token. You can customize this endpoint using the NEXT_PUBLIC_ACCESS_TOKEN_ROUTE environment variable:
Multi-Resource Refresh Tokens (MRRT)
When usingaudience or scope parameters, you must configure Multi-Resource Refresh Tokens in your Auth0 Application:
- Go to your Auth0 Application settings
- Navigate to Advanced Settings > Grant Types
- Enable Refresh Token
- Configure Refresh Token Policies with the audiences and scopes your application needs
Notes
- This function only works on the client side (browser)
- The user must be authenticated before calling this function
- Tokens are automatically refreshed if expired
- For server-side token retrieval, use the server-side
getAccessTokenmethod fromAuth0Client