The Ajax Cart API lets a theme read and change the storefront cart with JavaScript, without reloading the page. All endpoints work on the cart of the current visitor session.

Money values: prices are returned as integers in cents when the shop runs in Shopify compatibility mode. Without compatibility mode, decimal amounts can be returned as JSON strings. Use the money filters in Liquid, or format the values in JavaScript accordingly.

Endpoint Method Purpose
/cart.js GET, POST Read the cart
/cart/add.js GET, POST Add one or more variants
/cart/update.js POST Change quantities, note and attributes
/cart/change.js GET, POST Change the quantity of one line item
/cart/clear.js GET, POST Empty the cart
/shipping_rates.json GET, POST Calculate shipping rates for a country

POST /cart/add.js

Adds a variant to the cart. Send id (the variant ID) and quantity. A missing quantity, or a quantity below 1, is treated as 1.

fetch('/cart/add.js', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ id: 36110175633573, quantity: 2 })
})
  .then(response => response.json())
  .then(lineItem => console.log(lineItem));

The response of a single add is the added line item. If the same variant with the same options and price is already in the cart, the quantities are merged, and quantity in the response is the new total quantity of that line item.

To add several variants in one request, send an items array. The response then contains an items array with the added line items:

{
  "items": [
    { "id": 36110175633573, "quantity": 1 },
    { "id": 36110175666341, "quantity": 2 }
  ]
}

Items are processed one after another. The request is not atomic: an item that cannot be added does not stop the other items.

Add line item properties

Line item properties store additional information on a line item, for example an engraving text. Empty property values are dropped.

In a batch request, send properties inside each item:

{
  "items": [
    { "id": 36110175633573, "quantity": 1, "properties": { "Engraving": "Max" } }
  ]
}

In a single request, send the properties inside a product object:

{
  "id": 36110175633573,
  "quantity": 1,
  "product": { "properties": { "Engraving": "Max" } }
}

Line item JSON

A line item in the Ajax API has the following keys:

{
  "id": 36110175633573,
  "variant_id": 36110175633573,
  "product_id": 4511223344,
  "key": "4511223344:2f1e0c9ab8d4e5f60718293a4b5c6d7e8f901234",
  "title": "Rain coat - Red / M",
  "product_title": "Rain coat",
  "variant_title": "Red / M",
  "product_description": "<p>Waterproof rain coat.</p>",
  "product_type": "Coats",
  "vendor": "Example Brand",
  "handle": "rain-coat",
  "url": "/products/rain-coat",
  "sku": "RC-RED-M",
  "grams": 450,
  "quantity": 2,
  "price": 12900,
  "original_price": 12900,
  "final_price": 12900,
  "line_price": 25800,
  "original_line_price": 25800,
  "final_line_price": 25800,
  "line_level_total_discount": 0,
  "discounts": [],
  "discount_allocations": [],
  "line_level_discount_allocations": [],
  "properties": { "Engraving": "Max" },
  "variant_options": ["Red", "M"],
  "options_with_values": [
    { "name": "Color", "value": "Red" },
    { "name": "Size", "value": "M" }
  ],
  "image": "https://cdn.example.com/products/rain-coat.jpg",
  "featured_image": {
    "id": 9988776655,
    "position": 1,
    "src": "https://cdn.example.com/products/rain-coat.jpg",
    "variant_ids": [36110175633573]
  },
  "requires_shipping": true,
  "gift_card": false,
  "product_has_only_default_variant": false
}

Notes:

  • key identifies the line item in the cart (product ID, a colon and a checksum). Use it with /cart/change.js and /cart/update.js.
  • price and final_price are aliases, as are line_price and final_line_price.
  • The discount lists (discounts, discount_allocations, line_level_discount_allocations) are currently always empty. gift_card is always false.
  • Subscriptions via selling plans are not supported. A selling_plan parameter is ignored.

Errors when adding

/cart/add.js does not return a separate error schema. Keep the following behavior in mind:

  • For variants with stock tracking, the quantity can be limited to the available stock.
  • An unavailable variant that is not yet in the cart is skipped.
  • An unknown variant ID returns HTTP 200 with an empty object {}.

Check the response, or read /cart.js afterwards, to see what was actually added.

GET /cart.js

Returns the cart as JSON.

{
  "note": "Please gift wrap",
  "total_price": 25800,
  "subtotal_price": 25800,
  "items_subtotal_price": 25800,
  "total_weight": 900,
  "item_count": 1,
  "items": [ ],
  "requires_shipping": true,
  "shipping_address": {},
  "cart_level_discount_applications": []
}
  • items contains line items.
  • item_count is the number of line items.
  • subtotal_price and items_subtotal_price have the same value.
  • Cart attributes, the currency and a cart token are not part of this response. Read cart attributes in Liquid with cart.attributes.

POST /cart/update.js

