# Step 3: Initialize the SDK

> STEP 2-3

Pass the store ID and `CartConnector` to `init()` to initialize the SDK. Initialization makes no network requests. You use the `CheckoutPlusSession` that `init()` returns in the following steps (such as `mount()` in Step 4 and `prepareCheckout()` in Step 5). For what the SDK does and when after initialization, see [SDK states and transitions](./shopify-headless-behavior.html).

Follow these rules when you initialize and call `refresh()`.

- Call `init()` from a place that runs only once each time the page loads. If you call it from a place that can run again on the same page, such as inside a component, call `destroy()` as described in [Deciding whether to call `destroy()`](./shopify-headless-reference-destroy.html#deciding-whether-to-call-destroy). Don't initialize inside a component that opens and closes, such as a cart drawer; initialize on the page, and call only `mount()` and `unmount()` in the drawer (same section).
- Call `refresh()` once right after initialization. This removes a Checkout+ product left over from the previous screen, and it's needed even on screens without the banner. If you initialize again, call it again right after that. In a SPA, if you don't initialize again when the screen changes, you don't need to call it again ([`refresh`: Call it once right after initialization](./shopify-headless-reference-refresh.html#call-it-once-right-after-initialization)).
- When your code changes the cart (changing quantities, adding and removing products, applying discount codes, and so on), call `refresh()` after the update finishes ([`refresh()`](./shopify-headless-reference-refresh.html)). After the SDK adds or removes the Checkout+ product, you only fetch the cart for your UI again in `onCheckoutPlusLinesChange`; you don't need to call `refresh()`.

The following example uses `CheckoutPlusProvider` (`components/CheckoutPlusProvider.tsx`) in [4-1. Examples that connect to the SDK](./shopify-headless-sample-code-sdk.html#components-checkoutplusprovider-tsx). `CheckoutPlusProvider` is an example that calls `init()`, `refresh()`, and `destroy()` according to the first two rules above, and you can also use it as is.

- It calls `init()` when it is rendered. While `enabled` is `false`, it doesn't call it, and calls it when `enabled` becomes `true`.
- It calls `destroy()` when `CheckoutPlusProvider` goes away and when `storeId`, `locale`, or `enabled` changes.
- It calls `refresh()` once, right after it calls `init()`.

If you use `CheckoutPlusProvider`, don't call `init()` outside it. When your code changes the cart, call `refresh()` on the `CheckoutPlusSession` returned by `useCheckoutPlus()` after the code that changes the cart.

The following `StoreCheckoutPlus` is an example that passes `CartConnector`, the function that refetches the cart, and the store ID to `CheckoutPlusProvider`. Place it in the shared layout that every screen shows, inside your existing cart Provider, and wrap the whole page. This makes it call `refresh()` once even on screens without the banner. `useCart()` in the example is a hook that returns your cart state (the cart and a function to refetch it). Replace it with your existing cart React Context or state management library. In the Next.js App Router, use it inside a component marked with `'use client'`. The files in [4. Sample code](./shopify-headless-sample-code.html#files) already include it.

`cartConnector` in the example is the `CartConnector` you implemented in step 2. Its name and file location (`@/lib/checkout-plus-connector`) are examples.

```tsx
// `components/StoreCheckoutPlus.tsx` (React 19 or later example)
// Place it in the shared layout, inside your existing cart Provider, and wrap the whole page
'use client'
import type { ReactNode } from 'react'
import { CheckoutPlusProvider } from '@/components/CheckoutPlusProvider'
import { cartConnector } from '@/lib/checkout-plus-connector'
// Cart state (example; replace it with your existing cart React Context or state management library)
import { useCart } from '@/components/cart/cart-context'

export function StoreCheckoutPlus({
  storeId,
  children,
}: {
  // This store's store ID
  storeId: string
  children: ReactNode
}) {
  const { reloadCart } = useCart()
  return (
    <CheckoutPlusProvider
      storeId={storeId}
      // Your `CartConnector` (Step 2)
      cart={cartConnector}
      // Refetch the cart for the UI after the SDK adds or removes a Checkout+ product
      reloadCart={reloadCart}
      // Send to your monitoring (reportError is a placeholder for your logic)
      onError={(error) => reportError(error)}
    >
      {children}
    </CheckoutPlusProvider>
  )
}
```

Adapt the values passed to `CheckoutPlusProvider` (`storeId`, `cart`, `reloadCart`, and `onError`) to your settings and logic. `reloadCart` is called from `onCheckoutPlusLinesChange` of `init()`, and `onError` from `onError` of `init()`.

- Which values passed to `init()` are required, and what happens when they are omitted, is in [`init(options)`](./shopify-headless-reference-init.html#options-you-can-pass). The example passes `onError` to send errors to your monitoring system.
- How to pass the store ID is in [Development Shopify store and store ID](./shopify-headless.html#development-shopify-store-and-store-id).
- The `kind` names delivered to `onError` are listed in the table in [`onError`](./shopify-headless-reference-on-error.html).

To use the display settings (`displaySettings`), add them to the props of `CheckoutPlusProvider` (for details, see [`displaySettings`: Text and details dialog](./shopify-headless-reference-init.html#displaysettings-text-and-details-dialog)). `cart`, `displaySettings`, and `initialSelection` use the values passed when `init()` is called. `CheckoutPlusProvider` doesn't recreate the SDK when these values change, so if you load them from a CMS or similar, set `enabled` to `false` until loading finishes. The screens inside `CheckoutPlusProvider` render first, and `init()` is called once the values are ready.

```tsx
<CheckoutPlusProvider
  // … (`storeId`, `cart`, `reloadCart`, and `onError` from above)
  displaySettings={{ title: 'あんしんプラス', description: '…' }} // Optional: banner text, etc. (`displaySettings` of `init`)
  enabled={settingsLoaded} // Optional: if you load from a CMS or similar, false until loading finishes (settingsLoaded is a placeholder for your loading state)
>
```
