Get session tokens using Shopify App Bridge utilities
app-bridge-utils library provides helper functions to handle using session tokens for your application.
This tutorial shows you how to use helper functions to fetch a session token from Shopify App Bridge and include them in requests being made to the app backend.
You must meet the following requirements to complete this tutorial:
- You've created a public app or custom app from your Partner Dashboard.
- The app is embedded in Shopify admin.
- The app uses App Bridge version 1.23 or higher.
Get a session token
getSessionToken helper retrieves a session token from Shopify. It sets up a subscription on the Shopify App Bridge client to listen for the
APP::SESSION_TOKEN_RESPOND action and then immediately dispatches the
In your app, set up the Shopify App Bridge client and import
getSessionToken using the following code:
Where your app requires a session token, specify the following code:
getSessionToken returns a
Promise, which either resolves with the session token, or rejects with an
APP::ERROR::FAILED_AUTHENTICATION error when the session token is
Authenticate your requests
authenticatedFetch helper function authenticates your requests using the session token. The function internally gets the session token from Shopify App Bridge and passes in the
Authorization header to your subsequent
app: The App Bridge instance.
fetchOperation: Optional: Lets you define your own custom fetch wrapper.
The following example shows how to use
authenticatedFetch with a custom ApolloLink:
Use your own custom fetch wrapper
If you want to add custom headers, caching, or special treatment of requests, then you can optionally pass in your own custom fetch wrapper function to the
app-bridge-utils function (
authenticatedFetch) returns your custom fetch wrapper function along with an authenticated
Authorization header appended to the request options provided.
Any custom fetch function that you provide needs to append all appropriate options, including headers. The following example shows how to append options: