Meet Toki Operator

Docs/Wallet SDK

Cart

Read and change the Shopify cart, and apply discount codes to it.

These methods work with the customer's cart through Shopify's Ajax Cart API, so they're for Shopify stores and don't need a signed-in customer. Amounts from Shopify are in cents, such as 4900 for $49.00.

read

getShopifyCart#

tokiWallet.getShopifyCart()→ Promise<ShopifyCart>

Returns the customer's current Shopify cart.

Resolves the cart as Shopify returns it. The most useful fields are listed here; see Shopify's Cart API reference for the rest.

Returns

Promise<ShopifyCart>
  • tokenstring

    Cart token.

  • item_countnumber

    Total quantity across all lines.

  • items_subtotal_pricenumber

    Total of the lines after line discounts, in cents.

  • total_pricenumber

    Total after all discounts, in cents.

  • original_total_pricenumber

    Total before discounts, in cents.

  • total_discountnumber

    Sum of all discounts, in cents.

  • currencystring

    Cart currency, such as "USD".

  • itemsCartLine[]
    14 fields per item
    • keystring

      Unique ID of the line. Pass it to change or remove the line.

    • variant_idnumber
    • product_idnumber
    • quantitynumber
    • titlestring

      Product and variant title, such as "Linen Shirt - Small".

    • product_titlestring
    • variant_titlestring | null

      null for a product with a single variant.

    • pricenumber

      Unit price in cents.

    • final_pricenumber

      Unit price after line discounts, in cents.

    • final_line_pricenumber

      Line total after line discounts, in cents.

    • urlstring

      Path of the product page with the variant selected.

    • handlestring
    • imagestring | null

      Image URL.

    • propertiesobject | null

      Line item properties, such as { Engraving: "Anna" }.

  • cart_level_discount_applicationsobject[]

    Discounts applied to the whole cart, such as a discount code.

Example
const { item_count, items } =
  await tokiWallet.getShopifyCart()
Result
{
  "token": "Z2NwLWV1cm9wZS13ZXN0MTowMUpBQ0E5?key=4c2b8f0e9d1a",
  "item_count": 2,
  "items_subtotal_price": 9998,
  "total_price": 9998,
  "original_total_price": 9998,
  "total_discount": 0,
  "currency": "USD",
  "items": [
    {
      "key": "43872109510766:5f1d3c0b9a7e2f4d",
      "variant_id": 43872109510766,
      "product_id": 7982301462638,
      "quantity": 2,
      "title": "Linen Shirt - Small",
      "product_title": "Linen Shirt",
      "variant_title": "Small",
      "price": 4999,
      "final_price": 4999,
      "final_line_price": 9998,
      "url": "/products/linen-shirt?variant=43872109510766",
      "handle": "linen-shirt",
      "image": "https://cdn.shopify.com/s/files/1/0601/2345/files/linen-shirt.jpg?v=1712345678",
      "properties": {}
    }
  ],
  "cart_level_discount_applications": []
}
read

getCartSubtotal#

tokiWallet.getCartSubtotal()→ Promise<CartSubtotal>

Returns the cart subtotal and the quantity of each variant in the cart.

subtotalPrice is in currency units, such as 98.5 for $98.50: the total of the cart's lines after line discounts, before cart-wide discounts and shipping. Use it for spending goals, such as a free shipping bar. Inside a Tapcart mobile app, it reads the Tapcart cart.

Returns

Promise<CartSubtotal>
  • subtotalPricenumber

    Subtotal in currency units.

  • currencystringOptional

    Cart currency, such as "USD".

  • itemsCartSubtotalItem[]

    One entry per cart line.

    2 fields per item
    • variant_idstring

      Variant ID.

    • quantitynumber
Example
const { subtotalPrice } =
  await tokiWallet.getCartSubtotal()
const leftForFreeShipping = Math.max(
  0,
  100 - subtotalPrice,
)
Result
{
  "subtotalPrice": 99.98,
  "items": [
    {
      "variant_id": "43872109510766",
      "quantity": 2
    }
  ],
  "currency": "USD"
}
action

addToShopifyCart#

tokiWallet.addToShopifyCart({ variantId, quantity, properties? })→ Promise<ShopifyCartAddResult>

Adds a variant to the Shopify cart.

