Meet Toki Operator

Docs/Developers

Wallet SDK.

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.

JSwindow.tokiWallet
Methods
116
Sections
12
Runs in
Your storefront

Your first call

  1. 01

    Turn Toki on

    The SDK comes with Toki's app embed. If you've installed Toki, it's on every page of your store already.

  2. 02

    Wait for it

    Toki's script loads after your page. Wait for tokiWalletRpcInjected before the first call.

  3. 03

    Call a method

    Every method returns a promise and acts as the signed-in customer.

Show the customer's points
const tokiWallet = await whenTokiWallet() // see "Loading the SDK"

if (await tokiWallet.isSignedIn()) {
  const { displayValue } = await tokiWallet.getBalance()
  document.querySelector("#points").textContent = displayValue
}
displayValue
"1,250 Points"

getBalance in the reference lists everything it returns.

Loading the SDK#

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.

Wait for the SDK
const whenTokiWallet = () =>
  new Promise((resolve) => {
    const ready = () => resolve(window.tokiWallet)
    if (window.tokiWallet) ready()
    else window.addEventListener("tokiWalletRpcInjected", ready)
  })

const tokiWallet = await whenTokiWallet()

Customers and guests#

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").

Signed in or not
async function render() {
  if (await tokiWallet.isSignedIn()) {
    const customer = await tokiWallet.getCustomer()
    greet(customer.name)
  } else {
    showSignIn(() => tokiWallet.open("/auth"))
  }
}

render()
window.addEventListener("tokiWalletUserAuthenticated", render)

Errors#

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.

Redeem from a button
button.addEventListener("click", async () => {
  button.disabled = true
  try {
    const { code } = await tokiWallet.redeemPoints({
      amount: 500,
    })
    await tokiWallet.applyDiscountToShopifyCart(code)
  } catch (error) {
    showError(error.message)
  } finally {
    button.disabled = false
  }
})

Events#

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.

Keep a balance fresh
async function showPoints() {
  const { displayValue } = await tokiWallet.getBalance()
  document.querySelector("#points").textContent = displayValue
}

window.addEventListener("tokiWalletBalanceChanged", showPoints)
tokiWalletRpcInjected

window.tokiWallet exists. Wait for it before your first call.

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:opened
detail: { id, name }

The customer opened an activity in the wallet.

toki:public:activity:submitted
detail: { id, name, completion }

The customer submitted an activity in the wallet or in a form block.

toki:public:cart:added
detail: { variantId, quantity }

Toki added a product to the cart.

toki:public:cart:updated
detail: { 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:added
detail: { discountCode }

Toki applied a discount code to the cart.

toki:public:paid-memberships:added
detail: { variantId, sellingPlanId }

A paid membership was added to the cart.

toki:public:cart:error
detail: { message, status }

A cart change Toki made failed, for example a product that sold out.

Reference

Methods grouped by what they're for, the way your customers see them.