Public API

Please note that this API is still under active development, we'll be adding new methods so we suggest you to keep an eye on this page to be updated with new things!

The public API is the new way to access Snipcart data. It allows you to retrieve some revelant information about the current Snipcart's session.

All methods of the public API can be access through Snipcart.api object.

Make sure that you always call these methods when Snipcart is fully loaded, we suggest you to subscribe to the cart.ready event and do the operations needed in the callback of this event.

Snipcart.subscribe('cart.ready', function() {
  var count = Snipcart.api.getItemsCount();
});

getItemsCount

This method allows you to retrieve the current cart items count.

Usage

var count = Snipcart.api.getItemsCount();

It will return an integer representing the total number of items in the cart.


getItems

This method allows you to retrieve the list of items in the cart.

Usage

var items = Snipcart.api.getItems();

The method will return an array containing all items in the cart.

[{
  "price": 449,
  "quantity": 2,
  "id": "SMARTPHONE",
  "taxable": true,
  "shippable": true,
  "uniqueId": "91efc227-876c-4c27-a8ce-de285066fb89",
  "customFields": [{
    "name": "Memory size",
    "options": "16GB|32GB[+50.00]",
    "type": "dropdown",
    "uniqueId": "91efc227-876c-4c27-a8ce-de285066fb89",
    "value": "32GB",
    "operation": 50,
    "required": false
  }, ...],
  "name": "Smartphone",
  "description": ""
}, {
  "price": 10,
  "quantity": 1,
  "id": "42",
  "taxable": true,
  "shippable": true,
  "uniqueId": "0fc4ceb2-3e1e-4be5-9b26-a3545796b033",
  "customFields": [],
  "name": "Bacon",
  "description": ""
},
...]

addItems

This method allows you to add multiple items at the same time. Do not use item.add method if you need to add more than one item at at time.

Usage

The method takes a single parameter which is an array of cart items.

Snipcart.api.addItems([{
  "id": "CS",
  "name": "Custom shirt",
  "price": 20.00,
  "url": "/",
  "customFields": [{
    "name": "Size",
    "options": "Small|Medium|Large",
    "value": "Medium"
  }]
}, {
  "id": "MUG",
  "name": "Super Mug",
  "price": 10.00,
  "url": "/"
}]);

getUser

This method can be used to get the current logged in user information. This method will return a value only if you don't have Guests only enabled.

Usage

var user = Snipcart.api.getUser();

It will return a JSON object containing using attributes.


showCart

This method will open the cart if it was closed.

Usage

Snipcart.api.showCart();

closeCart

This method will close the cart modal.

Usage

Snipcart.api.closeCart();

setCurrency

This method is used to set the current session currency. If a cart is already started in another currency, it will reset when this method is called. You must supply us a ISO4217 currency code. By default, the currency will be the one defined in the dashboard under the Regional settings menu.

Usage

Snipcart.api.setCurrency('usd');

getCurrentCurrency

This method will return the current session currency. We will return a ISO4217 currency code.

Usage

var currency = Snipcart.api.getCurrentCurrency();