Afterwards the SDK updates cart count elements in your theme, such as [data-cart-count] and .cart-count-bubble, and dispatches the cart events common themes listen for: cart:updated, cart:refresh and cart:change on document, and cart:updated and theme:cart:change on window. toki:public:cart:added carries { variantId, quantity }, and toki:public:cart:updated carries { cart }, the whole cart, so you can refresh your own cart UI.

If Shopify refuses the item, for example because it's sold out, the SDK dispatches toki:public:cart:error with { message, status } and rejects with the same message.

Options

one object
  • variantIdstring | numberRequired

    Variant ID.

  • quantitynumberRequired
  • propertiesRecord<string, string>

    Line item properties for the new line, such as { Engraving: "Anna" }.

Returns

Promise<ShopifyCartAddResult>
  • itemsCartLine[]

    The line Shopify added.

    14 fields per item
    • keystring

      Unique ID of the line. Pass it to change or remove the line.

    • variant_idnumber
    • product_idnumber
    • quantitynumber
    • titlestring

      Product and variant title, such as "Linen Shirt - Small".

    • product_titlestring
    • variant_titlestring | null

      null for a product with a single variant.

    • pricenumber

      Unit price in cents.

    • final_pricenumber

      Unit price after line discounts, in cents.

    • final_line_pricenumber

      Line total after line discounts, in cents.

    • urlstring

      Path of the product page with the variant selected.

    • handlestring
    • imagestring | null

      Image URL.

    • propertiesobject | null

      Line item properties, such as { Engraving: "Anna" }.

Events

Example
await tokiWallet.addToShopifyCart({
  variantId: "43872109510766",
  quantity: 1,
  properties: { Engraving: "Anna" },
})
Result
{
  "items": [
    {
      "key": "43872109510766:9c2e71a4b8d03f5e",
      "variant_id": 43872109510766,
      "product_id": 7982301462638,
      "quantity": 1,
      "title": "Linen Shirt - Small",
      "product_title": "Linen Shirt",
      "variant_title": "Small",
      "price": 4999,
      "final_price": 4999,
      "final_line_price": 4999,
      "url": "/products/linen-shirt?variant=43872109510766",
      "handle": "linen-shirt",
      "image": "https://cdn.shopify.com/s/files/1/0601/2345/files/linen-shirt.jpg?v=1712345678",
      "properties": {
        "Engraving": "Anna"
      }
    }
  ]
}
action

updateShopifyCartItemQuantity#

tokiWallet.updateShopifyCartItemQuantity({ id, quantity })→ Promise<ShopifyCart>

Changes the quantity of a cart line.

id is the line's key from getShopifyCart(). A variant ID works too; the key picks the exact line when a variant is on several lines. A quantity of 0 removes the line.

Afterwards the SDK updates your theme's cart count and dispatches the same cart events as addToShopifyCart(), except toki:public:cart:added. If Shopify refuses the change, it dispatches toki:public:cart:error with { message, status } and rejects with the same message.

Options

one object
  • idstring | numberRequired

    The line's key, or a variant ID.

  • quantitynumberRequired

Returns

Promise<ShopifyCart>

The updated cart.

  • tokenstring

    Cart token.

  • item_countnumber

    Total quantity across all lines.

  • items_subtotal_pricenumber

    Total of the lines after line discounts, in cents.

  • total_pricenumber

    Total after all discounts, in cents.

  • original_total_pricenumber

    Total before discounts, in cents.

  • total_discountnumber

    Sum of all discounts, in cents.

  • currencystring

    Cart currency, such as "USD".

  • itemsCartLine[]
    14 fields per item
    • keystring

      Unique ID of the line. Pass it to change or remove the line.

    • variant_idnumber
    • product_idnumber
    • quantitynumber
    • titlestring

      Product and variant title, such as "Linen Shirt - Small".

    • product_titlestring
    • variant_titlestring | null

      null for a product with a single variant.

    • pricenumber

      Unit price in cents.

    • final_pricenumber

      Unit price after line discounts, in cents.

    • final_line_pricenumber

      Line total after line discounts, in cents.

    • urlstring

      Path of the product page with the variant selected.

    • handlestring
    • imagestring | null

      Image URL.

    • propertiesobject | null

      Line item properties, such as { Engraving: "Anna" }.

  • cart_level_discount_applicationsobject[]

    Discounts applied to the whole cart, such as a discount code.

