Personal access token
Generate short-lived tokens from your backend so the real API key never ships inside your Unity build, eliminating credential exposure from client applications.
By default, the Convai Unity SDK reads your API key from ConvaiSettings.asset, which is compiled into your build. Anyone who extracts the build can retrieve the key and use it against your Convai account. Personal Access Tokens (PATs) eliminate that exposure entirely. Your real API key lives on your backend — a server-side application you control (Node.js, Python, .NET, etc.) that your users never interact with directly. The backend generates a short-lived token — valid for one hour — and hands it to the Unity app. The app connects to Convai using that token. If the token is intercepted, it expires within the hour and cannot be used to access your account settings, characters, or billing. PATs require a server that holds the real API key and calls Convai's token endpoint on behalf of your users. A lightweight function (AWS Lambda, Azure Function, Cloudflare Worker, etc.) is sufficient — it only needs to make one HTTP request per session.
Your Backend ──holds──► Real API Key
│
│ POST /user/connect (server-side, never from client)
▼
Convai API ──returns──► apiAuthToken (1 hour)
│
│ delivered to Unity app at runtime
▼
Unity App ──uses──► apiAuthToken as credential
(real API key never in build)Token endpoints
All three endpoints target https://api.convai.com and require your real API key in the CONVAI-API-KEY header. These calls are made from your backend, not from the Unity app.
Generate a token
POST https://api.convai.com/user/connectContent-Type
application/json
CONVAI-API-KEY
Your Convai API key
Request body: {}
Response:
apiAuthToken
The short-lived token to deliver to the Unity app.
expirationTime
UTC timestamp of expiry — approximately 1 hour from generation.
You can generate a new token while the current one is still active. Generating a new token does not invalidate the previous one.
Extend a token
Headers: same as Generate.
Resets the expiry clock on an existing token without invalidating it.
Revoke a token
Headers: same as Generate.
Immediately invalidates the token. Call this on logout or whenever the token is no longer needed.
Integration with the Unity SDK
Fetch the apiAuthToken from your backend, then pass it to ConvaiManager.ActiveManager.ConnectWithAuthTokenAsync() — the SDK sends it as the CONVAI-API-KEY credential header for that connection attempt, the same slot the API key would occupy. This requires Auth Token mode selected in Project Settings; see Configure Auth Token mode and the full parameter reference in Connect with an existing auth token.
Do not set an API key in ConvaiSettings.asset for production builds. Leave the field empty and supply the PAT at connect time instead.
No ConvaiManager subclass is required — ConnectWithAuthTokenAsync fetches nothing itself, so the token fetch and the connection call stay in a plain component.
Never call https://api.convai.com/user/connect directly from the Unity app. Doing so requires the real API key to be in the build — which is what PATs exist to prevent. Always call it from your backend.
Do not persist apiAuthToken to disk (e.g., PlayerPrefs). A cached token is a stored credential. Always fetch a fresh token from your backend at each app startup.
Token expiry and session length
Once a Convai session starts, the token is no longer checked for the duration of that session — a token that expires mid-session does not disconnect the user. The PAT is only consumed at connect time.
Token expires before ConnectWithAuthTokenAsync() is called
Connection fails — fetch a fresh token from your backend and retry.
Token expires during an active session
Session is unaffected — the token is only checked at connect time, not held for the session duration.
App restarts after token expiry
Always fetch a fresh token at startup — do not cache tokens across launches.
Usage examples
Example 1: LMS platform with per-session tokens
A corporate safety training platform issues a Convai PAT as part of the LMS login response. The token is generated server-side when the learner authenticates and is delivered alongside the LMS session data.
Example 2: Shared kiosk with per-resident token rotation
Each resident logs into a shared training kiosk, receives a fresh PAT from the backend, interacts with the simulation, then logs out. On logout, the token is explicitly revoked so it cannot be reused.
Example 3: On-demand token refresh for long-running applications
Industrial training simulations can run for multiple hours. While an active session is unaffected by token expiry, a new session started after expiry needs a fresh token. Use your backend's extend or re-generate endpoint before reconnecting.
Troubleshooting
Connect fails immediately
apiAuthToken is null — backend fetch failed
Check Console for the Token fetch failed log; verify your backend endpoint URL and auth headers.
401 auth error from Convai on connect
Token was already expired or revoked before ConnectWithAuthTokenAsync()
Always fetch a fresh token immediately before connecting — never reuse a cached token across sessions.
Connection fails with ConfigAuthTokenModeRequired
The project's ConvaiSettings asset still has AuthMode set to ApiKey
Set AuthMode to AuthToken in Edit > Project Settings > Convai SDK > Credentials, even if no endpoint URL is configured there.
apiAuthToken is null in the backend response
Malformed request body or missing CONVAI-API-KEY header on the backend call
Ensure the body is {} and the header is present. Log the raw response on the backend to inspect the error.
Token works in development but fails in production build
ConvaiSettings.asset API key field is empty and no PAT is fetched
Confirm the token-fetch call completes and its result is passed to ConnectWithAuthTokenAsync() before the app tries to connect.
Next steps
Custom identity providerCustom credential providerLast updated
Was this helpful?