The @auth0/auth0-api-js library allows for securing API's running on a JavaScript runtime.
Using this SDK as-is in your API may not be trivial, as it is not a plug-and-play library for your framework. Instead, it is designed to be used as a building block for building framework-specific SDKs.
The AUTH0_DOMAIN can be obtained from the Auth0 Dashboard once you've created an application.
The AUTH0_AUDIENCE is the identifier of the API. You can find this in the API section of the Auth0 dashboard.
3. Verify the Access Token
The SDK's verifyAccessToken method can be used to verify the access token.
The SDK supports RFC 8693 OAuth 2.0 Token Exchange, allowing you to exchange tokens for different API audiences while preserving user identity.
When to Use Which Flow
Custom Token Exchange: Use when you control the subject token format. Common scenarios:
Exchanging MCP server tokens for Auth0 tokens
Migrating from legacy authentication systems
Federating with partner systems using custom token formats
Exchanging tokens issued by your own services
Access Token Exchange with Token Vault (via getAccessTokenForConnection): Use when exchanging for external provider's access tokens:
Accessing Google APIs with a user's Google token
Calling Facebook Graph API with a user's Facebook token
Any scenario where Auth0 manages the external provider's refresh tokens in the Token Vault
Custom Token Exchange Example
import { ApiClient } from'@auth0/auth0-api-js';
const apiClient = newApiClient({
domain: '<AUTH0_DOMAIN>',
audience: '<AUTH0_AUDIENCE>',
clientId: '<AUTH0_CLIENT_ID>',
clientSecret: '<AUTH0_CLIENT_SECRET>',
});
// Exchange a custom token (e.g., from an MCP server or legacy system)const result = await apiClient.getTokenByExchangeProfile(
userToken, // The token to exchange
{
subjectTokenType: 'urn:example:custom-token', // Your custom token type URNaudience: 'https://api.backend.com',
}
);
// Handle token expiry - check expiresAt and re-exchange when needed// Note: expiresAt is in seconds, Date.now() is in millisecondsconst tokenIsValid = Math.floor(Date.now() / 1000) < result.expiresAt;
if (!tokenIsValid) {
// Re-exchange with a fresh subject token (e.g., from your auth provider)const newSubjectToken = awaitgetNewTokenFromYourProvider();
const refreshed = await apiClient.getTokenByExchangeProfile(newSubjectToken, {
subjectTokenType: 'urn:example:custom-token',
audience: 'https://api.backend.com',
});
}
Security Note: The extra parameter (if exposed in your application) should never contain Personally Identifiable Information (PII) or sensitive data. Extra parameters may be logged by Auth0 or included in audit trails. Only use it for non-sensitive technical parameters that don't identify users.
Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.
What is Auth0?
Auth0 is an easy to implement, adaptable authentication and authorization platform. To learn more checkout Why Auth0?
This project is licensed under the MIT license. See the LICENSE file for more info.
Auth0 Authentication SDK for API's on JavaScript runtimes
The npm package @auth0/auth0-api-js receives a total of 15,381 weekly downloads. As such, @auth0/auth0-api-js popularity was classified as popular.
We found that @auth0/auth0-api-js demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.It has 18 open source maintainers collaborating on the project.
Package last updated on 15 Dec 2025
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
OSV withdrew 157 OSV malware reports after automated false positives incorrectly flagged trusted npm and PyPI packages, sending bad records into tools that rely on OSV data.