Looking to access user data? Check out our Identity
tokens.
Access token format
Privy access tokens are JSON Web Tokens (JWT), signed with the ES256 algorithm. These JWTs include certain information about the user in their claims, namely:Read more about Privy’s tokens and their security in our security
guide.
Sending the access token
Accessing the token from your client
To include the current user’s access token in requests from your frontend to your backend, you’ll first need to retrieve it, then send it appropriately.You can get the current user’s Privy token as a string using the If you need to get a user’s Privy token outside of Privy’s React context, you can directly import the
getAccessToken
method from the usePrivy
hook. This method will also automatically refresh the user’s access token if it is nearing expiration or has expired.getAccessToken
method:When using direct imports, you must ensure
PrivyProvider
has rendered before invoking the method.
Whenever possible, you should retrieve getAccessToken
from the usePrivy
hook.If your app is configured to use HTTP-only cookies (instead of the default local storage), the
access token will automatically be included in the cookies for requests to the same domain. In
this case, you don’t need to manually include the token in the request headers.
Using the access token with popular libraries
When sending requests to your backend, here’s how you can include the access token with different HTTP client libraries:Getting the access token
Accessing the token from your server
When your server receives a request, the location of the user’s access token depends on whether your app uses local storage (the default) or cookies to manage user sessions.Verifying the access token
Once you’ve obtained the user’s access token from a request, you should verify the token against Privy’s verification key for your app to confirm that the token was issued by Privy and the user referenced by the DID in the token is truly authenticated. The access token is a standard ES256 JWT and the verification key is a standard Ed25519 public key. You can verify the access token against the public key using the@privy-io/server-auth
library or using a third-party library for managing tokens.
Using Privy SDK
Pass the user’s access token as astring
to the PrivyClient
’s verifyAuthToken
method:verifyAuthToken
will return an AuthTokenClaims
object with additional information about the request, with the fields below:Parameter | Type | Description |
---|---|---|
appId | string | Your Privy app ID. |
userId | string | The authenticated user’s Privy DID. Use this to identify the requesting user. |
issuer | string | This will always be 'privy.io' . |
issuedAt | string | Timestamp for when the access token was signed by Privy. |
expiration | string | Timestamp for when the access token will expire. |
sessionId | string | Unique identifier for the user’s session. |
verifyAuthToken
will throw an error and you should not consider the requesting user authorized. This generally occurs if the token has expired or is invalid (e.g. corresponds to a different app ID).The Privy Client’s
verifyAuthToken
method will make a request to Privy’s API to fetch the verification key for your app. You can avoid this API request by copying your verification key from the Configuration > App settings page of the Dashboard and passing it as a second parameter to verifyAuthToken
:Using JavaScript libraries
You can also use common JavaScript libraries to verify tokens:To start, install Then, load your Privy public key using Lastly, using If the JWT is valid, you can extract the JWT’s claims from the
jose
:jose.importSPKI
:jose.jwtVerify
, verify that the JWT is valid and was issued by Privy!payload
. For example, you can use payload.sub
to get the user’s Privy DID.If the JWT is invalid, this method will throw an error.Managing expired access tokens
A user’s access token might expire while they are actively using your app. For example, if a user does not take action on an application for an extended period of time, the access token can become expired.- Handle invalid token errors: In these scenarios, if a method returns with an
'invalid auth token'
error, we recommend calling thegetAccessToken
method with a time-based backoff until the user’s access token is refreshed with an updated expiration time. - Return errors from backend: If you receive an expired access token in your backend, return an error to your client, and as above, trigger
getAccessToken
in your client. - Handle failed refreshes: If the user’s access token cannot be refreshed, the user will be logged out.