Skip to main content
from stytch import B2BClient

client = B2BClient(
  project_id="project-test-8aed2e54-0266-4793-9b5e-0cc9c56064da",
  secret="secret-test-IJ7zLTgXp8xoS7yXO2xavNxZTbYfvm-2nZM=",
)

resp = client.sessions.authenticate_jwt(
  session_jwt="eyJ...",
)

print(resp)
{
  "member_session": {
    "member_session_id": "<string>",
    "member_id": "<string>",
    "started_at": "<string>",
    "last_accessed_at": "<string>",
    "expires_at": "<string>",
    "authentication_factors": [
      {
        "type": "magic_link",
        "delivery_method": "email",
        "last_authenticated_at": "<string>",
        "created_at": "<string>",
        "updated_at": "<string>",
        "email_factor": {
          "email_id": "<string>",
          "email_address": "<string>"
        },
      }
    ],
    "organization_id": "<string>",
    "roles": [
      "<string>"
    ],
    "organization_slug": "<string>",
    "custom_claims": "<string>"
  },
  "session_jwt": "<string>",
  "verdict": {
    "authorized": true,
    "granting_roles": [
      "<string>"
    ]
  }
}
from stytch import B2BClient

client = B2BClient(
  project_id="project-test-8aed2e54-0266-4793-9b5e-0cc9c56064da",
  secret="secret-test-IJ7zLTgXp8xoS7yXO2xavNxZTbYfvm-2nZM=",
)

resp = client.sessions.authenticate_jwt(
  session_jwt="eyJ...",
)

print(resp)
{
  "member_session": {
    "member_session_id": "<string>",
    "member_id": "<string>",
    "started_at": "<string>",
    "last_accessed_at": "<string>",
    "expires_at": "<string>",
    "authentication_factors": [
      {
        "type": "magic_link",
        "delivery_method": "email",
        "last_authenticated_at": "<string>",
        "created_at": "<string>",
        "updated_at": "<string>",
        "email_factor": {
          "email_id": "<string>",
          "email_address": "<string>"
        },
      }
    ],
    "organization_id": "<string>",
    "roles": [
      "<string>"
    ],
    "organization_slug": "<string>",
    "custom_claims": "<string>"
  },
  "session_jwt": "<string>",
  "verdict": {
    "authorized": true,
    "granting_roles": [
      "<string>"
    ]
  }
}
Given a Session , this method authenticates a Session and updates its lifetime by the specified session_duration_minutes. If session_duration_minutes is not specified, the Session will not be extended.
This method is only available when using our backend SDKs.If you are looking for client side JWT authentication, please use the frontend SDKs’ Authenticate Session endpoint instead.
If you provide a JWT that needs to be refreshed and is expired according to its exp claim, a new JWT will be returned if both the signature and the underlying Session are still valid. See our JWT guides for more information. If the JWT is older than max_token_age_seconds or if the JWT is expired, this method will communicate with the Stytch API to authenticate the session. Otherwise, the JWT will be validated locally.

Local JWT validation

If you do not provide a max_token_age_seconds parameter, then the authenticateJwt method will only communicate with the Stytch API if the JWT is expired (Stytch JWTs have an exp of five minutes). Specifying a max_token_age_seconds parameter of less than five minutes is one way to reduce security risks inherent to local JWT validation by forcing communication with the Stytch API more frequently. We recommend relying primarily on this method over the authenticateSession method, as it handles the local JWT validation vs. remote session authentication logic for you, improving latency when the JWT is less than max_token_age_seconds old and authenticating the underlying session with Stytch when necessary.

Authorization

If an authorization_check is passed in, this method will also check if the is authorized to perform the given action on the given Resource in the specified . A Member is authorized if:
  • their Member Session contains a Role, assigned explicitly or implicitly, with adequate permissions.
  • the organization_id passed in the authorization check matches Member Session’s Organization.
If either of these conditions are not met, a 403 error will be thrown. Otherwise, the response will contain a list of Roles that satisfied the authorization check.

Request Parameters

session_jwt
string
required
The Session JWT to authenticate.
authorization_check
object
If included, this method will also check if the Member is authorized to perform the given action on the given Resource in the specified Organization. A Member is authorized if their Member Session contains a Role, assigned explicitly or implicitly, with adequate permissions. In addition, the organization_id passed in the authorization check must match the Member’s Organization.
max_token_age_seconds
number
If set, remote verification will be forced if the JWT was issued more than that many seconds ago (based on the iat claim).

Response

member_session
string
required
The Session object associated with the authenticated JWT.
session_jwt
string
required
A new JWT for the authenticated Session.
session_token
string
An opaque Session token for the authenticated Session.Will only be returned when remote JWT authentication occurs.
member
object
The Member object associated with the authenticated Session.Will only be returned when remote JWT authentication occurs.
organization
object
The Organization object associated with the authenticated Session.Will only be returned when remote JWT authentication occurs.
verdict
object
If an authorization_check is provided in the request and the check succeeds, this field will return information about why the Member was granted permission.
status_code
number
The HTTP status code of the response. Stytch follows standard HTTP response status code patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX are server errors.
request_id
string
Globally unique UUID that is returned with every API call. This value is important to log for debugging purposes; we may ask for this value to help identify a specific API call when helping you debug an issue.