shopify storefront api
Budget: €12 – €18 EUR
const createCartAndAddProduct = async () => {
try {
// Step 1: Create the cart
const createCartResponse = await axios.post(
`https://${shopifyDomain}/api/2023-10/graphql.json`,
{
query: `
mutation {
cartCreate(input: {}) {
cart {
id
}
userErrors {
field
message
}
}
}
`
},
{
headers: {
'Content-Type': 'application/json',
'X-Shopify-Storefront-Access-Token': storefrontAccessToken,
},
}
);
const { cart, userErrors } = createCartResponse.data.data.cartCreate;
if (userErrors && userErrors.length > 0) {
console.error("User Errors (Cart Creation):", userErrors);
return;
}
console.log("Cart created:", cart.id);
// Step 2: Add the product to the cart
const productId = "gid://shopify/ProductVariant/44707067461897";
const addToCartResponse = await axios.post(
`https://${shopifyDomain}/api/2023-10/graphql.json`,
{
query: `
mutation AddToCart($cartId: ID!, $variantId: ID!) {
cartLinesAdd(cartId: $cartId, lines: [{ quantity: 1, merchandiseId: $variantId}]) {
cart {
lines(first: 100) {
edges {
node {
id
quantity
merchandise {
... on ProductVariant {
product {
title
}
}
}
}
}
}
}
userErrors {
field
message
}
}
}
`,
variables: {
cartId: cart.id,
variantId: productId
}
},
{
headers: {
'Content-Type': 'application/json',
'X-Shopify-Storefront-Access-Token': storefrontAccessToken,
},
}
);
const { cart: updatedCart, userErrors: addToCartErrors } = addToCartResponse.data.data.cartLinesAdd;
if (addToCartErrors && addToCartErrors.length > 0) {
console.error("User Errors (Add To Cart):", addToCartErrors);
return;
}
console.log("Product added to cart:", updatedCart.lines.edges);
} catch (error) {
console.error('Error:', error.response ? error.response.data : error.message);
}
};
createCartAndAddProduct();
But i just added and had this response :
{
"product": {
"id": 8338361549065,
"title": "tester",
"body_html": "<strong>Good snowboard!</strong>",
"vendor": "Burton",
"product_type": "Snowboard",
"created_at": "2023-11-02T02:11:20+01:00",
"handle": "tester-1",
"updated_at": "2023-11-02T02:11:20+01:00",
"published_at": "2023-11-02T02:11:20+01:00",
"template_suffix": null,
"published_scope": "global",
"tags": "",
"status": "active",
"admin_graphql_api_id": "gid://shopify/Product/8338361549065",
"variants": [
{
"id": 44707067461897,
"product_id": 8338361549065,
"title": "Default Title",
"price": "399.99",
"sku": "",
"position": 1,
"inventory_policy": "deny",
"compare_at_price": null,
"fulfillment_service": "manual",
"inventory_management": null,
"option1": "Default Title",
"option2": null,
"option3": null,
"created_at": "2023-11-02T02:11:20+01:00",
"updated_at": "2023-11-02T02:11:20+01:00",
"taxable": true,
"barcode": null,
"grams": 0,
"image_id": null,
"weight": 0,
"weight_unit": "kg",
"inventory_item_id": 46517911978249,
"inventory_quantity": 0,
"old_inventory_quantity": 0,
"requires_shipping": true,
"admin_graphql_api_id": "gid://shopify/ProductVariant/44707067461897"
}
],
"options": [
{
"id": 10659962061065,
"product_id": 8338361549065,
"name": "Title",
"position": 1,
"values": [
"Default Title"
]
}
],
"images": [],
"image": null
}
}
but i get :
User Errors (Add To Cart): [
{
field: [ 'lines', '0', 'merchandiseId' ],
message: 'The merchandise with id gid://shopify/ProductVariant/44707067461897 does not exist.'
}
]
try {
// Step 1: Create the cart
const createCartResponse = await axios.post(
`https://${shopifyDomain}/api/2023-10/graphql.json`,
{
query: `
mutation {
cartCreate(input: {}) {
cart {
id
}
userErrors {
field
message
}
}
}
`
},
{
headers: {
'Content-Type': 'application/json',
'X-Shopify-Storefront-Access-Token': storefrontAccessToken,
},
}
);
const { cart, userErrors } = createCartResponse.data.data.cartCreate;
if (userErrors && userErrors.length > 0) {
console.error("User Errors (Cart Creation):", userErrors);
return;
}
console.log("Cart created:", cart.id);
// Step 2: Add the product to the cart
const productId = "gid://shopify/ProductVariant/44707067461897";
const addToCartResponse = await axios.post(
`https://${shopifyDomain}/api/2023-10/graphql.json`,
{
query: `
mutation AddToCart($cartId: ID!, $variantId: ID!) {
cartLinesAdd(cartId: $cartId, lines: [{ quantity: 1, merchandiseId: $variantId}]) {
cart {
lines(first: 100) {
edges {
node {
id
quantity
merchandise {
... on ProductVariant {
product {
title
}
}
}
}
}
}
}
userErrors {
field
message
}
}
}
`,
variables: {
cartId: cart.id,
variantId: productId
}
},
{
headers: {
'Content-Type': 'application/json',
'X-Shopify-Storefront-Access-Token': storefrontAccessToken,
},
}
);
const { cart: updatedCart, userErrors: addToCartErrors } = addToCartResponse.data.data.cartLinesAdd;
if (addToCartErrors && addToCartErrors.length > 0) {
console.error("User Errors (Add To Cart):", addToCartErrors);
return;
}
console.log("Product added to cart:", updatedCart.lines.edges);
} catch (error) {
console.error('Error:', error.response ? error.response.data : error.message);
}
};
createCartAndAddProduct();
But i just added and had this response :
{
"product": {
"id": 8338361549065,
"title": "tester",
"body_html": "<strong>Good snowboard!</strong>",
"vendor": "Burton",
"product_type": "Snowboard",
"created_at": "2023-11-02T02:11:20+01:00",
"handle": "tester-1",
"updated_at": "2023-11-02T02:11:20+01:00",
"published_at": "2023-11-02T02:11:20+01:00",
"template_suffix": null,
"published_scope": "global",
"tags": "",
"status": "active",
"admin_graphql_api_id": "gid://shopify/Product/8338361549065",
"variants": [
{
"id": 44707067461897,
"product_id": 8338361549065,
"title": "Default Title",
"price": "399.99",
"sku": "",
"position": 1,
"inventory_policy": "deny",
"compare_at_price": null,
"fulfillment_service": "manual",
"inventory_management": null,
"option1": "Default Title",
"option2": null,
"option3": null,
"created_at": "2023-11-02T02:11:20+01:00",
"updated_at": "2023-11-02T02:11:20+01:00",
"taxable": true,
"barcode": null,
"grams": 0,
"image_id": null,
"weight": 0,
"weight_unit": "kg",
"inventory_item_id": 46517911978249,
"inventory_quantity": 0,
"old_inventory_quantity": 0,
"requires_shipping": true,
"admin_graphql_api_id": "gid://shopify/ProductVariant/44707067461897"
}
],
"options": [
{
"id": 10659962061065,
"product_id": 8338361549065,
"name": "Title",
"position": 1,
"values": [
"Default Title"
]
}
],
"images": [],
"image": null
}
}
but i get :
User Errors (Add To Cart): [
{
field: [ 'lines', '0', 'merchandiseId' ],
message: 'The merchandise with id gid://shopify/ProductVariant/44707067461897 does not exist.'
}
]