# `createCart(cartInput)`

Creates a cart for a buyer in a webstore.

## Syntax

```javascript
import { createCart } from 'lightning/commerceCartApi';
createCart(cartInput: object): Promise<CartSummary>
```

## Commerce API Resource

```
POST /commerce/webstores/{webstoreId}/carts
```

`createCart` uses this [Commerce API resource](https://developer.salesforce.com/docs/atlas.en-us.chatterapi.meta/chatterapi/connect_resources_commerce_webstore_carts.htm).

## Parameters

- `webstoreId`—(Required) The ID of the webstore. The ID prefix is `0ZE`.
- `version`—(Required) The API version for the adapter, for example, `v62.0`. Without this parameter, the call doesn't fire.
- `cart`—(Required) The cart input object. Accepts these fields:
  - `effectiveAccountId`—(Optional) The effective account ID for the cart.
  - `name`—(Optional) The friendly name for the cart.
  - `type`—(Optional) The cart type. Valid values: `Cart`, `PayNowReadOnly`, `ReadOnly`, `Template`.
  - `currencyIsoCode`—(Optional) The ISO currency code for the cart, for example, `USD`.

:::note
Additional fields (`orderOwnerId`, `isSecondary`, `description`, `typeAsString`, `customFields`) are available in API version 64.0 and later.
:::

## Returns

A `Promise` that resolves with a `CartSummary` object with these fields:

- `cartId`—The ID of the created cart.
- `name`—The name of the cart.
- `status`—The status of the cart, for example, `Active`.
- `type`—The cart type.
- `webStoreId`—The ID of the webstore associated with the cart.

## Usage

`createCart` is an imperative function, not a `@wire` adapter. Call it in response to a user action.

```javascript
import { LightningElement } from 'lwc';
import { createCart } from 'lightning/commerceCartApi';

export default class CreateCartExample extends LightningElement {
  async handleCreateCart() {
    try {
      const result = await createCart({
        webstoreId: '0ZExx0000000001',
        version: 'v62.0',
        cart: {
          name: 'My Cart',
          currencyIsoCode: 'USD',
        },
      });
      console.log('Cart created:', result.cartId);
    } catch (error) {
      console.error('Error creating cart:', error);
    }
  }
}
```

## Error Handling

If the call fails, the `Promise` rejects with an error object containing an `errorCode` and a `message`.

**See Also**

- [`deleteCart(cartInput)`](/docs/platform/lwc/guide/reference-lightning-commerce-cart-delete-cart.md)
