Note: This article is only relevant for self-managed headless projects. For Wix-managed headless projects, the CLI automatically generates and manages visitor tokens for you.
Use visitor tokens to maintain anonymous visitor sessions in your self-managed headless project. The SDK uses these tokens when making requests to Wix APIs on behalf of a visitor, preserving their data such as cart items or event reservations.
Create a Wix client with the OAuth strategy. When initializing your app, check if you have stored tokens from a previous session. If tokens exist, pass them to the client to resume the session. If not, create a new client without tokens.
Note: OAuth for Wix Headless only requires a client ID. It doesn't require a client secret.
If no tokens are passed, tokens are prepared but not yet populated. They're created when you either:
If tokens are passed, the client resumes the previous visitor's session, preserving their data such as cart items or event reservations.
Make API calls as usual. The client handles token generation and renewal automatically.
After your first API call, the client has valid tokens containing:
To maintain a visitor's session across page reloads or app restarts, save the tokens to local storage, a cookie, or a file.
Retrieve the current tokens using getTokens():
The function returns an object in this format:
Save the tokens to your preferred storage:
In most cases, the client manages tokens automatically. Use these methods when you need manual control.
If you already have a client instance and need to set tokens on it later, use setTokens():
Once tokens are set, the visitor's data is preserved. For example, items added to a cart or tickets reserved are reflected in future API calls.
Call generateVisitorTokens() to create tokens before making any API calls:
Pass existing tokens to generateVisitorTokens() to confirm they're valid or renew them if expired:
The function returns:
Use renewToken() to generate a new access token without checking if the current one is valid:
Use loggedIn() to check if the current visitor is a logged-in member:
This example demonstrates creating a client that handles both new and returning visitors, making API calls, and saving tokens: