Step-by-Step Guide
- Set Up Your Environment:
- Install the required packages:
npm install dotenv shopify-api-node
Create Your .env File:
- Create a
.envfile in the root of your project and add your Shopify store details:
SHOPIFY_STORE=your-store-name.myshopify.com
SHOPIFY_ACCESS_TOKEN=your-access-token
- Create Your Script:
- Create a file, e.g.,
createProduct.js, and add the following code:
- Create a file, e.g.,
require('dotenv').config()
const Shopify = require('shopify-api-node')
const shopify = new Shopify({
shopName: process.env.SHOPIFY_STORE,
accessToken: process.env.SHOPIFY_ACCESS_TOKEN
})
const createProduct = async () => {
const mutation = `
mutation {
productCreate(input: {
title: "New GraphQL Product 2"
bodyHtml: "<strong>Great product!</strong>"
vendor: "Your Vendor Name"
tags: ["Tag1", "Tag2"]
variants: [{
title: "Default Title"
price: "19.99"
sku: "SKU1234"
}]
}) {
product {
id
title
}
userErrors {
field
message
}
}
}`
try {
const response = await shopify.graphql(mutation)
console.log('Product created:', JSON.stringify(response, null, 2))
} catch (error) {
console.error('Error creating product:', error)
}
}
createProduct()
Explanation
- Environment Variables:
dotenvis used to load environment variables from the.envfile. - Shopify API Node: This package simplifies the interaction with Shopify’s API.
- GraphQL Mutation: The
productCreatemutation creates a new product with specified attributes like title, bodyHtml, vendor, tags, and variants. - Error Handling: The
try-catchblock captures any errors during the API call and logs them.
Run the Script
To run the script, execute the following command in your terminal:
node createProduct.js
This will create a new product in your Shopify store with the details specified in the mutation.
By following these steps, you can successfully create products in Shopify using the GraphQL API with Node.js. If you need further customization or additional features, you can modify the mutation accordingly.

Leave a Reply