Step 1: Install Required Packages
First, install the necessary npm packages:
npm install shopify-api-node dotenv express body-parser
Step 2: Set Up Environment Variables
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: Initialize Shopify API Client
Create a file named shopify.js
to initialize the Shopify API client:
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
})
module.exports = shopify
Step 4: Set Up Express Server
Create a file named server.js
to set up your Express server:
const express = require('express')
const bodyParser = require('body-parser')
const shopify = require('./shopify')
const app = express()
app.use(bodyParser.json())
app.get('/', (req, res) => {
res.send('Shopify Integration App')
})
// Fetch and display products
app.get('/products', async (req, res) => {
try {
const products = await shopify.product.list()
res.json(products)
} catch (error) {
res.status(500).send(error.message)
}
})
// Save a new product
app.post('/products', async (req, res) => {
try {
const newProduct = req.body
const product = await shopify.product.create(newProduct)
res.json(product)
} catch (error) {
res.status(500).send(error.message)
}
})
const PORT = process.env.PORT || 3000
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`)
})
Step 5: Run the Server
Start your Node.js server:
node server.js
Step 6: Fetch Products
To fetch products, make a GET request to the /products
endpoint:
curl http://localhost:3000/products
Step 7: Advanced Fetching and Saving Strategies
Fetch Products with Filters
You can fetch products with specific filters using query parameters:
app.get('/products', async (req, res) => {
const { limit = 10, page = 1 } = req.query
try {
const products = await shopify.product.list({ limit, page })
res.json(products)
} catch (error) {
res.status(500).send(error.message)
}
})
Batch Update Products
To update multiple products efficiently, you can implement batch updates:
app.put('/products', async (req, res) => {
const { products } = req.body
try {
const updatedProducts = []
for (const product of products) {
const updatedProduct = await shopify.product.update(product.id, product)
updatedProducts.push(updatedProduct)
}
res.json(updatedProducts)
} catch (error) {
res.status(500).send(error.message)
}
})
Summary
By following this guide, you will have set up a Node.js application that can fetch and save products using Shopify’s Admin API. Here’s a quick recap:
- Install Dependencies: Install
shopify-api-node
,dotenv
,express
, andbody-parser
. - Set Up Environment Variables: Securely store your API credentials in a
.env
file. - Initialize Shopify API Client: Create a
shopify.js
file to initialize the API client. - Set Up Express Server: Create a
server.js
file to set up an Express server. - Fetch and Save Products: Implement endpoints to fetch and save products.
- Advanced Strategies: Utilize filters and batch updates for more efficient data handling.
This foundational setup can be customized and extended to fit your specific automation needs in Shopify, enabling you to master product management through the Shopify API.
Leave a Reply