Problems Adding Shopify Line Items To Cart With JS Buy SDK

- 1 answer

Having some problems with the Shopify js-buy-SDK.

I have been able to make a cart, and I have also tried fetching products, etc, and it works. But when I try to add a line item to the cart, the updated cart array, from addLineItems, returns empty. I know the variantId is correct, because if I change it I get an error.

Full code:

    <script src=""></script>
    <script src=""></script>
    <script type="text/javascript">


    const client = ShopifyBuy.buildClient({
          domain: '',
          storefrontAccessToken: 'xxxxxxx',
          appId: '6'
    // Create an empty checkout
    client.checkout.create().then((checkout) => {
       // Do something with the checkout
       x(, client);



function x(check, client) {
        const itemToAdd = [
            { variantId : 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0LzYyMDExMDQxMzg4NA==', quantity : 12 }

        // Add an item to the checkout
        client.checkout.addLineItems(check, itemToAdd).then((checkout) => {
            console.log(checkout.lineItems); // THIS RETURNS AN EMPTY ARRAY



Are you using the productID or the variantID of the item you're trying to add?

In my experience, if an ID ends with == it's a productID.

Assuming the item you're trying to add only has one variant, try

const productId = 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0LzYyMDExMDQxMzg4NA==';

.then((product) => {
    const variantId = product.variants[0].id;

If you're seeing an ID-like string and not undefined in your console then that is the correct value for your variantID.