Skip to main content

Session Token

The API for interacting with session tokens.

The base API object provided to purchase extension targets.

required

The session token providing a set of claims as a signed JSON Web Token (JWT).

The token has a TTL of 5 minutes.

If the previous token expires, this value will reflect a new session token with a new signature and expiry.

Refer to session token examples for more information.

Anchor to useSessionToken
useSessionToken()

Returns a the session token API object.

SessionToken

get
() => Promise<string>

Requests a session token that hasn't expired. You should call this method every time you need to make a request to your backend in order to get a valid token. This method will return cached tokens when possible, so you don’t need to worry about storing these tokens yourself.

Examples

Preact

import '@shopify/ui-extensions/preact';
import {render} from 'preact';
import {useEffect} from 'preact/hooks';

export default function extension() {
render(<Extension />, document.body);
}

function Extension() {
const {sessionToken} = shopify;

useEffect(() => {
async function queryApi() {
// Request a new (or cached) session token from Shopify
const token =
await shopify.sessionToken.get();
console.log('sessionToken.get()', token);

const apiResponse =
await fetchWithToken(token);
// Use your response
console.log('API response', apiResponse);
}

function fetchWithToken(token) {
const result = fetch(
'https://myapp.com/api/session-token',
{
headers: {
Authorization: `Bearer ${token}`,
},
},
);
return result;
}

queryApi();
}, [sessionToken]);

return (
<s-banner>
See console for API response
</s-banner>
);
}
Was this page helpful?