/
Contact usSee pricingStart building
    Overview
    Installation
    Changelog

    Pre-built UI

    Component Playground
    StytchLogin
      UI Configuration
      UI Callbacks
    StytchPasswordReset
    StytchPasskeyRegistration
    IdentityProviderBeta
      UI Configuration
      UI Callbacks

    Headless

    Users
      Get user
      Update user
      Delete authentication factors
    Email Magic Links
      Send
      Login or create
      Authenticate
    OAuth
      Start
      Google One Tap
      Authenticate
    Passwords
      Create
      Authenticate
      Reset by Email Start
      Reset by Email
      Reset by Existing Password
      Reset by Session
      Strength Check
    One-Time Passcodes (OTP)
      Login or create via SMS
      Send via SMS
      Login or create via Email
      Send via Email
      Login or create via WhatsApp
      Send via WhatsApp
      Authenticate
    Time-Based One-Time Passcodes (TOTP)
      Create
      Authenticate
      Get Recovery Codes
      Recover
    Session Management
      Get Session
      Authenticate Session
      Revoke Session
      Update Session
      Get Tokens
    Passkeys & WebAuthn
      Register
      Authenticate
      Update
      Browser supports autofill
    Crypto Wallets
      Authenticate
      Authenticate Start
    Impersonation
      Authenticate

    More Resources

    Cookies & session management
    SWR & caching
    TypeScript
    User privacy measures
    Multi-factor authentication
    Next.js
    CAPTCHA
Get support on SlackVisit our developer forum

Contact us

Consumer Authentication

/

Frontend SDKs

/

Headless

/

Session Management

/

Get Tokens

Get tokens

Returns the session_token and session_jwt values associated with the logged-in user's active session.

Session tokens are only available if:

  • There is an active session, and
  • The session is not managed via HttpOnly cookies.

If either of these conditions is not met, getTokens will return null.

Note that the Stytch SDK stores the session_token and session_jwt values as session cookies in the user's browser. Those cookies will be automatically included in any request that your frontend makes to a service (such as your backend) that shares the domain set on the cookies, so in most cases, you will not need to explicitly retrieve the session_token and session_jwt values using the getTokens() method. However, we offer this method to serve some unique use cases where explicitly retrieving the tokens is necessary.

import { useState } from 'react';
import { useStytch } from '@stytch/react';
import { SessionTokens } from '@stytch/vanilla-js';

export const GetSessionTokens = () => {
  const stytch = useStytch();
  const [sessionTokens, setSessionTokens] = useState<SessionTokens | null>(null);

  // Callback to retrieve session tokens on demand
  const handleGetTokens = () => {
    const tokens = stytch.session.getTokens();
    setSessionTokens(tokens);
  };

  return (
    <div>
      <button onClick={handleGetTokens}>Get Session Tokens</button>
      {sessionTokens && (
        <div>
          <p>Session Token: {sessionTokens.session_token}</p>
          <p>Session JWT: {sessionTokens.session_jwt}</p>
        </div>
      )}
    </div>
  );
};