Skip to main content

Script Setup

This library can be used with JavaScript applications in both server and client aspects.

Installation

Add the recharge-sdk to your page:

<head>
<script src="https://static.rechargecdn.com/assets/storefront/recharge-client-1.25.3.min.js"></script>
</head>

This will add a recharge global to the window that can be used throughout your page.

Initialization v1.0.0 Types

To initialize the JavaScript SDK you call initRecharge and pass the following arguments:

  • storeIdentifier - optional when JavaScript SDK is used within Shopify or Recharge hosted environments, required for headless usage. On Shopify this should be your myshopify.com domain.
  • storefrontAccessToken - optional, but required when using the API functions (see API for additional details)
  • appName - optional, name of your application to help with traceability and debugging
  • appVersion - optional, version of your application to help with traceability and debugging
  • loginRetryFn - optional, middleware function that should return a Session, called when API functions return a 401. This function allows your app to refetch a session, store it by whatever means your app uses for future calls, then return it so the call that failed can be retried.
info

Access to the storefront API functions require SDK to be initialized with a Recharge storefrontAccessToken - this token can be created via the merchant-admin where you manage your API tokens. Please make sure that the created token is of type Storefront. You can verify you have created the correct token by double checking it starts with strfnt.

recharge.init({
storeIdentifier: 'storeIdentifier', // On Shopify this should be your myshopify.com domain
// required for Storefront API access
storefrontAccessToken: 'storefrontAccessToken',
appName: 'appName',
appVersion: '1.0.0',
loginRetryFn: () => {
return recharge.auth.loginShopifyAppProxy().then(session => {
// do anything you want with the session here
// return session
return session;
});
},
});