Events

Example
const { items } = await tokiWallet.getShopifyCart()
const cart =
  await tokiWallet.updateShopifyCartItemQuantity({
    id: items[0].key,
    quantity: 3,
  })
Result
{
  "token": "Z2NwLWV1cm9wZS13ZXN0MTowMUpBQ0E5?key=4c2b8f0e9d1a",
  "item_count": 3,
  "items_subtotal_price": 14997,
  "total_price": 14997,
  "original_total_price": 14997,
  "total_discount": 0,
  "currency": "USD",
  "items": [
    {
      "key": "43872109510766:5f1d3c0b9a7e2f4d",
      "variant_id": 43872109510766,
      "product_id": 7982301462638,
      "quantity": 3,
      "title": "Linen Shirt - Small",
      "product_title": "Linen Shirt",
      "variant_title": "Small",
      "price": 4999,
      "final_price": 4999,
      "final_line_price": 14997,
      "url": "/products/linen-shirt?variant=43872109510766",
      "handle": "linen-shirt",
      "image": "https://cdn.shopify.com/s/files/1/0601/2345/files/linen-shirt.jpg?v=1712345678",
      "properties": {}
    }
  ],
  "cart_level_discount_applications": []
}
action

deleteShopifyCartItem#

tokiWallet.deleteShopifyCartItem({ id })→ Promise<ShopifyCart>

Removes a line from the Shopify cart.

Works like updateShopifyCartItemQuantity() with a quantity of 0. id is the line's key, or a variant ID.

Options

one object
  • idstring | numberRequired

    The line's key, or a variant ID.

Returns

Promise<ShopifyCart>

The updated cart.

  • tokenstring

    Cart token.

  • item_countnumber

    Total quantity across all lines.

  • items_subtotal_pricenumber

    Total of the lines after line discounts, in cents.

  • total_pricenumber

    Total after all discounts, in cents.

  • original_total_pricenumber

    Total before discounts, in cents.

  • total_discountnumber

    Sum of all discounts, in cents.

  • currencystring

    Cart currency, such as "USD".

  • itemsCartLine[]
    14 fields per item
    • keystring

      Unique ID of the line. Pass it to change or remove the line.

    • variant_idnumber
    • product_idnumber
    • quantitynumber
    • titlestring

      Product and variant title, such as "Linen Shirt - Small".

    • product_titlestring
    • variant_titlestring | null

      null for a product with a single variant.

    • pricenumber

      Unit price in cents.

    • final_pricenumber

      Unit price after line discounts, in cents.

    • final_line_pricenumber

      Line total after line discounts, in cents.

    • urlstring

      Path of the product page with the variant selected.

    • handlestring
    • imagestring | null

      Image URL.

    • propertiesobject | null

      Line item properties, such as { Engraving: "Anna" }.

  • cart_level_discount_applicationsobject[]

    Discounts applied to the whole cart, such as a discount code.

Events

Example
const { items } = await tokiWallet.getShopifyCart()
const cart = await tokiWallet.deleteShopifyCartItem({
  id: items[0].key,
})
Result
{
  "token": "Z2NwLWV1cm9wZS13ZXN0MTowMUpBQ0E5?key=4c2b8f0e9d1a",
  "item_count": 0,
  "items_subtotal_price": 0,
  "total_price": 0,
  "original_total_price": 0,
  "total_discount": 0,
  "currency": "USD",
  "items": [],
  "cart_level_discount_applications": []
}
action

applyDiscountToShopifyCart#

tokiWallet.applyDiscountToShopifyCart(discountCode)→ Promise<boolean>

Applies a discount code to the Shopify cart.

Shopify keeps the code for the customer's session and applies it to the cart and at checkout. To show the discount, read the cart with getShopifyCart() afterwards. Dispatches toki:public:cart:discount-code:added with { discountCode }, then toki:public:cart:updated.

Arguments

  • discountCodestringRequired

    The code, such as "WELCOME10".

Returns

Promise<boolean>

Resolves true once Shopify has the code.

Events

Example
await tokiWallet.applyDiscountToShopifyCart("WELCOME10")
Result
true