Docs
STEP 3

Shopify (headless): SDK reference

This page lists the Checkout+ SDK methods and the functions the SDK calls. Details for each are on the following pages.

Methods and callbacks

NameWhen to useRoleWhen it is called
init(options)All storesInitializes the SDK and returns a CheckoutPlusSession. Makes no network requestsOnce, after the SDK loads
mount(target)All storesShows the banner in the given elementWhen the element for the banner is ready
refresh()All storesFetches the cart again and updates the price and the bannerOnce right after initialization, and when your code changes the cart
prepareCheckout()All storesAdds or removes the Checkout+ product based on the shopper's selection (whether to add the Checkout+ product), then verifies the cartWhen the checkout button is pressed
getCart()All storesA CartConnector function. Returns the latest cartCalled by the SDK
updateCheckoutPlusLines(change)All storesA CartConnector function. Adds and removes the Checkout+ productCalled by the SDK
onCheckoutPlusLinesChangeWhen you show cart lines or a cart totalNotifies you when the SDK adds or removes the Checkout+ productCalled by the SDK
onErrorAll stores (recommended)Reports why Checkout+ is unavailable, and failuresCalled by the SDK
isCheckoutPlusLine(line)When you render the cart linesTells whether a cart line is the Checkout+ product lineBefore rendering, once for each cart line
unmount()As neededRemoves one banner shown with mountWhen you discard the element that shows the banner
destroy()As neededRemoves all banners and shuts down the SDKWhen you discard the component that initialized the SDK. When you recreate the SDK with different values passed to init(), before calling the new init()

An example written in React is in 4. Sample code. With other frameworks, you call the same methods.

Exceptions and failures

No failure in the SDK blocks checkout. Your code needs almost nothing to handle failures. As the side that receives failures, all your code does is send what arrives in onError to its monitoring system.

SDK methods do not throw

SDK methods do not throw, and the Promises they return never reject. What happens when the cart or the benefit set (the commitment charge and benefits for each range) cannot be fetched is in What the SDK does for each trigger.

The behavior of each method is in "Exceptions and failures" on its page: init(options), mount(target), refresh(), prepareCheckout(), isCheckoutPlusLine(line), unmount(), destroy()

When an unexpected failure occurs inside the SDK

What happens when an unexpected failure occurs inside the UI components (the banner or the details dialog) is in the ui-failure row of the table in What the SDK does for each trigger.

When your functions throw

Make CartConnector (getCart and updateCheckoutPlusLines) throw when it fails. Even if a callback throws, the SDK keeps running. The SDK does not wait for Promises returned by callbacks. The SDK never calls a callback synchronously inside the method you called. It calls callbacks after the method returns its return value (a Promise, for methods that return one). It may call them before that Promise settles. This means you can use the CheckoutPlusSession returned by init() inside a callback.

How the SDK handles each function is in "When it throws" on that function's page: getCart, updateCheckoutPlusLines, onCheckoutPlusLinesChange, onError

What the SDK takes care of

The SDK handles the following, so you do not need to write code for them.

  • Discards stale cart responses. When cart fetches overlap, it uses only the result of the fetch that started last
  • Merges Checkout+ product lines into one at the checkout button (prepareCheckout()) when there are two or more
  • Determines the range again from the cart right before checkout, and adds or removes the Checkout+ product as selected (prepareCheckout specification)
  • Removes a Checkout+ product left in the cart when the shopper comes back from checkout (⑩ and ⑫ in What the SDK does for each trigger)
© Recustomer, Inc.