> ## Documentation Index
> Fetch the complete documentation index at: https://docs.totebot.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Custom shop integrations

> Let the agent read product details and manage the cart through your own shop API

Some shops run their own e-commerce backend instead of Shopify, WooCommerce or Medusa. For these shops, Totebot can connect the agent directly to your shop API, so it can read live product details, add products to the cart and change quantities.

> [!NOTE]
>
> Custom integrations are set up by Totebot support. They are not self-service. Contact support with your API documentation to get started. Once your integration is live, the only thing your site needs to do is tell the widget which cart the visitor has, as described below.

## How it works

1. Your site loads the Totebot widget as usual (see [Web SDK](https://docs.totebot.ai/api/web-sdk.md)).
2. Your site calls `window.totebot.setCart()` with the visitor's cart id and the cart fields your shop API needs.
3. When the visitor asks the agent to add a product or change their cart, Totebot calls your shop API with that cart id and those fields.
4. If the agent creates a new cart, the widget tells your page, so your site can show the updated cart.

## When to call setCart

Call `window.totebot.setCart()`:

* **On page load**, once you know the visitor's cart.
* **Every time the cart changes on your site**, for example after the visitor adds a product with your own buttons.
* **After checkout or logout**, with `null`, so the agent stops using the old cart.

You can call it at any time, also before the widget has loaded. Only the most recent call is used.

## Examples

### Page load

```javascript theme={null}
window.totebot.setCart({
  cartId: shop.cart.id,
  metadata: {
    storeId: 12,
    priceList: 'retail',
  },
});
```

### Cart change

```javascript theme={null}
shop.on('cart:updated', (cart) => {
  window.totebot.setCart({
    cartId: cart.id,
    metadata: { storeId: cart.storeId, priceList: cart.priceList },
  });
});
```

### Checkout or logout

```javascript theme={null}
shop.on('checkout:completed', () => window.totebot.setCart(null));
shop.on('logout', () => window.totebot.setCart(null));
```

### When the agent changes the cart

When the agent creates a cart for the visitor, the widget fires a `totebot:cart-updated` event on `window`. Use it to refresh your mini-cart, then call `setCart` again with the new cart:

```javascript theme={null}
window.addEventListener('totebot:cart-updated', async (event) => {
  const cart = await shop.loadCart(event.detail.cartId);
  shop.renderMiniCart(cart);
  window.totebot.setCart({
    cartId: cart.id,
    metadata: { storeId: cart.storeId, priceList: cart.priceList },
  });
});
```

## What to put in metadata

`metadata` is for the cart fields your shop API needs to handle the cart, such as a store id, a price list or a sales channel.

* Send only the fields your integration needs. Totebot support agrees the list with you, and any other field is dropped.
* Never send secrets: no API keys, tokens or passwords. Your API credentials are stored encrypted on our side and are never sent from the browser.
* Never send personal data such as names, emails, phone numbers or addresses. To tell the agent who the visitor is, use [`identifyUser`](https://docs.totebot.ai/api/web-sdk#identify-a-user).
* Keep it small. The cart id plus metadata may be up to 4 KB.

The widget accepts `setCart` only from your own site, so make sure your shop's domain is listed under the widget's allowed domains.

The agent never sees these values in its instructions. They are only forwarded to your shop API.

## setCart and identifyUser

`identifyUser` tells the widget who the visitor is. `setCart` tells it what is in their cart. Keep cart fields out of `identifyUser`, and keep personal data out of `setCart`.

## What support needs from you

To build your integration, Totebot support needs:

* API documentation for the product details, create cart and update cart endpoints.
* How the API authenticates (API key, bearer token, basic auth or signed requests).
* Test credentials and a test store or sandbox.
* The list of cart fields your site will send in `metadata`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.