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

# Add Line Item

> Adds a product variant to the cart with automatic shipment grouping based on fulfillment requirements

## Overview

The Add Line Item endpoint adds a new product to the shopping cart with these features:

* **Automatic Shipment Assignment**: Items are automatically grouped into shipments based on their fulfillment requirements
* **Catalog Integration**: Uses `add_to_cart_ref` object directly from catalog API responses
* **Variant Support**: Handles configurable products with variant handles
* **Cart Creation**: Automatically creates a new cart if one doesn't exist
* **Optional Cart Clearing**: Can clear existing cart before adding new item

## Authentication

> [card]
>
> **x-firmly-authorization** `string` *required*
>
> Device authentication token to identify and map the session

## Path Parameters

**domain** `string` *required*

Domain of the merchant website (e.g., `staging.luma.gift`)

## Query Parameters

**flush\_cart** `boolean` (default: false)

When `true`, clears the cart before adding the new item

## Request Body

**add\_to\_cart\_ref** `object` *required*

Product reference from catalog API containing variant information

**Properties:**

* `variant_id` (string, required): Product variant identifier
* `product_id` (string, optional): Parent product identifier
* `variant_handles` (array, optional): Array of variant configuration handles (e.g., `["color:blue", "size:large"]`)

**quantity** `number` *required*

Quantity to add (minimum 1)

## Response

