Cart API reference
The Cart API is used to interact with a cart during a customer's session. This guide shows how to use the Cart API to update cart line items, add cart attributes and notes, and generate shipping rates.
All Ajax API requests should use locale-aware URLs to give visitors a consistent experience.
For simplicity, the code examples in this guide don't always use a callback.
Anchor to POST /{locale}/cart/add.jsPOST /{locale}/cart/add. js
Use the POST /{locale}/cart/add.js
endpoint to add one or multiple variants to the cart.
In the following example, quantity
is the amount of the variant that you want to add and id
is the variant ID of that variant. You can add multiple variants to the cart by appending more objects in the items
array.
Below is a simplified POST
request using the fetch
API. The formData
object is built in JavaScript, so the Content-Type
should be set to application/json
in the headers
object. The response is the JSON of the line items associated with the added variants. If an item is already in the cart, then quantity
is equal to the new quantity for that item.
Example request
Response
Alternatively, you can use the FormData
constructor and target the desired add-to-cart form:
Anchor to ResponseResponse
The response for a successful POST
request is a JSON object of the line items associated with the added items.
If an added item was already in the cart, then the quantity
is equal to the new quantity for that cart line item. However, if the same items have differing prices, properties, or selling plans, then they'll be split into their own line items.
Changes in price are typically the result of automatic discounts or Shopify Scripts.
Example data
Response
Anchor to Error responsesError responses
If the specified quantity for an item exceeds the available stock (e.g., attempting to add 20 items when only 10 are available), the cart will instead add the maximum available quantity. The error returned in JSON format is as follows:
If the product is entirely sold out, then the following error is returned:
If the product is not sold out, but all of its stock is in the cart, then the following error is returned:
Anchor to Add line item propertiesAdd line item properties
You can add a variant to the cart with line item properties using the properties
property. Its value must be an object of key-value pairs.
Request data
Response
Anchor to Add a selling planAdd a selling plan
You can add a variant with a selling plan to the cart using the selling_plan
parameter. Its value must be the selling plan ID.
Request data
Response
Anchor to GET /{locale}/cart.jsGET /{locale}/cart. js
Use the GET /{locale}/cart.js
endpoint to get the cart as JSON.
All monetary properties are returned in the customer's presentment currency. To check the customer's presentment currency, you can use the currency
field in the response. To learn more about selling in multiple currencies, refer to Migrate your app to support multi-currency.
Anchor to ResponsesResponses
Anchor to JSON of a cartJSON of a cart
Anchor to JSON of an empty cartJSON of an empty cart
If you want to empty an existing cart, then use the /{locale}/cart/clear
endpoint.
Anchor to POST /{locale}/cart/update.jsPOST /{locale}/cart/update. js
Use the POST /{locale}/cart/update.js
endpoint to update the cart's line item quantities, note, or attributes. You can submit a serialized cart form, or submit separate updates to a cart's line items, note, or attributes.
Anchor to Update line item quantitiesUpdate line item quantities
To update line item quantities, you can make a POST
request with an updates
object. The updates
object must contain key-value pairs where the value is the desired quantity, and the key is one of the following:
- The line item's
variant_id
- The line item's
key
A cart can have multiple line items that share the same variant_id
. For example, when variants have different line item properties, or automatic discounts create variants at different prices. Because of this, it's recommended to use the line item key to ensure that you're only changing the intended line item.
The line item key is not persistent for the lifetime of a line item, it changes as characteristics of the line item change. This includes, but is not limited to, properties and discount applications.
If you use the variant ID, then the key can be either an integer or a string. However, if you use the line item key, then the key needs to be a string.
The following POST
request yields the same result:
The /{locale}/cart/update.js
endpoint adds new line items to the cart if the variant_id
provided doesn't match any line item already in the cart. However, if the variant_id
matches multiple line items, then the first matching line item is updated.
Use the change.js
endpoint when changing line items that are already in the cart, and the add.js
endpoint when adding new line items.
You can remove items from the cart by setting the quantity to 0:
You can also submit an array of numbers to /{locale}/cart/update.js
, provided the size of the array matches the number of line items in the cart. Each number in the array sets the quantity for the corresponding line item in the cart. For example, if you have 3 line items in the cart with quantities 1, 2, and 3, and you want to change those quantities to 3, 2, and 1, then you can use the following:
Anchor to Update the cart noteUpdate the cart note
To update the cart note, you can make a POST
request with a note
character string:
Anchor to Update cart attributesUpdate cart attributes
To update cart attributes, you can make a POST
request with an attributes
object. The attributes
object must contain key-value pairs where the key
is the name of the attribute you want to update, and the value is the attribute value:
The following POST
request yields the same result:
Anchor to Update discounts in the cartUpdate discounts in the cart
You can apply a discount to the cart, as shown in the following example:
You can also apply multiple discounts to the cart using a comma ,
separator:
To remove all discounts from the cart, use an empty string:
Anchor to ResponseResponse
The JSON of the cart.
Anchor to Error responsesError responses
If a variant ID is provided that either doesn't exist or isn't available in the online store channel, then the endpoint returns the following error:
The update.js
endpoint doesn't validate the quantity on variants that are already in the cart. This means that it's possible to add more inventory than is available.
Anchor to POST /{locale}/cart/change.jsPOST /{locale}/cart/change. js
Use the /{locale}/cart/change.js
endpoint to change the quantity
, properties
, and selling_plan
properties of a cart line item. Only items already in your cart can be changed, and you can change only one line item at a time.
Anchor to Identify the line item to changeIdentify the line item to change
The POST
data requires either an id
or line
property to identify the line item to be changed.
Anchor to The ,[object Object], propertyThe id
property
id
propertyThe value of the id
property can be one of the following:
- The line item's
variant_id
- The line item's
key
{
'id': 794864053,
'quantity': 3
}
{
'id': '794864053:83503fd282b94a4737d2c95bd95db0b8',
'quantity': 3
}
A cart can have multiple line items that share the same variant_id
. For example, when variants have different line item properties, or automatic discounts create variants at different prices. Because of this, it's recommended to use the line item key to ensure that you're only changing the intended line item.
If you use the variant ID, then the id
value can be either an integer or a string. However, if you use the line item key, then the id
value needs to be a string.
Anchor to The ,[object Object], propertyThe line
property
line
propertyA cart can have multiple line items that share the same variant_id
. For example, when variants have different line item properties, or automatic discounts create variants at different prices. To account for this, you can use the line
property when updating the cart.
The value of line
is the 1-based index position of the item in the cart.
Anchor to Update quantitiesUpdate quantities
The value of thequantity
property represents the new quantity you want for the line item. It must be an integer.
You can remove a line item by setting the quantity
to 0
:
Anchor to Update propertiesUpdate properties
The properties
property sets the line item properties. Its value must be an object of key-value pairs.
Whenever a POST
request includes properties
, it overwrites the entire properties
object. Any key-value pairs that were already in the properties
object are erased.
It's not possible to set a line item's properties
property to an empty object once a value is set. If you need to remove all line item properties, then you need to remove the entire line item.
You can visually hide line item properties at checkout by creating private properties. This technique might be an alternative to removing a line item when managing properties
.
Anchor to Update selling plansUpdate selling plans
The selling_plan
property sets the line item selling plan. Its value must be one of the following:
- The selling plan ID: To set a specific selling plan for the line item.
null
or an empty string: To remove any selling plan from the line item.
{
'line': 2,
'quantity': 2,
'selling_plan': 183638
}
{
'line': 2,
'quantity': 2,
'selling_plan': null
}
When specifying the selling_plan
property, consider the following:
- You can use only the
line
property for identifying the line item. - You should always specify the
quantity
property. If thequantity
isn't specified, then it's assumed to be 1.
Anchor to ResponseResponse
The JSON of the cart.
Anchor to Error responsesError responses
If the item that you're attempting to change isn't already in the cart, then /{locale}/cart/change.js
doesn't add it. Instead, it returns a 400
error:
Anchor to POST /{locale}/cart/clear.jsPOST /{locale}/cart/clear. js
Use the POST /{locale}/cart/clear.js
endpoint to set all quantities of all line items in the cart to zero.
Anchor to ResponseResponse
The JSON of an empty cart. This does not remove cart attributes or the cart note.
Anchor to Generate shipping ratesGenerate shipping rates
Use the POST /{locale}/cart/prepare_shipping_rates.json
and GET /{locale}/cart/async_shipping_rates.json
endpoints to generate shipping rates:
- The
POST /{locale}/cart/prepare_shipping_rates.json
endpoint initiates the process of calculating shipping rates for the cart given a destination. - The
GET /{locale}/cart/async_shipping_rates.json
endpoint returns the shipping rates results if the calculations have finished.
Anchor to Example ,[object Object], callExample prepare_shipping_rates
call
prepare_shipping_rates
callAnchor to ResponseResponse
null
Anchor to Example ,[object Object], callExample async_shipping_rates
call
async_shipping_rates
callIf you call async_shipping_rates
with the same parameters as prepare_shipping_rates
, then it checks whether Shopify has finished calculating the rates. If the shipping rates aren't ready, then the response is null
.
If the shipping rates are ready, the rates are returned:
Use the GET /{locale}/cart/shipping_rates.json
to get estimated shipping rates.
The recommended method to generate shipping rates is to use the POST /{locale}/cart/prepare_shipping_rates.json
and GET /{locale}/cart/async_shipping_rates.json
endpoints because it might take a while for shipping rates to be returned. The GET /{locale}/cart/shipping_rates.json
endpoint is subject to throttling.
Anchor to Private properties and attributesPrivate properties and attributes
Private line item properties and private cart attributes are used when you need to attach information to either cart line items or the entire cart, and you don't want the properties and attributes to be visible to the online store's visitors.
Both private properties and private cart attributes are visually hidden at checkout, but are visible on the Order details page in the Shopify admin.
If you hide private line item properties on the storefront, then you must modify your theme's codebase.
Anchor to Private line item propertiesPrivate line item properties
To make a line item property private, prepend an underscore (_
) to the key. For example, to add a variant to cart with a private _foo
property using the /{locale}/cart/add.js
endpoint:
The properties
property can have a mix of private and public line item properties:
Anchor to Hide properties in a themeHide properties in a theme
Private line item properties are available in the Liquid line_item.properties
object and Ajax API. To hide private properties on the storefront, you must modify the theme's codebase.
In the following example, all properties
items that begin with _
in Liquid are filtered out:
Liquid
Anchor to Private cart attributesPrivate cart attributes
To make a cart attribute private, append a double underscore (__
) to an attribute name.
Private cart attributes are not available in the Liquid cart.attributes
object or the Ajax API. This means there is no code modification required to hide them in theme files. Private cart attributes also do not affect the page rendering, which allows for more effective page caching.
Anchor to Bundled section renderingBundled section rendering
Bundled section rendering allows you to request the HTML markup for up to five sections you might want to update based on an initial call to the Cart API, within that same call.
Bundled section rendering is available for the following Cart API endpoints:
/{locale}/cart/add
/{locale}/cart/change
/{locale}/cart/clear
/{locale}/cart/update
Anchor to Request sectionsRequest sections
To request sections, you can include a sections
parameter in your API call data:
Example
The sections
parameter can be a comma-separated list or an array, similar to when using the Section Rendering API directly.
By default, sections are rendered in the context of the current page, based on the HTTP Referer header. However, you can specify any other page using the sections_url
parameter. The sections_url
must begin with a /
and can include query parameters like q
and page
.
Example
The HTML for the requested sections is included under the sections
key of the returned JSON. Each section can be identified by the same ID that was passed in the request.
Example
Anchor to Error responseError response
Sections are rendered after the data modifications from the request are completed. Because of this, rendering errors don't affect the response status of the API call. Sections that fail to render are returned as null
, so you should account for this possibility.
Passing invalid values for the sections
or sections_url
parameters, such as a sections_url
that doesn't begin with /
, causes the entire request to return an HTTP 400 Bad Request status. However, this doesn't mean that the rest of the request didn't succeed.