---
title: Create and query nested cart lines
description: Learn how to create nested cart lines using our APIs.
source_url:
html: >-
https://shopify.dev/docs/apps/build/product-merchandising/nested-cart-lines/create-nested-cart-lines
md: >-
https://shopify.dev/docs/apps/build/product-merchandising/nested-cart-lines/create-nested-cart-lines.md
---
# Create and query nested cart lines
### Creating nested cart lines
You can add a nested cart line from a variety of Shopify APIs:
* [Cart Ajax API](https://shopify.dev/docs/api/ajax/reference/cart)
* [Checkout UI Extension API](https://shopify.dev/docs/api/checkout-ui-extensions/latest/apis/cart-lines)
* [Storefront API](https://shopify.dev/docs/api/storefront/unstable/input-objects/CartLineInput)
### Query for nested cart lines
Nested cart lines are represented across the following APIs:
* [Cart Ajax API](https://shopify.dev/docs/api/ajax/reference/cart)
* [Checkout UI Extension API](https://shopify.dev/docs/api/checkout-ui-extensions/latest/apis/cart-lines)
* [Function API](https://shopify.dev/docs/api/functions/latest#function-anatomy)
* [Liquid API](https://shopify.dev/docs/api/liquid/objects/line_item)
* [Order API](https://shopify.dev/docs/api/admin-graphql/unstable/objects/LineItemGroup)
* [Storefront API](https://shopify.dev/docs/api/storefront/unstable/objects/CartLine)
* [Webhook API](https://shopify.dev/docs/api/webhooks)
***
## Create nested cart lines
To create a nested cart line, specify a `parent` in the API input.
### Storefront API
Use the `parent` field in the cart mutation input. You can reference the parent by `lineId` (for existing line items in the cart) or `merchandiseId` (for adding both parent and nested cart lines in the same request).
* Only one of `lineId` or `merchandiseId` should be used per line.
* The response will return the nested cart line with a reference to its parent cart line under the `parentRelationship` object.
Example: Add TV and attach Warranty as an add-on
## POST https://{shop}.myshopify.com/api/{api\_version}/graphql.json
## GraphQL mutation
```graphql
mutation cartLinesAdd($cartId: ID!, $lines: [CartLineInput!]!) {
cartLinesAdd(cartId: $cartId, lines: $lines) {
cart {
lines(first: 10) {
edges {
node {
id
merchandise {
... on ProductVariant {
id
title
}
}
... on CartLine {
parentRelationship {
parent {
id
}
}
instructions {
canRemove
canUpdateQuantity
}
}
}
}
}
}
}
}
```
## Variables
```json
{
"cartId": "gid://shopify/Cart/1",
"lines": [
{
"merchandiseId": "gid://shopify/ProductVariant/1",
"quantity": 1
},
{
"merchandiseId": "gid://shopify/ProductVariant/2",
"quantity": 1,
"parent": {
"merchandiseId": "gid://shopify/ProductVariant/1"
}
}
]
}
```
## JSON response
```json
{
"data": {
"cartLinesAdd": {
"cart": {
"lines": {
"edges": [
{
"node": {
"id": "gid://shopify/CartLine/123",
"merchandise": {
"id": "gid://shopify/ProductVariant/1",
"title": "TV"
},
"parentRelationship": null,
"instructions": {
"canRemove": true,
"canUpdateQuantity": true
}
}
},
{
"node": {
"id": "gid://shopify/CartLine/456",
"merchandise": {
"id": "gid://shopify/ProductVariant/2",
"title": "1 year Warranty"
},
"parentRelationship": {
"parent": {
"id": "gid://shopify/CartLine/123"
}
},
"instructions": {
"canRemove": true,
"canUpdateQuantity": true
}
}
}
]
}
}
}
}
}
```
### Cart AJAX API
You can reference the parent by specifying a `parent_line_key` (for existing line items in the cart) or `parent_id` (for adding both parent and nested cart lines in the same request) in the API input.
**Notes:**
* Only one of `parent_line_key` or `parent_id` should be used per line.
* The response will return the nested cart line with a reference to its parent cart line under the `parent_relationship` object.
Example: Add TV and attach a warranty as an add-on.
## POST https://{shop}.myshopify.com/cart/add.js
## Variables
```json
[
{
"id": 1,
"quantity": 1
},
{
"id": 2,
"quantity": 1,
"parent_id": 1
}
]
```
## JSON response
```json
{
"items": [
{
"id": 1,
"properties": {},
"quantity": 1,
"variant_id": 1,
"key": "1:12345",
"title": "TV",
"product_title": "TV",
"variant_title": null,
"handle": "tv",
"requires_shipping": true
},
{
"id": 2,
"properties": {},
"quantity": 1,
"variant_id": 2,
"key": "2:56789",
"title": "Leash Warranty - 1 year",
"product_title": "Leash Warranty",
"variant_title": "1 year",
"parent_relationship": {
"parent_key": "1:12345"
},
"instructions": {
"can_remove": true,
"can_update_quantity": true
},
"handle": "tv-warranty",
"requires_shipping": true
}
]
}
```
### Checkout UI Extension
Example: Add a warranty as an add-on to a product in the cart
## Preact
```typescript
import {render} from 'preact';
import {useCartLineTarget} from '@shopify/ui-extensions/checkout/preact';
export default function extension() {
render(, document.body);
}
function Extension() {
const cartLines = useCartLines();
const lineId = cartLines[0]?.id;
const warrantyVariantId = "gid://shopify/ProductVariant/2";
const handleNestWarranty = async () => {
try {
const result = await shopify.applyCartLinesChange({
type: 'addCartLine',
merchandiseId: warrantyVariantId,
quantity: 1,
parent: {
lineId,
}
});
if (result.type === 'error') {
throw new Error(result.message);
}
} catch (error) {
throw new Error(error)
}
return (
Nest line
);
}
```
***
## Next steps
* Learn how to [build an add-ons app](https://shopify.dev/docs/apps/build/product-merchandising/nested-cart-lines/tutorial)
***