Docs/Developers
Toki's JavaScript API, on every page of your store. Show customers their points, tiers and rewards, let them redeem, and run their wishlist from your theme's own code. It acts as the customer who's signed in, so there's no API key to keep.
window.tokiWalletThe SDK comes with Toki's app embed. If you've installed Toki, it's on every page of your store already.
Toki's script loads after your page. Wait for tokiWalletRpcInjected before the first call.
Every method returns a promise and acts as the signed-in customer.
const tokiWallet = await whenTokiWallet() // see "Loading the SDK"
if (await tokiWallet.isSignedIn()) {
const { displayValue } = await tokiWallet.getBalance()
document.querySelector("#points").textContent = displayValue
}"1,250 Points"getBalance in the reference lists everything it returns.
Toki's storefront script creates window.tokiWallet, then dispatches tokiWalletRpcInjected on window. The script loads after your page, so your code may run first: use the global if it's there, otherwise wait for the event.
On Shopify, the Toki Wallet app embed and every Toki app block load the script, so with the embed on, the SDK is on every page. On a headless or non-Shopify site, the HTML snippet of any Toki module loads it (see Install modules on your storefront).
tokiWallet is a property of window, so it's a global: the examples here call tokiWallet.getBalance() directly. Top-level await works in a <script type="module">; in a classic script, use .then() or an async function.
The SDK acts as the customer who's signed in to Toki in this browser, the one the wallet shows. Requests carry that customer's own session, so a method only ever reads and changes their data.
When your store signs customers in with their store account, Toki signs them in too, right after the SDK loads. Once Toki has confirmed the customer, it dispatches tokiWalletUserAuthenticated, on every page load. Check isSignedIn() when the SDK loads, and again on that event.
Methods marked Signed-in customer are for a customer who's signed in, so check isSignedIn() first. Those marked Guests too work for every visitor. To invite a guest to sign in, open the wallet's sign-in screen with open("/auth").
Every method returns a promise. When an action can't go through, for example because the customer doesn't have enough points yet, the promise rejects with an Error whose message says why. Catch it and show the customer what happened.
Call actions that spend points or change the customer's data from a click, and disable the button while the promise is pending, so each click redeems once.
Toki dispatches these events on window, whether the change came through the SDK, the wallet or one of Toki's blocks. Events with details carry them in event.detail.
tokiWalletRpcInjected |
|
tokiWalletUserAuthenticated | Toki confirmed the signed-in customer: on each page load for a customer who's signed in, and right after they sign in. |
tokiWalletBalanceChanged | The customer spent points for a discount, through the SDK or in the wallet. Read the balance again. |
tokiWalletActivityCompleted | The customer joined your program, completed a social activity or sent a picture for an activity in the wallet. |
toki:public:activity:openeddetail: { id, name } | The customer opened an activity in the wallet. |
toki:public:activity:submitteddetail: { id, name, completion } | The customer submitted an activity in the wallet or in a form block. |
toki:public:cart:addeddetail: { variantId, quantity } | Toki added a product to the cart. |
toki:public:cart:updateddetail: { cart } (the Shopify cart, when Toki read it) | Toki changed the cart: added a product, changed a quantity or applied a discount code. |
toki:public:cart:discount-code:addeddetail: { discountCode } | Toki applied a discount code to the cart. |
toki:public:paid-memberships:addeddetail: { variantId, sellingPlanId } | A paid membership was added to the cart. |
toki:public:cart:errordetail: { message, status } | A cart change Toki made failed, for example a product that sold out. |
Methods grouped by what they're for, the way your customers see them.
Open and close the wallet, and show or hide its launcher.
4 actions
The signed-in customer: profile, sign-out, birthday, marketing consent, addresses and Apple Wallet pass.
5 reads5 actions
Balances, pending points, redemption options, discount codes and the total code.
6 reads2 actions
Your activities and their rewards, referral links and redeem codes.
2 reads1 action
Your tiers and paid memberships, the customer's progress to the next tier, and tier rewards.
4 reads
Sell paid memberships, and let customers change, pause, cancel and extend their subscription.
9 reads17 actions
Recently viewed products, product recommendations and products from a collection.
3 reads1 action
Customers' wishlists: save products, organize them in named lists, share them and check them out.
6 reads8 actions
Read and change the Shopify cart, and apply discount codes to it.
2 reads4 actions
Show a customer's order and its progress, and let them buy it again, start a return or track it in Apple Wallet.
3 reads1 action
Show product reviews, ratings and review carousels, and let customers write, vote on and delete reviews.
7 reads3 actions
Shared wallets for clubs and teams: the customer's groups, members, invitations and group points.
7 reads16 actions