Returns the complete shopping cart including the newly added item. See [Get Cart](https://firmly.mintlify.site/api-reference/cart-management/get-cart.md) for full response schema

### Key Response Features:

**shipments** `array`

Shows automatic shipment assignment for the new item

**addons.offers** `array`

Available addon services for the updated cart

**schema\_version** `string`

Indicates the cart schema version

## Code Examples

```bash cURL theme={null}
curl --request POST \
  --url https://api.firmly.work/api/v2/domains/staging.luma.gift/cart/line-items \
  --header 'Content-Type: application/json' \
  --header 'x-firmly-authorization: YOUR_TOKEN' \
  --data '{
    "add_to_cart_ref": {
      "variant_id": "WS12-XS-Orange"
    },
    "quantity": 1
  }'
```

```javascript JavaScript theme={null}
const response = await fetch('https://api.firmly.work/api/v2/domains/staging.luma.gift/cart/line-items', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'x-firmly-authorization': 'YOUR_TOKEN'
  },
  body: JSON.stringify({
    add_to_cart_ref: {
      variant_id: 'WS12-XS-Orange'
    },
    quantity: 1
  })
});

const cart = await response.json();
console.log(cart);
```

```python Python theme={null}
import requests

response = requests.post(
    'https://api.firmly.work/api/v2/domains/staging.luma.gift/cart/line-items',
    headers={
        'Content-Type': 'application/json',
        'x-firmly-authorization': 'YOUR_TOKEN'
    },
    json={
        'add_to_cart_ref': {
            'variant_id': 'WS12-XS-Orange'
        },
        'quantity': 1
    }
)

cart = response.json()
print(cart)
```

```php PHP theme={null}
<?php
$curl = curl_init();

$data = [
    'add_to_cart_ref' => [
        'variant_id' => 'WS12-XS-Orange'
    ],
    'quantity' => 1
];

curl_setopt_array($curl, [
  CURLOPT_URL => "https://api.firmly.work/api/v2/domains/staging.luma.gift/cart/line-items",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_POST => true,
  CURLOPT_HTTPHEADER => [
    "Content-Type: application/json",
    "x-firmly-authorization: YOUR_TOKEN"
  ],
  CURLOPT_POSTFIELDS => json_encode($data)
]);

$response = curl_exec($curl);
curl_close($curl);

$cart = json_decode($response, true);
print_r($cart);
?>
```

## Advanced Examples

### With Variant Handles

For configurable products with multiple options:

```json theme={null}
{
  "add_to_cart_ref": {
    "variant_id": "MH07-XS-Gray",
    "variant_handles": ["color:gray", "size:xs"]
  },
  "quantity": 1
}
```

### Clear Cart First

To replace cart contents with a new item:

```bash theme={null}
curl --request POST \
  --url 'https://api.firmly.work/api/v2/domains/staging.luma.gift/cart/line-items?flush_cart=true' \
  --header 'Content-Type: application/json' \
  --header 'x-firmly-authorization: YOUR_TOKEN' \
  --data '{
    "add_to_cart_ref": {
      "variant_id": "WT09-XS-White"
    },
    "quantity": 2
  }'
```

## Catalog Integration

The `add_to_cart_ref` object should be passed directly from catalog API responses:

```json Catalog API Response theme={null}
{
  "products": [
    {
      "product_id": "WS12",
      "variants": [
        {
          "add_to_cart_ref": {
            "variant_id": "WS12-XS-Orange",
            "variant_handles": ["color:orange", "size:xs"]
          }
        }
      ]
    }
  ]
}
```

```json Add to Cart Request theme={null}
{
  "add_to_cart_ref": {
    "variant_id": "blue-sofa-large",
    "variant_handles": ["color:blue", "size:large"]
  },
  "quantity": 1
}
```

```json theme={null}
{
  "line_items": [
    {
      "line_item_id": "new-item-12345",
      "platform_line_item_id": "0",
      "sku": "WS12-XS-Orange",
      "variant_description": "Color: Orange, Size: XS",
      "description": "Radiant Tee",
      "base_sku": "WS12",
      "quantity": 1,
      "price": {
        "currency": "USD",
        "value": 22.00,
        "number": 2200,
        "symbol": "$"
      },
      "line_price": {
        "currency": "USD",
        "value": 22.00,
        "number": 2200,
        "symbol": "$"
      },
      "msrp": {
        "currency": "USD",
        "value": 22.00,
        "number": 2200,
        "symbol": "$"
      },
      "image": {
        "url": "https://cdn.staging.luma.gift/product/radiant-tee-orange.jpg",
        "alt": "Radiant Tee in Orange",
        "type": "default"
      },
      "requires_shipping": true
    }
  ],
  "shipments": [
    {
      "shipment_id": "auto-generated-shipment-1",
      "line_item_ids": ["new-item-12345"],
      "fulfillment_type": {
        "id": "SHIP_TO_ADDRESS",
        "name": "Ship to Address",
        "description": "Standard shipping to your address"
      },
      "fulfillment_type_options": [
        {
          "id": "SHIP_TO_ADDRESS",
          "name": "Ship to Address", 
          "description": "Standard shipping to your address"
        },
        {
          "id": "SCHEDULED_DELIVERY",
          "name": "Scheduled Delivery",
          "description": "Delivery scheduled for a specific date"
        }
      ],
      "shipping_method_options": [
        {
          "id": "STANDARD_SHIPPING",
          "description": "Standard Shipping",
          "price": {
            "currency": "USD",
            "value": 50,
            "number": 5000,
            "symbol": "$"
          },
          "estimated_delivery": "5-7 business days"
        },
        {
          "id": "PREMIUM_SHIPPING",
          "description": "Premium Delivery",
          "price": {
            "currency": "USD",
            "value": 100,
            "number": 10000,
            "symbol": "$"
          },
          "estimated_delivery": "2-3 business days"
        }
      ],
      "shipping_method": {
        "id": "STANDARD_VENDOR_SHIPPING",
        "description": "Standard Shipping (O)",
        "price": {"value": 0, "currency": "USD", "number": 0, "symbol": "$"},
        "estimated_delivery": "Varies by vendor"
      },
      "notes": ""
    }
  ],
  "sub_total": {
    "currency": "USD",
    "value": 1299.99,
    "number": 129999,
    "symbol": "$"
  },
  "shipping_total": {
    "currency": "USD",
    "value": 0,
    "number": 0,
    "symbol": "$"
  },
  "tax_total": {
    "currency": "USD",
    "value": 0,
    "number": 0,
    "symbol": "$"
  },
  "fees": [
    {
      "description": "Recycle fee",
      "currency": "USD",
      "value": 2.00,
      "number": 200,
      "symbol": "$"
    }
  ],
  "fee_total": {
    "currency": "USD",
    "value": 2.00,
    "number": 200,
    "symbol": "$"
  },
  "total": {
    "currency": "USD",
    "value": 1301.99,
    "number": 130199,
    "symbol": "$"
  },
  "addons": {
    "offers": [
      {
        "addon_id": "protection-new-item-12345",
        "display": {
          "name": "Product Protection Plan"
        },
        "price": {
          "currency": "USD",
          "value": 0,
          "number": 0,
          "symbol": "$"
        },
        "scope": "ITEM",
        "coverage_mode": "PER_ITEM",
        "eligible_line_item_ids": [
          "new-item-12345"
        ],
        "child_offers": [
          {
            "addon_id": "A0-FURN-2y",
            "display": {
              "name": "2 Year Protection"
            },
            "scope": "INHERIT",
            "coverage_mode": "PER_ITEM",
            "eligible_line_item_ids": [
              "new-item-12345"
            ],
            "price": {
              "currency": "USD",
              "value": 149.99,
              "number": 14999,
              "symbol": "$"
            }
          },
          {
            "addon_id": "A0-FURN-3y",
            "display": {
              "name": "3 Year Protection"
            },
            "scope": "INHERIT",
            "coverage_mode": "PER_ITEM",
            "eligible_line_item_ids": [
              "new-item-12345"
            ],
            "price": {
              "currency": "USD",
              "value": 199.99,
              "number": 19999,
              "symbol": "$"
            }
          }
        ]
      }
    ],
    "selections": []
  },
  "addon_total": {
    "currency": "USD",
    "value": 0,
    "number": 0,
    "symbol": "$"
  },
  "schema_version": "2.0"
}
```

## Error Responses

> [accordion] 400 Bad Request
>
> Invalid request format or missing required parameters
>
> ```json theme={null}
> {
>   "code": 400,
>   "error": "ErrorInvalidInputBody",
>   "description": "Request body validation failed"
> }
> ```

> [accordion] 404 Not Found
>
> Product variant not found in catalog
>
> ```json theme={null}
> {
>   "code": 404,
>   "error": "ErrorProductNotFound",
>   "description": "Product variant was not found"
> }
> ```

> [accordion] 409 Conflict
>
> Product is not supported for cart operations
>
> ```json theme={null}
> {
>   "code": 400,
>   "error": "ErrorProductNotSupported",
>   "description": "Product is not supported for this operation"
> }
> ```

> [accordion] 422 Unprocessable Entity
>
> Various processing errors
>
> ```json theme={null}
> {
>   "code": 400,
>   "error": "ErrorNotEnoughStock",
>   "description": "Insufficient stock for requested quantity"
> }
> ```

## Automatic Shipment Assignment

> [!NOTE]
>
> When adding items to the cart, the system automatically:
>
> 1. **Groups compatible items**: Items with similar fulfillment requirements are grouped together
> 2. **Creates new shipments**: Items with different requirements get separate shipments
> 3. **Inherits options**: New shipments inherit available fulfillment and shipping options

### Example: Mixed Cart

Adding a large sofa (requires scheduled delivery) to a cart with small accessories (standard shipping) creates separate shipments:

```json theme={null}
{
  "shipments": [
    {
      "shipment_id": "furniture-shipment",
      "line_item_ids": ["hoodie-item"],
      "fulfillment_type": {"id": "SCHEDULED_DELIVERY"}
    },
    {
      "shipment_id": "accessories-shipment", 
      "line_item_ids": ["tee-item", "tank-item"],
      "fulfillment_type": {"id": "SHIP_TO_ADDRESS"}
    }
  ]
}
```

## Related Endpoints

* [Get Cart](https://firmly.mintlify.site/api-reference/cart-management/get-cart.md) - View current cart state
* [Update Line Item](https://firmly.mintlify.site/api-reference/cart-management/update-line-item.md) - Modify cart items
* [Clear Cart](https://firmly.mintlify.site/api-reference/cart-management/clear-cart.md) - Empty the cart


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