Skip to main content
err:unkey:authentication:portal_session_not_found
Example

What Happened?

This error is returned by POST /v2/portal.exchangeCode and any portal-authenticated endpoint when the supplied credential cannot be resolved to a usable session. There are four common reasons:
  • Expired code: The exchange code carried by the portal URL is valid for 15 minutes. After that it can no longer be redeemed.
  • Already-redeemed code: Codes are single-use. Once the portal redeems one, the same code cannot be redeemed again.
  • Expired access token: After exchange, the access token is valid for 24 hours. Once it expires, requests using it return this error.
  • Revoked session: A session that has been explicitly revoked returns this error even before its natural expiry.
The exchange endpoint deliberately does not distinguish between these cases in its response. An unknown code, an expired one, and one that was already redeemed all produce the same message, so a caller cannot probe which codes exist.

How To Fix

Create a fresh session from your backend and redirect the user again:
Then redirect the user to the returned url. The portal will redeem the new code for a 24-hour access token. Re-authenticating always mints a new session rather than extending the existing one, so this is the correct response to every case above. If you passed a returnUrl when creating the session, expired sessions will automatically redirect there with ?reason=session_expired. Use that hook to re-mint a session and bounce the user back into the portal seamlessly.

Common Mistakes

  • Reusing a code: Codes are single-use. Generate a new session for every redirect.
  • Storing codes: Don’t persist exchange codes. They are short-lived credentials meant to be consumed immediately, and they are not recoverable from Unkey once issued.
  • Confusing id with the code: portal.createSession returns a non-secret id alongside the url. The id identifies the session for your own records; it is not a credential and cannot be exchanged.
  • Long-running tabs: Users who keep the portal open beyond 24 hours need a fresh session.
Last modified on August 19, 2026