Step 1: Set Up Your Environment
Create a new Node.js project and install the required packages:
mkdir shopify-product-fetch
cd shopify-product-fetch
npm init -y
npm install shopify-api-node dotenv
Step 2: Create the .env
File
Create a .env
file in the root of your project to securely store your Shopify API credentials:
SHOPIFY_SHOP_NAME=your-shop-name
SHOPIFY_API_KEY=your-api-key
SHOPIFY_PASSWORD=your-password
Step 3: Create the Script
Create a file named fetchProducts.js
and add the following code to fetch products:
require('dotenv').config();
const Shopify = require('shopify-api-node');
const shopify = new Shopify({
shopName: process.env.SHOPIFY_SHOP_NAME,
apiKey: process.env.SHOPIFY_API_KEY,
password: process.env.SHOPIFY_PASSWORD
});
async function fetchProducts() {
try {
const products = await shopify.product.list({ limit: 250 });
console.log(`Fetched ${products.length} products`);
products.forEach(product => {
console.log(`ID: ${product.id}, Title: ${product.title}`);
});
} catch (error) {
console.error(`Failed to fetch products: ${error.message}`);
}
}
fetchProducts();
Explanation
- Environment Variables: The script uses environment variables to store sensitive information securely.
- Fetch Products Function: The
fetchProducts
function uses the Shopify API’sproduct.list
method to fetch a list of products from your Shopify store. - Error Handling: The script includes error handling to log any issues encountered during the fetching process.
- Pagination: The
limit: 250
parameter is used to fetch up to 250 products at a time, which is the maximum allowed by the Shopify API per request. For stores with more than 250 products, you would need to implement pagination.
Step 4: Run the Script
Run your Node.js script to fetch products from your Shopify store:
node fetchProducts.js
Handling Pagination
If you have more than 250 products, you need to handle pagination. Here’s how you can modify the script to fetch all products:
require('dotenv').config();
const Shopify = require('shopify-api-node');
const shopify = new Shopify({
shopName: process.env.SHOPIFY_SHOP_NAME,
apiKey: process.env.SHOPIFY_API_KEY,
password: process.env.SHOPIFY_PASSWORD
});
async function fetchAllProducts() {
const allProducts = [];
let params = { limit: 250, page_info: undefined };
let hasMoreProducts = true;
while (hasMoreProducts) {
const products = await shopify.product.list(params);
allProducts.push(...products);
const linkHeader = products.headers['link'];
const pageInfo = linkHeader && linkHeader.match(/page_info=([^&>]+)/);
if (pageInfo) {
params.page_info = pageInfo[1];
} else {
hasMoreProducts = false;
}
}
return allProducts;
}
async function fetchProducts() {
try {
const products = await fetchAllProducts();
console.log(`Fetched ${products.length} products`);
products.forEach(product => {
console.log(`ID: ${product.id}, Title: ${product.title}`);
});
} catch (error) {
console.error(`Failed to fetch products: ${error.message}`);
}
}
fetchProducts();
Summary
By following this guide, you will be able to fetch products from your Shopify store using the Admin API with Node.js. Customize the script as needed to handle specific product attributes and additional requirements. This approach allows for efficient and automated product management in your Shopify store.
Leave a Reply