
<SdkPage group="setup" />

<Method id="init" signature="Subbly.init(config)">

Starts Subbly.js with your Storefront API key and resolves with the SDK
instance. Every other method in this reference is a method of that instance.

`init` always loads the shop first, so `subbly.shop` is filled in by the time
the promise resolves. It then checks the stored access token, and, when you pass
`loadCart`, loads or creates a cart and copies the cart currency onto the
instance. A failed shop load rejects the promise.

Call `init` once per page. The SDK builds one shared HTTP client and keeps it,
so a second call with a different `apiKey`, `apiUrl` or `lang` quietly reuses
the first one.

<Params>
  <Param name="config" required type="SubblyConfig">
    How to start the SDK. Leave it out, or pass an empty `apiKey`, and `init`
    rejects with `Error('Missing required apiKey config')`.

    <Properties label="config">
      <Param name="apiKey" required type="string">
      Your Storefront API key. The SDK sends it as the `x-api-key` header on
      every request, and you get it from
      [Shop settings](https://subbly.co/admin/settings/api-keys) in Subbly Admin.
      </Param>
      <Param name="apiUrl" optional type="string">
      Base URL of the Storefront API. Defaults to
      `https://api.subbly.co/storefront/v1/`. Change it only for a proxy or a
      test environment. An empty string falls back to the default.
      </Param>
      <Param name="lang" optional type="string">
      Language code, sent as the `accept-language` header. Empty by default, so
      the server picks. Use a code from `subbly.shop.languages[].code`, and
      change it later with `subbly.setLanguage`.
      </Param>
      <Param name="cartId" optional type="string">
      Cart to load at start-up. The SDK reads it only when `loadCart` is true,
      and falls back to the `subbly_cart_id` cookie when you leave it out.
      </Param>
      <Param name="loadCart" optional type="boolean">
      Load or create a cart during start-up. `false` by default. When it is
      true, the SDK loads the cart named by `cartId` or the cookie, creates a
      new cart when there is none or the load fails, writes the
      `subbly_cart_id` cookie, and calls `subbly.setCurrency` with the cart
      currency. When it is false, `subbly.cart` has no `id` and the cart
      methods that need one fail.
      </Param>
      <Param name="accessToken" optional type="string">
      Customer access token to verify at start-up. It beats the
      `subbly_access_token` cookie, and the SDK writes it to that cookie when
      the check passes. A failed check clears the cookie; it does not reject.
      </Param>
    </Properties>
  </Param>
</Params>

<Returns type="Promise<Subbly>">
The SDK instance.
<Properties label="instance">
  <Param name="VERSION" type="string">
  Version of the SDK package.
  </Param>
  <Param name="config" type="SubblyConfig">
  The `apiKey`, `apiUrl` and `lang` the SDK started with.
  </Param>
  <Param name="cart" type="CartModel">
  The cart: every field of the cart object, plus the cart methods. The
  [Cart](/reference/subbly-sdk/cart) page writes the fields out.
  </Param>
  <Param name="shop" type="SubblyShop">
  The shop: every field of the shop object, plus `shop.load`. The
  [Shop](/reference/subbly-sdk/shop) page writes the fields out.
  </Param>
  <Param name="addresses, auth, bundles, checkout, countries, customers, funnels, lead, metafield, paymentIntents, pickupInfo, products, stock, subscriptions, surveys, wallet" type="object">
  The rest of the namespaces, one per group in this reference. They are all
  built during `init` and are always present.
  </Param>
</Properties>
</Returns>

<Example>

```js title="Start the SDK"
import Subbly from '@subbly/sdk'

const subbly = await Subbly.init({
  apiKey: 'API_KEY',
})
```

```js title="Start with a cart and a customer"
const subbly = await Subbly.init({
  apiKey: 'API_KEY',
  cartId: 'aaea067c-6364-4157-82e9-71b6edfd84a0',
  loadCart: true,
  accessToken: 'eyJh...eyJ9',
})
```

</Example>

</Method>

<Method id="set-currency" signature="subbly.setCurrency(currencyCode)">

Sets the currency for the prices in later product, bundle and survey responses.
The SDK sends it as the `x-currency` header.

It only fans the code out to `subbly.products.setCurrency`,
`subbly.bundles.setCurrency` and `subbly.surveys.setCurrency`, which each store
it for their own requests; an `x-currency` header you pass on a single call
still wins.

This does not change the cart currency. Use
`subbly.cart.update({ currencyCode })` for that. When `init` loads a cart, it
calls `setCurrency` with the cart currency itself.

<Params>
  <Param name="currencyCode" required type="string">
  A currency abbreviation the shop supports, such as `USD`. Read the list from
  `subbly.shop.currencies[].abbreviation`.
  </Param>
</Params>

<Returns type="void">
Nothing.
</Returns>

<Example>

```js title="Set the currency"
subbly.setCurrency('EUR')

// Later product prices come back in euros.
const products = await subbly.products.list()
```

</Example>

</Method>

<Method id="set-language" signature="subbly.setLanguage(languageCode)">

Sets the `accept-language` header the SDK sends with every later request.

<Params>
  <Param name="languageCode" required type="string">
  A language code the shop supports. Read the list from
  `subbly.shop.languages[].code`.
  </Param>
</Params>

<Returns type="void">
Nothing.
</Returns>

<Example>

```js title="Set the language"
subbly.setLanguage('fr')
```

</Example>

</Method>

<Method id="version" signature="subbly.VERSION">

A read-only property with the version of the `@subbly/sdk` package the page
loaded. Useful in a bug report.

<Returns type="string">
The version of the `@subbly/sdk` package on the page.
</Returns>

<Example>

```js title="Read the version"
console.log(subbly.VERSION)
```

</Example>

</Method>

<Method id="config" signature="subbly.config">

The three client values `init` received, exactly as you passed them. It leaves
out `cartId`, `loadCart` and `accessToken`, and it does not follow a later
`setLanguage` call.

`subbly.config` is not the configuration in use. The SDK applies its defaults
deeper in, when it builds the HTTP client, so a value you left out stays
`undefined` here.

<Returns type="SubblyConfig">
The values passed to `init`.
<Properties label="config">
  <Param name="apiKey" type="string">
  The Storefront API key you passed.
  </Param>
  <Param name="apiUrl" type="string | undefined">
  The base URL you passed, or `undefined`. The requests still go to
  `https://api.subbly.co/storefront/v1/` in that case.
  </Param>
  <Param name="lang" type="string | undefined">
  The language code you passed, or `undefined`. The server then picks the
  language.
  </Param>
</Properties>
</Returns>

<Example>

```js title="Read the configuration"
const subbly = await Subbly.init({ apiKey: 'API_KEY' })

subbly.config.apiKey // 'API_KEY'
subbly.config.apiUrl // undefined
subbly.config.lang // undefined
```

</Example>

</Method>

