Session tokens overview
A session token is a mechanism that lets your embedded app authenticate the requests that it makes between the client side and your app's backend.
The following video provides a short introduction to session tokens:
How session tokens workAnchor link to section titled "How session tokens work"
This section describes the authentication and request flows associated with session tokens, and the lifetime of a session token. It also provides information about implementing both OAuth and session token authentication for embedded apps.
Authentication flow using a session tokenAnchor link to section titled "Authentication flow using a session token"
When your embedded app first loads, it's unauthenticated and serves up the frontend code for your app. Your app renders a user interface skeleton or loading screen to the user.
After the frontend code has loaded, your app calls a Shopify App Bridge action to get the session token. Your app includes the session token in an authorization header when it makes any HTTPS requests to its backend.
Request flow using a session tokenAnchor link to section titled "Request flow using a session token"
The session token is signed using the shared secret between your app and Shopify so that your backend can verify if the request is valid.
Lifetime of a session tokenAnchor link to section titled "Lifetime of a session token"
The lifetime of a session token is one minute. Session tokens must be fetched using Shopify App Bridge on each request to make sure that stale tokens aren't used.
OAuth and session tokensAnchor link to section titled "OAuth and session tokens"
Session tokens aren't a replacement for implementing OAuth with Shopify.
Unlike API access tokens, session tokens can't be used to make authenticated requests to Shopify APIs. An API access token is what you use to send requests from your app's backend to Shopify so that you can fetch specific data from the merchant's shop.
For example, to make authenticated requests to the Admin API, your app must store the access token it receives during the OAuth flow. To contrast, session tokens are used by your app's backend to verify the embedded request coming from your app's frontend.
The following diagram shows the authentication process using session tokens and API access tokens:
Anatomy of a session tokenAnchor link to section titled "Anatomy of a session token"
Session tokens use the JSON Web Token (JWT) format and contain information about the merchant that's currently using your embedded app.
A session token consists of a header, payload, and signature. For an interactive example, refer to JWT.io, where you can experiment with setting different values for each section. Shopify recommends that you use your test app's credentials when testing on JWT.io.
For the most part, you shouldn't have to manage the anatomical details of session tokens. In most scenarios, you'll use a library, such as
authenticated_fetch from app-bridge-utils, which generates and includes the session token in your requests. On the backend, you can use middleware similar to Shopify App Node's
After a Shopify session token is decoded, it has the following fields:
The values in the header are constant and never change.
alg: The algorithm used to encode the JWT.
typ: The (type) header parameter used by session token to declare the media type.
iss: The shop's admin domain.
dest: The shop's domain.
aud: The API key of the receiving app.
sub: The user that the session token is intended for.
exp: When the session token expires.
nbf: When the session token activates.
iat: When the session token was issued.
jti: A secure random UUID.
sid: A unique session ID per user and app.
Example payloadAnchor link to section titled "Example payload"
Limitations and considerationsAnchor link to section titled "Limitations and considerations"
Session token authentication is fully supported for single-page apps. For more information, refer to Getting started with session token authentication.
If you have a multi-page app, then you might be able to convert it to behave as if it were a single-page app using Turbolinks. You can only use session tokens for a multi-page app if you convert it to behave like a single-page app. For more information, refer to Authenticate a server-side rendered embedded app using Rails and Turbolinks.