Reputation: 73
I have exported a CSV file from my Shopify store. My goal is to automatically add any new products added to the CSV file to my store. To achieve this, I have installed the csv-parser and shopify-api-node packages in my Node.js environment. Additionally, I have created an app with the same name as my store in the 'Apps and sales channels' section of my Shopify store. I have generated the admin API key and API secret for this app. Below is the code I am trying to run:
const express = require("express");
const dotenv = require("dotenv");
const fs = require("fs");
const parse = require("csv-parser");
const app = express();
// ===================
const Shopify = require("shopify-api-node");
// ===================
// middleware
dotenv.config();
// codes will be here
// ===================
const shopify = new Shopify({
shopName: process.env.SHOP_NAME,
apiKey: process.env.API_KEY,
password: process.env.SECRET_KEY,
});
fs.createReadStream("products.csv")
.pipe(parse({ columns: true, delimiter: "," }))
.on("data", (product) => {
// Add the new product to Shopify
shopify.product
.create(product)
.then((createdProduct) => {
console.log("Product created:", createdProduct.title);
})
.catch((error) => {
console.error("Failed to create product:", error);
});
})
.on("end", () => {
console.log("Finished adding products");
});
// ===================
// server
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`App listening on port ${port}`);
});
After running this code in the terminal, I get an error like this,
Failed to create product: HTTPError: Response code 401 (Unauthorized)
at Request.<anonymous> (E:\Web-Development\shopify\codes\natural-
treasure\node_modules\got\dist\source\as-promise\index.js:118:42)
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
code: 'ERR_NON_2XX_3XX_RESPONSE',
timings: {
start: 1683388059109,
socket: 1683388059113,
lookup: 1683388059169,
connect: 1683388059181,
secureConnect: 1683388059199,
upload: 1683388059199,
response: 1683388059548,
end: 1683388059550,
error: undefined,
abort: undefined,
phases: {
wait: 4,
dns: 56,
tcp: 12,
tls: 18,
request: 0,
firstByte: 349,
download: 2,
total: 441
}
}
}
I am using the shop name from 'shop-name.myshopify.com/admin/', with the API key as 'apikey' and the API secret as the password. I also have the admin API key from the store, but I am not sure how to use it. My endgoal is, I will write a product in the csv/excel file, and it will automatically add that in the shopify store.
Upvotes: 0
Views: 292