- Node.js installed.
shopify-api-node
package installed..env
file set up with your Shopify API credentials.
Step 1: Install the required packages
npm install shopify-api-node dotenv
Step 2: Set up your .env
file
Create a .env
file in the root of your project with the following content:
SHOPIFY_SHOP_NAME=your-shop-name
SHOPIFY_API_KEY=your-api-key
SHOPIFY_PASSWORD=your-password
Step 3: Create the Node.js script
createProduct.js
const Shopify = require('shopify-api-node')
require('dotenv').config()
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()
Step 4: Run the script
node createProduct.js
Explanation:
- Environment Variables: The script reads your Shopify credentials from the
.env
file. - Creating the Product: The
createProduct
function defines a new product with various attributes liketitle
,body_html
,vendor
,product_type
,tags
,variants
, andimages
. - Shopify API Call: The script uses the
shopify-api-node
library to create the product in your Shopify store. - Error Handling: If the product creation fails, an error message is logged.
This script provides a basic structure for creating a product in Shopify. Adjust the product details as needed to match your specific requirements.
4o
Leave a Reply