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

# Events

> Reference for the Swish SDK event bus - subscribe to item, list, cart, and notification events.

The `swish.events` API lets you subscribe to events emitted by the SDK. Use it to react to changes in saved items, lists, cart state, and notifications.

## `subscribe(type, callback)`

Subscribe to one or more events. Returns an unsubscribe function.

```js theme={null}
const unsubscribe = swish.events.subscribe("item-create", (event) => {
  console.log("Item created:", event.detail);
});

// Stop listening
unsubscribe();
```

Pass an array to subscribe to multiple events at once:

```js theme={null}
const unsubscribe = swish.events.subscribe(
  ["item-create", "item-delete"],
  (event) => {
    console.log("Item changed");
  },
);
```

## `unsubscribe(type, callback)`

Remove a specific event listener. Equivalent to calling the function returned by `subscribe()`.

```js theme={null}
const handler = (event) => console.log(event.detail);

swish.events.subscribe("item-create", handler);
swish.events.unsubscribe("item-create", handler);
```

## Event reference

### Item events

| Event | Trigger | Detail |
| - | - | - |
| `item-create` | A saved item is created. | The created `Item`. Does not include list assignments. |
| `item-update` | A saved item is updated. | The updated `Item`. |
| `item-delete` | A saved item is removed. | The deleted `Item`. A bulk delete publishes an array of items. |
| `item-lists-update` | An item's list assignments change. | The updated `ItemDetail`, including `listIds`. |

### List events

| Event | Trigger | Detail |
| - | - | - |
| `list-create` | A list is created. | The created list. |
| `list-update` | A list is updated (name, access, etc.). | The updated list. |
| `list-delete` | A list is deleted. | The deleted list. |

### Cart events

| Event | Trigger | Detail |
| - | - | - |
| `cart-add` | An item is added to the Shopify cart. | The cart response from the Ajax API. |
| `cart-update` | The Shopify cart is updated. | The cart response from the Ajax API. |
| `cart-change` | The Shopify cart changes, including item removals. | The cart change response, with `items_removed` when applicable. |
| `cart-clear` | The Shopify cart is cleared. | The cart response from the Ajax API. |

### Notification events

| Event | Trigger | Detail |
| - | - | - |
| `notification-update` | A notification is updated (e.g. marked as read). | The updated notification. |
| `notification-delete` | A notification is deleted. | The deleted notification. |

### Auth events

| Event | Trigger | Detail |
| - | - | - |
| `token-update` | The auth token is refreshed. | `null` |

## Examples

### Update a cart count badge

```js theme={null}
swish.events.subscribe(
  ["cart-add", "cart-update", "cart-change", "cart-clear"],
  async () => {
    const cart = await swish.ajax.fetchCart();
    badge.textContent = cart.item_count;
  },
);
```

### Track saves for analytics

```js theme={null}
swish.events.subscribe("item-create", (event) => {
  analytics.track("product_saved", {
    itemId: event.detail.id,
  });
});
```

### Refresh UI after list changes

```js theme={null}
swish.events.subscribe(["list-create", "list-update", "list-delete"], () => {
  refreshListDisplay();
});
```


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