> For the complete documentation index, see [llms.txt](https://docs.ipaper.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ipaper.io/flipbook-javascript-api/commands-and-events/basket.md).

# Basket

## Commands

### `open`

Opens the basket.

<pre class="language-javascript"><code class="lang-javascript"><strong>instance.basket.open();
</strong></code></pre>

### `close`

Closes the basket.

```javascript
instance.basket.close();
```

### `getState`

Gets the current state of the basket.

```javascript
// You can either await the result of the command
const result = await instance.basket.getState();

// OR pass a callback
instance.basket.getState((result) => {
    // YOUR CODE HERE
});
```

The command will return an object with the following properties:

| Property | Type      | Description                  |
| -------- | --------- | ---------------------------- |
| `isOpen` | `boolean` | Is the basket currently open |

### `addProduct`

Adds a product to the basket. The command accepts a single argument, consisting of the properties below:

| Property      | Type     | Description                          |
| ------------- | -------- | ------------------------------------ |
| `title`       | `string` | Product title                        |
| `description` | `string` | Product description                  |
| `productId`   | `string` | Product Id                           |
| `price`       | `number` | Product price                        |
| `originPage`  | `number` | Page where the product is located on |
| `quantity`    | `number` | Quanity of product to add            |

Below is an example use:

```javascript
instance.basket.addProduct({
    title: 'My product',
    description: 'Product description',
    productId: 'PROD-25B',
    price: '29.95',
    originPage: 6,
    quantity: 1
});
```

## Events

### `onOpened`

This subscription will trigger every time the basket is opened.

<pre class="language-javascript"><code class="lang-javascript"><strong>instance.basket.onOpened(() => {
</strong>    // YOUR CODE HERE
});
</code></pre>

### `onClosed`

This subscription will trigger every time the basket is closed.

<pre class="language-javascript"><code class="lang-javascript"><strong>instance.basket.onClosed(() => {
</strong>    // YOUR CODE HERE
});
</code></pre>

### `onToggled`

This subscription will trigger every time the basket is toggled between open and closed state.

<pre class="language-javascript"><code class="lang-javascript"><strong>instance.basket.onToggled(() => {
</strong>    // YOUR CODE HERE
});
</code></pre>

### `onProductAdd`

This subscription will trigger whenever a product is added to the basket.

{% hint style="warning" %}
**Note:**

Products added through the `addProduct` API command will not trigger this subscription.
{% endhint %}

```javascript
instance.basket.onProductAdd((product) => {
    // YOUR CODE HERE
});
```

The callback contains a value/result of the product that was added, containing the following properties:

| Property      | Type     | Description                                          |
| ------------- | -------- | ---------------------------------------------------- |
| `title`       | `string` | Product title                                        |
| `description` | `string` | Product description                                  |
| `productId`   | `string` | Product ID                                           |
| `price`       | `number` | Product price                                        |
| `pageIndex`   | `number` | Page index where the product is located\* (see note) |
| `originPage`  | `number` | Page where the product is located on                 |
| `quantity`    | `number` | Quanity of product added                             |
| `packageSize` | `number` | Number of items per package                          |

{% hint style="info" %}
**Note:**

Please note that `pageIndex` starts at `0`. So the 1st page of your flipbook will have a `pageIndex` of `0`, page 2 a `pageIndex` of `1` and so on. To get the actual pagenumber you should use `originPage` instead.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.ipaper.io/flipbook-javascript-api/commands-and-events/basket.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