Changes several line items, the cart note or cart attributes in one request, and returns the cart JSON.

Update line items

Send updates as an object. The key can be the line item key, the product ID or the product access token. The value is the new quantity:

{ "updates": { "4511223344:2f1e0c9ab8d4e5f60718293a4b5c6d7e8f901234": 3 } }

You can also send updates as an array of quantities in the order of the line items in the cart.

  • A quantity of 0 (or an empty string) removes the line item.
  • Missing or invalid values are ignored.
  • Unknown IDs are skipped. update.js does not add new line items; use /cart/add.js for that.
  • Stock limits apply.
  • If a product ID matches several line items, the first matching line item is changed. Use the line item key to be precise.

Update cart note

{ "note": "Please gift wrap" }

An empty string "" clears the note. If note is missing or null, the note stays unchanged.

Update cart attributes

{ "attributes": { "Delivery date": "2026-12-24" } }

The attributes you send are merged into the existing cart attributes (string keys and string values). Sending an empty object does not remove existing attributes. The JSON response does not contain the attributes; read them in Liquid with cart.attributes.

POST /cart/change.js

Changes the quantity of one line item and returns the cart JSON.

Identify the line item with id (line item key, product ID or product access token) or with line (1-based position in the cart). If both are sent, id wins.

{ "id": "4511223344:2f1e0c9ab8d4e5f60718293a4b5c6d7e8f901234", "quantity": 1 }
{ "line": 2, "quantity": 0 }
  • quantity: 0 (or an empty string) removes the line item.
  • Negative, fractional or otherwise invalid quantities are ignored.
  • Stock limits apply.
  • If the line item is not found, the response is HTTP 200 with the unchanged cart.
  • Positions start at 1: line: 1 is the first line item in the cart.
  • properties cannot be changed with change.js. To change properties, remove the line item and add it again with the new properties.
  • In addition, change.js accepts a shipping_address for the cart.

POST /cart/clear.js

Removes all line items, clears the note and the cart attributes, and resets payment method and coupon.

The response of /cart/clear.js is the internal cart model and differs from the /cart.js format. Call /cart.js (or /cart/clear.json, which returns the regular cart JSON) if you need the cart in the usual format afterwards.

Shipping rates

/shipping_rates.json calculates the shipping rates for the current cart synchronously.

/shipping_rates.json?shipping_address[country]=Germany

Only the country is evaluated. Use the English country name, for example Germany. Postal code and region are not taken into account. If the country is not recognized, the previously set destination country of the cart is kept.

{
  "shipping_rates": [
    { "name": "Standard shipping", "price": "4.90", "delivery_date": null, "source": "versacommerce" },
    { "name": "Express", "price": "12.90", "delivery_date": null, "source": "versacommerce" }
  ]
}

If the calculation fails, the response is HTTP 200 with:

{ "error": ["An unknown error occurred."] }

The endpoints /cart/prepare_shipping_rates.json, /cart/async_shipping_rates.json and /cart/shipping_rates.json do not exist in VersaCommerce.

Private properties and attributes

VersaCommerce does not hide line item properties or cart attributes based on their name. A property such as _internal_id (leading underscore) or an attribute such as __source (double underscore) is stored and output like any other value, in Liquid and in the Ajax API.

If a theme should not show such values, filter them in Liquid, for example:

{% for property in line_item.properties %}
  {% assign first_character = property.first | slice: 0 %}
  {% unless first_character == '_' %}
    {{ property.first }}: {{ property.last }}
  {% endunless %}
{% endfor %}

Bundled section rendering

When you change the cart, you can have updated sections rendered in the same request. This saves a second request for the cart drawer, the cart icon or similar areas.

Bundled section rendering works with /cart/add.js, /cart/change.js, /cart/change.json and /cart/clear.js. /cart/update.js returns the cart without sections.

Request sections

Add the parameter sections with up to five section IDs, as a comma-separated string or as an array:

{
  "id": 36110175633573,
  "quantity": 1,
  "sections": "cart-icon-bubble,cart-drawer"
}

The sections are rendered after the cart change, in the context of the cart template. The response gets an additional sections object. Its keys are the requested section IDs, its values the rendered HTML:

{
  "id": 36110175633573,
  "quantity": 1,
  "sections": {
    "cart-icon-bubble": "<div id=\"shopify-section-cart-icon-bubble\" class=\"shopify-section\">...</div>",
    "cart-drawer": "<div id=\"shopify-section-cart-drawer\" class=\"shopify-section\">...</div>"
  }
}

The wrapper markup (shopify-section-<id>, class shopify-section) is described in the Section Rendering API. A sections_url parameter is not evaluated; the rendering context is always the cart template.

Error response

There is no separate validation of the requested section IDs and no HTTP 400 for invalid IDs. Check in your JavaScript whether a requested section is present in the sections object and contains HTML before you replace the content on the page.