Step 1: Set Up Your Environment
Create a new Node.js project and install the required packages:
mkdir shopify-product-creation
cd shopify-product-creation
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 createProduct.js
and add the following code to create a product:
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 createProduct() {
const newProduct = {
title: 'New Product Title',
body_html: '<strong>Good product!</strong>',
vendor: 'Your Vendor Name',
product_type: 'Your Product Type',
tags: 'tag1, tag2',
variants: [
{
option1: 'Default Title',
price: '19.99',
sku: '123',
inventory_quantity: 100
}
],
images: [
{
src: 'https://example.com/path/to/image.jpg'
}
]
};
try {
const createdProduct = await shopify.product.create(newProduct);
console.log(`Product created: ${createdProduct.id}`);
} catch (error) {
console.error(`Failed to create product: ${error.message}`);
}
}
createProduct();
Explanation
- Environment Variables: The script uses environment variables to store sensitive information securely.
- Create Product Function: The
createProduct
function defines the product data structure and uses the Shopify API’sproduct.create
method to create the product in your Shopify store. - Error Handling: The script includes error handling to log any issues encountered during the product creation process.
Step 4: Run the Script
Run your Node.js script to create a product in your Shopify store:
node createProduct.js
Summary
By following this guide, you will be able to create products in your Shopify store using the Admin API with Node.js. Customize the newProduct
object as needed to include additional product attributes and variations. This approach allows for efficient and automated product management in your Shopify store.
Leave a Reply