# init: Initialize the SDK

> STEP 3-1

```ts
window.rcmr.checkoutPlus.init(options: CheckoutPlusInitOptions): CheckoutPlusSession
```

`window.rcmr.checkoutPlus.init(options)` initializes the SDK and returns a `CheckoutPlusSession`. It makes no network requests at initialization. On the first call to `mount()`, `refresh()`, or `prepareCheckout()`, the SDK fetches the cart and the Checkout+ metafield through `CartConnector`'s `getCart`.

- When to call: Call it once after the SDK loads ([Step 3: Initialize the SDK](./shopify-headless-init.html)). Keep exactly one running `CheckoutPlusSession` per page at all times ([One running `CheckoutPlusSession` per page](#one-running-checkoutplussession-per-page)).
- Return value: A `CheckoutPlusSession` with `mount`, `refresh`, `prepareCheckout`, `isCheckoutPlusLine`, and `destroy`.

## Exceptions and failures

- Exceptions: None.
- If `storeId` or `cart` is invalid: This is when `storeId` is missing or an empty string, or when `cart` is missing or its `getCart` or `updateCheckoutPlusLines` is not a function. It returns a `CheckoutPlusSession` in the same state as after `destroy()`, and notifies `onError` with `invalid-options` after returning. The banner is not shown, and `prepareCheckout()` completes immediately with `{ attached: false }`.
- Network failures: `init()` makes no network requests, so none happen here. They happen in the first `mount()`, `refresh()`, or `prepareCheckout()`.

## Options you can pass

| Option | Required | If omitted | Description |
| --- | --- | --- | --- |
| `storeId` | Required | — | The store ID ([Development Shopify store and store ID](./shopify-headless.html#development-shopify-store-and-store-id)) |
| `cart` | Required | — | The `CartConnector` the SDK user implemented ([`getCart`](./shopify-headless-reference-get-cart.html) and [`updateCheckoutPlusLines`](./shopify-headless-reference-update-checkout-plus-lines.html)). One `CheckoutPlusSession` uses only the one `CartConnector` passed here |
| [`onCheckoutPlusLinesChange`](./shopify-headless-reference-on-checkout-plus-lines-change.html) | Required if you show cart lines or a cart total | Not called | A function called after the SDK adds or removes the Checkout+ product. Takes no arguments. Fetch the cart again in it |
| `locale` | Optional | The value of `<html lang>` | The amount format. Only `ja-JP` is supported for now; if you set another value, amounts are shown in the `ja-JP` format. The default text does not change |
| `displaySettings` | Optional | Recustomer's default content | The banner text and the details dialog content ([`displaySettings` below](#displaysettings-text-and-details-dialog)) |
| `initialSelection` | Optional | `false` | The initial state of the checkbox. Set it to `true` to show the box checked ([`initialSelection` below](#initialselection-initial-state-of-the-checkbox)) |
| [`onError`](./shopify-headless-reference-on-error.html) | Optional | Not called | A function that receives why Checkout+ is unavailable, and failures |

- When `onCheckoutPlusLinesChange` is called is on the [`onCheckoutPlusLinesChange`](./shopify-headless-reference-on-checkout-plus-lines-change.html) page.
- The currency comes from the cart (`cart.currency`), so you don't need to set it with `locale`.
- To show another language, specify the text with `displaySettings` ([`displaySettings` below](#displaysettings-text-and-details-dialog)).
- "by Recustomer" and "Powered by Recustomer" are fixed and cannot be changed ([Parts of the banner in `mount`](./shopify-headless-reference-mount.html#parts-of-the-banner)).

```ts
// Example of init()
// Before the SDK finishes loading, window.rcmr.checkoutPlus doesn't exist and nothing is initialized
// Run this after the SDK finishes loading (the recustomer-checkout-plus:ready event) (Step 1)
const checkoutPlus = window.rcmr?.checkoutPlus?.init({
  storeId: '(store ID)',                                                  // Required: the ID for each store
  cart: cartConnector,                                                       // Required: CartConnector you implemented
  onCheckoutPlusLinesChange: () => { /* Fetch your cart again */ }, // Required if you show cart lines or a cart total
  locale: 'ja-JP',                                                           // Optional: amount format
  displaySettings: { /* Banner text, etc. */ },                              // Optional: display settings
  initialSelection: false,                                                   // Optional: initial state of the checkbox
  onError: (error) => reportError(error),                                    // Optional: send to your existing monitoring system
})
```

## `displaySettings`: Text and details dialog

Specifies the text and images of the banner and the details dialog. Every field is optional. Omitted fields show Recustomer's default content. The default text and the default accessible names are in Japanese regardless of `locale`. To show another language, specify the text in that language. Change the look (colors, corner radius, font) with CSS ([Change the look with CSS](./shopify-headless-reference-mount.html#change-the-look-with-css)).

| Field | Type | Default | Description |
| --- | --- | --- | --- |
| `title` | `string` | 「あんしんプラス」 (Anshin Plus) | The banner title. Displayed as a string, and HTML is not supported |
| `description` | `string` | None (no description is shown) | The banner description. Displayed as a string. It can include the estimated coupon cashback amount (see the placeholder below) |
| `detailsButtonLabel` | `string` | 「詳しく見る」 (See details) | Text of the button that opens the details dialog |
| `shippingBadgeLabel` | `string` | 「返品送料無料」 (Free return shipping) | Text of the 「返品送料無料」 badge. Displayed as a string, and HTML is not supported |
| `cashbackBadgeLabel` | `string` | 「クーポンバック」 (Coupon cashback) | Text of the 「クーポンバック」 badge. Displayed as a string, and HTML is not supported |
| `exclusionNotice` | `string` | 「※{excludedItem}は対象外商品です。」 (※{excludedItem} is not eligible.) | The excluded-item notice. `{excludedItem}` is replaced with the list of excluded product names |
| `excludedItemFormat` | `string` | `「{name}」` (the name in Japanese corner brackets) | How one product name is written in the excluded-item notice. `{name}` is replaced with the product name |
| `excludedItemSeparator` | `string` | None (names are written one after another) | The string placed between excluded product names (e.g. `, `) |
| `detailsModal.heroImageUrl` | `string` | None (no image is shown) | URL of the image shown at the top of the details dialog. The details dialog is 480px wide, so a 960px-wide image is recommended for high-resolution screens |
| `detailsModal.logoImageUrl` | `string` | None (no logo is shown) | URL of the logo in the details dialog |
| `detailsModal.logoImageAlt` | `string` | `ストアロゴ` (Store logo) | Alternative text of the logo image (for screen readers) |
| `detailsModal.html` | `string` | Recustomer's default text | HTML for the body of the details dialog. Scripts, event attributes, and the like are removed before it is displayed. If it is an empty string, nothing is shown |
| `detailsModal.ariaLabel` | `string` | `{title}の詳細` ({title} details) | Accessible name of the details dialog. `{title}` is replaced with the value of `title` |
| `detailsModal.closeButtonAriaLabel` | `string` | `閉じる` (Close) | Accessible name of the close button |
| `detailsModal.poweredByLinkAriaLabel` | `string` | `Recustomer（別タブで開く）` (Recustomer, opens in a new tab) | Accessible name of the "Powered by Recustomer" link |

- Except for `detailsModal.html`, every text field is displayed as a string, and HTML is not supported. For these fields, an empty string is treated the same as an omitted field.
- When the excluded products are A and B, the default shows 「※「A」「B」は対象外商品です。」 (※"A" "B" are not eligible.).
- For badges, you can change only the text. Whether each badge appears depends on the range's benefits ([Parts of the banner in `mount`](./shopify-headless-reference-mount.html#parts-of-the-banner)).

`{cashback}` in `description` is replaced with the estimated coupon cashback amount. The estimate is calculated according to the Shopify store's tax setting (prices including or excluding tax).

For ranges with no estimated amount, the `{cashback}` part becomes empty. Write text that still reads naturally when it is empty. Recustomer calculates the final amount after the order.

In ranges where the coupon cashback is a fixed amount, `{cashback}` becomes that amount.

```ts
// Add to the options of init(). This is an example for a Japanese store
displaySettings: {
  title: 'あんしんプラス',
  description: 'ご購入後の返品・交換の送料が無料になります。さらに {cashback} 分のクーポンをお返しします。',
  detailsButtonLabel: '詳しく見る',
  shippingBadgeLabel: '返品送料無料',
  cashbackBadgeLabel: 'クーポンバック',
  exclusionNotice: '※{excludedItem}は対象外商品です。',
  excludedItemFormat: '「{name}」',
  excludedItemSeparator: '',
  detailsModal: {
    heroImageUrl: 'https://cdn.example.com/checkout-plus/hero.png',
    logoImageUrl: 'https://cdn.example.com/logo.png',
    logoImageAlt: 'ストアのロゴ',
    html: '<h2>安心して、もっと自由にショッピングを。</h2><p>…</p>',
    ariaLabel: '{title}の詳細',
    closeButtonAriaLabel: '閉じる',
    poweredByLinkAriaLabel: 'Recustomer（別のタブで開きます）',
  },
},
```

`displaySettings` uses the values passed when `init()` is called. If you load them from a CMS or similar, call `init()` after loading finishes. To change them, call `destroy()` and then call `init()` again.

## `initialSelection`: Initial state of the checkbox

Shows the banner checkbox in the specified initial state.

| `initialSelection` | Initial state |
| --- | --- |
| `false` (default) | Shown unchecked |
| `true` | Shown checked. Only takes effect for stores Recustomer has allowed. For stores that are not allowed, it is shown unchecked |

For stores that set `initialSelection` to `true`, the SDK behaves as follows.

- It adds the Checkout+ product by default only after the banner has been rendered. It does not add it if the shopper proceeds to checkout from a screen that does not show the banner, or before the banner is rendered. If the range changes after the banner is rendered and the new price has not been rendered yet, it still adds the product for the range at the moment the checkout button is pressed.
- If the shopper unchecks the box, the SDK remembers this until the tab is closed and does not check it again. It does not remember that the shopper checked the box.
- When the cart changes and the price changes, the selection follows [Selection when the displayed price changes](#selection-when-the-displayed-price-changes).
- When the shopper comes back from checkout, the checkbox returns to its initial state, both when [the previous screen is shown as is](./overview.html#glossary) and when the page reloads. If the SDK has recorded that the shopper unchecked it, it stays unchecked.

### Selection when the displayed price changes

When the displayed price changes (for example, because the cart changed and the range changed), the state of the checkbox does not change. If the shopper had checked the box, it stays checked. If the shopper had unchecked it, it stays unchecked.

If the checkout button is pressed before the new price is shown in the banner (for example, after the cart was changed in another tab), the SDK adds or removes the Checkout+ product as currently selected, for the range at the moment the button is pressed ([`prepareCheckout` behavior](./shopify-headless-reference-prepare-checkout.html#specification)).

## One running `CheckoutPlusSession` per page

When you recreate it, first call `destroy()` on the current `CheckoutPlusSession`. If you call `init()` without doing so, both run at the same time and the following happens.

| What both `CheckoutPlusSession` objects have | What happens |
| --- | --- |
| Monitoring whether the previous screen is shown as is | When the previous screen is shown as is, both fetch the cart and try to remove the Checkout+ product, duplicating cart updates |
| Callbacks | The same callback is called twice. Functions of components that were already discarded may also be called |
| Updates in progress | Results of the old `CheckoutPlusSession`'s updates get mixed into the new screen's processing |
| Shopper's selection (whether to add the Checkout+ product) | The two `CheckoutPlusSession` objects hold separate selections, and it becomes unclear which one is correct |

For when to call `destroy()`, see [Deciding whether to call `destroy()`](./shopify-headless-reference-destroy.html#deciding-whether-to-call-destroy).
