cartAddItem
Overview
Add a product/variant to the shopper's cart. Pass the required payload via AddToCartInput (product, variant, quantity, session details, etc.).
- Type: GraphQL Mutation
- Endpoint: https://subdomain.com
- Authentication:
Authorization: Bearer <TOKEN>(if needed)
Mutation
mutation AddToCart($data: AddToCartInput!) {
addToCart(data: $data) {
data {
coupon
}
success
message
}
}
Variables
Adjust the payload according to your backend schema. Example:
{
"data": {
"productId": "<PRODUCT_ID>",
"variantId": "<VARIANT_ID>",
"quantity": 1,
"sessionId": "<SESSION_ID>"
}
}
Examples
- cURL
- JavaScript (fetch)
curl -X POST \
https://subdomain.com\
-H "Content-Type: application/json" \
-H "Authorization: Bearer <TOKEN>" \
--data-binary @- << 'EOF'
{
"query": "mutation AddToCart($data: AddToCartInput!) { addToCart(data: $data) { success message data { coupon } } }",
"variables": {
"data": {
"productId": "<PRODUCT_ID>",
"variantId": "<VARIANT_ID>",
"quantity": 1,
"sessionId": "<SESSION_ID>"
}
}
}
EOF
const res = await fetch('https://subdomain.com', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + token,
},
body: JSON.stringify({
query: `
mutation AddToCart($data: AddToCartInput!) {
addToCart(data: $data) {
success
message
data { coupon }
}
}
`,
variables: {
data: {
productId: '<PRODUCT_ID>',
variantId: '<VARIANT_ID>',
quantity: 1,
sessionId: '<SESSION_ID>',
},
},
}),
});
const json = await res.json();
Notes
- Align the input payload with your backend's
AddToCartInputdefinition (e.g., lineItems array, device info, coupon codes). - Handle
success/messageto show feedback to the shopper.