Website Building » Shopify » How Do I Use Cart API on Shopify?

How Do I Use Cart API on Shopify?

Last updated on October 2, 2022 @ 2:22 am

The Shopify Cart API allows you to add, update, and delete items in a shop’s cart. It also allows you to retrieve information about the cart, such as the total number of items and the total price of the items.

In order to use the Cart API, you will need to create a Shopify account and then create a private app. Once you have done this, you will be able to get an API key and API secret from your Shopify account. With these, you will be able to make authenticated requests to the Cart API.

The first thing you will need to do is add an item to the cart.

To do this, you will need to make a POST request to the /cart/add.json endpoint. In the request body, you will need to include the following parameters:

PRO TIP: If you are not a Shopify Partner or staff member, do not attempt to use the Shopify Cart API. Unauthorised use of this API may result in your Shopify account being suspended.
  • line_item[quantity]: The number of items to add to the cart
  • line_item[variant_id]: The id of the variant of the product that you want to add to the cart

You can also optionally include the line_item[properties] parameter if you want to add any custom properties to the line item. Just like with adding items, updating items in a cart is done with a POST request, this time to the /cart/update. The request body for this endpoint is similar to that of the /cart/add.json endpoint, except that it also includes an id parameter for specifying which line item in the cart you want to update.

If you want to delete an item from a cart, you can do so with a DELETE request to the /cart/delete. In the request body, you will need to include the following parameters:

  • [line_item_id]: The id of the line item that you want to delete from the cart.

How Do I Use Cart API on Shopify?

In order use Shopify’s Cart API, first create a Shopify account and then create a private app within that account. Doing so will give you access authenticated requests including an API key and API secret.

With those credentials, make a POST request either /cart/add.json or /cart/update.json endpoint: including in body either line_item[quantity] and line_item[variant_id] (for adding an item) or id (for updating), respectively. To delete an item from a cart, include in body [line_item_id] in DELETE request sent /cart/delete.json .

Dale Leydon

Dale Leydon

Sysadmin turned Javascript developer. Owner of 20+ apps graveyard, and a couple of successful ones.