Title: How to append products to a cart using the Shopify API?

dotkonnek
Shopify Partner
9 0 0

I have the cart token, and I'm trying to append products to the cart using the Shopify API. However, when making the following cURL request, I'm getting a "Bad Request" error:

curl --location 'https://dev-dotkonnekt.myshopify.com/admin/api/2023-04/carts/b6822c166dd8d044/items.json' \
--header 'X-Shopify-Access-Token: token' \
--header 'Content-Type: application/json' \
--data '{
  "variant_id": 41182214520876,
  "quantity": 1
}'

I'm using the correct cart token and the variant ID. Can someone please suggest what might be causing this error? Additionally, if there are any alternative APIs or methods available to achieve the same result, I would greatly appreciate any suggestions.

Thank you in advance for your help!

 

Reply 1 (1)

Sector7HQ
Shopify Partner
1 0 0

Hello

Have you tried using the StorefrontAPI?
The Admin API does not support operations on carts; it is meant more for managing products, collections, orders, and other back-office tasks. Cart management (i.e., adding, updating, and removing items from the cart) is a front-end operation and is typically handled by the Storefront API.

curl -X POST https://dev-dotkonnekt.myshopify.com/api/2023-04/graphql.json \
-H "Content-Type: application/json" \
-H "X-Shopify-Storefront-Access-Token: token" \
-d '{
  "query": "mutation { cartAdd(input: { lineItems: [{ variantId: \"base64-encoded-ID-of-your-product-variant-here", quantity: 1 }] }, id: \"cart-id-here\") { cart { id } } }"
}'


Let me know if this works. Feel free to reach out to us at partnerships@sector7hq.co if you run into issues.