Using cart create in customer account extension ui

Hi there,

i am calling cartcreate mutation from my backend (after sending session token from customer account ui extension frontend)

it is giving 401 error. any idea what is wrong here? below is the code

const mutation = `mutation cartCreate($input: CartInput) {

        cartCreate(input: $input) {

          cart {

            id

            checkoutUrl

            totalQuantity

            cost {

              totalAmount {

                amount

                currencyCode

              }

            }

            lines(first: 10) {

              edges {

                node {

                  id

                  quantity

                  merchandise {

                    ... on ProductVariant {

                      id

                      title

                      product {

                        title

                      }

                    }

                  }

                }

              }

            }

          }

          userErrors {

            field

            message

          }

        }

      }

    \`;

const variables = {

input: {

lines: [

          {

quantity: 1,

merchandiseId: “gid://shopify/ProductVariant/50652074639680”

          }

        \]

      }

    };

const response = await fetch(url, {

method: ‘POST’,

headers: {

‘Content-Type’: ‘application/json’,

// ‘Authorization’: `Bearer ${storefrontAccessToken}`,

‘X-Shopify-Storefront-Access-Token’: storefrontAccessToken,

‘Shopify-Storefront-API-Version’: “2025-10”,

      },

body: JSON.stringify({

query: mutation,

variables: variables

      })

    });

Well 401 is Unauthorized, so I’d check you have the correct tokens etc.

hey @Luke thanks for quick response, token is indeed correct not sure why it is not working.

i tried session token from frontend as well as access token but no luck. also, cartcreate not working in shopify GraphiQL app. as well. any support will be really. appreciated

Are you trying to use the session token as an access token to query the storefront API? If so, that wouldn’t work, the session token isn’t meant for this, it’s meant for you to confirm the request to your backend came from Shopify.

You can create a cart directly from your extension using the Storefront API instead of going through your backend, which would mean you don’t have to deal with passing a token at all.

hey @Kenza_Iraki

thanks for the info. it did worked. cartcreate mutation creates cart and gives me checkout url which is fine but when i open cart page, it is empty. any solution for that?

Thanks in advance