JavaScript SDK API

The JS API allows you to retrieve relevant information about the current Snipcart session and apply certain operations to the cart.

The JS API exposes multiple services:

All async methods return promises and throw an SDKError in case of failure. If you are not familiar with the concept of promises, consider reading this introduction.

Cart

update

The update method updates the current cart's data with the provided payload argument. The payload is an object of type UpdateCartPayload. Updates can be partial, i.e. they can contain only a subset of the fields defined in the UpdateCartPayload interface.

try {
    await Snipcart.api.cart.update({
        email: 'john.doe@example.com',
        billingAddress:{
            name: 'John Doe'
        }
    });
} catch (error) {
    console.log(error);
}

applyDiscount

The applyDiscount method applies a discount to the cart. It takes a discount code as an argument and returns a promise of type DiscountAppliedResponse.

try {
    await Snipcart.api.cart.applyDiscount('{discountCode}');
} catch (error) {
    console.log(error);
}

removeDiscount

The removeDiscount method removes a discount from the cart. It takes the discount code as an argument and returns a promise of type DiscountRemovedResponse.

try {
    await Snipcart.api.cart.removeDiscount('{discountCode}');
} catch (error) {
    console.log(error);
}

fetchShippingRates

The fetchShippingRates method retrieves a list of available shipping rates for the cart and returns a promise of type ShippingRatesResponse.

try {
    const response = await Snipcart.api.cart.fetchShippingRates();
} catch (error) {
    console.log(error);
}

setShippingInformation

The setShippingInformation method provides the shipping information for the cart. It takes an argument of type ShippingInformation and returns a promise.

try {
    await Snipcart.api.cart.setShippingInformation({
        method: '{shippingMethod}',
        cost: 0,
    });
} catch (error) {
    console.log(error)
}

setPaymentDetails

The setPaymentDetails method provides the payment method and associated details for the cart. It takes an argument of type PaymentDetails and returns a promise.

try {
    await Snipcart.api.cart.setPaymentDetails({
        method: 'test-card',
        details: {
            cardNumber: '4242424242424242',
            cardExpiration: '2020/18',
            cardCvc: '123',
        }
    });
} catch (error) {
    console.log(error)
}

selectPaymentMethod

The selectPaymentMethod selects the payment method used for the payment. It takes the payment method's id as an argument and returns a promise.

try {
    await this.snipcart.api.cart.selectPaymentMethod('card');
} catch (error) {
    console.log(error)
}

initializePaymentSession

The initializePaymentSession method initializes a new payment session and returns a promise of type PaymentSessionResponse. When the payment method flow requires a redirection, the redirect URL is passed as an argument.

try {
    await Snipcart.api.cart.initializePaymentSession();
} catch (error) {
    console.log(error)
}

confirm

The confirm method confirms the cart, processes the payment and returns a promise of type CartConfirmResponse.

try {
    const response = await Snipcart.api.cart.confirm();
} catch (error) {
    console.log(error);
}

Items

add

The add method adds an item to the cart. It can take a ProductDefinition or an array of ProductDefinition as an argument and returns a promise of type CartItemAddedResponse.

try {
    await Snipcart.api.cart.items.add({
        id: 'PRODUCT_ID',
        name: 'Product 1',
        price: 1.11,
        url: '/',
        quantity: 1,
    });
} catch (error) {
    console.log(error)
}

remove

The remove method removes an item from the cart. It takes the item's unique ID as a parameter and returns a promise of type CartItemRemovedResponse.

try {
    await Snipcart.api.cart.items.remove('{itemId}');
} catch (error) {
    console.log(error)
}

update

The update method updates an item in the cart. It takes an argument of type CartItem and returns a promise of type CartItemUpdatedResponse.

try {
    await Snipcart.api.cart.items.update();
} catch (error) {
    console.log(error)
}

Customer

fetchOrder

The fetchOrder method retrieves a customer's order. It takes the order's token as a parameter and returns a promise of type CartResponse.

try {
    const response = await Snipcart.api.customer.fetchOrder('{orderToken}');
} catch (error) {
    console.log(error)
}