Step 1: Set Up Your Environment
Ensure you have Node.js installed. Then, create a new Node.js project and install the required packages.
mkdir shopify-product-import
cd shopify-product-import
npm init -y
npm install shopify-api-node axios 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
EXTERNAL_API_URL=https://api.example.com/products
Step 3: Create the Script
Create a file named importProducts.js
and add the following code:
require('dotenv').config();
const Shopify = require('shopify-api-node');
const axios = require('axios');
const shopify = new Shopify({
shopName: process.env.SHOPIFY_SHOP_NAME,
apiKey: process.env.SHOPIFY_API_KEY,
password: process.env.SHOPIFY_PASSWORD
});
async function fetchProductsFromExternalAPI() {
try {
const response = await axios.get(process.env.EXTERNAL_API_URL);
return response.data.products; // Adjust this line based on the actual response structure
} catch (error) {
console.error('Error fetching products from external API:', error.message);
throw error;
}
}
async function addProductToShopify(product) {
const newProduct = {
title: product.title,
body_html: product.description,
vendor: product.vendor,
product_type: product.type,
tags: product.tags,
variants: product.variants.map(variant => ({
option1: variant.option1,
price: variant.price,
sku: variant.sku,
inventory_quantity: variant.inventory_quantity
})),
images: product.images.map(image => ({ src: image.src }))
};
try {
const createdProduct = await shopify.product.create(newProduct);
console.log(`Product created: ${createdProduct.id}`);
} catch (error) {
console.error(`Failed to create product: ${error.message}`);
}
}
async function importProducts() {
try {
const products = await fetchProductsFromExternalAPI();
for (const product of products) {
await addProductToShopify(product);
}
console.log('All products have been added to Shopify');
} catch (error) {
console.error('Error importing products:', error.message);
}
}
importProducts();
Step 4: Run the Script
Run your Node.js script to fetch and add products to Shopify:
node importProducts.js
Explanation
- Environment Variables: The script uses environment variables to store sensitive information securely.
- Fetching Products: The
fetchProductsFromExternalAPI
function fetches products from the external API usingaxios
. - Adding Products: The
addProductToShopify
function formats and adds each product to Shopify using theshopify-api-node
library. - Import Process: The
importProducts
function orchestrates the fetching and adding process, iterating over the fetched products and adding them one by one to Shopify.
Summary
By following this guide, you will be able to fetch products from an external API and add them to your Shopify store using Node.js. Customize the script as needed to handle specific product data structures and additional requirements. This approach allows for automated and efficient product management across different platforms.
Leave a Reply