Skip to main content

Update Theme Engine Token

Deprecated

The Theme Engine API is deprecated. This guide keeps existing Theme Engine code working, but we recommend moving to the Recharge Storefront SDK. See Migrating from Theme Engine API to Storefront SDK.

If your Customer Portal JavaScript reads the portal token from the page URL (the ?token= query parameter) to call Theme Engine routes, switch it to window.ReCharge.getPortalToken().

The portal token is being removed from Customer Portal URLs. Once that happens on your store, code that reads token from the URL will get nothing back and its Theme Engine requests will fail. getPortalToken() keeps working whether or not the token is in the URL, and it's available now, so you can update your code before the change reaches your store.

What getPortalToken() returns​

window.ReCharge.getPortalToken() is defined on Recharge Customer Portal pages. It returns:

  1. The token the page was loaded with, either from the server or from the URL ?token= parameter.
  2. Otherwise, the token saved for the customer in the browser's session storage. This is what keeps a reload working after the token has been removed from the URL.
  3. Otherwise, null.

Call it when you need the token instead of reading it once and caching it. It's a synchronous function call with no network request.

Before​

Code that reads the token from the URL:

const params = new URLSearchParams(window.location.search);
const token = params.get('token');

async function getSubscriptions() {
const response = await fetch(
`/tools/recurring/portal/{{ customer.hash }}/subscriptions?token=${token}`
);
return response.json();
}

After​

Code that gets the token from the page:

function getToken() {
const token = window.ReCharge.getPortalToken();
if (!token) {
throw new Error('No Customer Portal token is available on this page');
}
return token;
}

async function getSubscriptions() {
const response = await fetch(
`/tools/recurring/portal/{{ customer.hash }}/subscriptions?token=${getToken()}`
);
return response.json();
}

Only the source of the token changes. Your Theme Engine routes, request bodies and responses stay the same.

Checking your code​

Search your Customer Portal theme for places that read the token from the URL, for example:

  • new URLSearchParams(window.location.search).get('token')
  • url.searchParams.get('token')
  • Regular expressions or string splitting on window.location.href or window.location.search that look for token=

Replace each with window.ReCharge.getPortalToken(). After updating, load a Customer Portal page, run window.ReCharge.getPortalToken() in the browser console to confirm it returns a token, and check that your Theme Engine requests still succeed.