Step 1: Set Up Your Environment
- Install Dependencies: Ensure you have
dotenv
andshopify-api-node
installed:
npm install dotenv shopify-api-node
Create Your .env
File:
- Create a
.env
file in the root of your project with your Shopify store details:
SHOPIFY_STORE=your-store-name.myshopify.com
SHOPIFY_ACCESS_TOKEN=your-access-token
Step 2: Create Your Script
Create a file, e.g., fetchOrders.js
, and add the following code to fetch and display orders:
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 fetchOrders = async () => {
const query = `
{
orders(first: 10) {
edges {
node {
id
name
email
totalPriceSet {
shopMoney {
amount
currencyCode
}
}
lineItems(first: 10) {
edges {
node {
title
quantity
priceSet {
shopMoney {
amount
currencyCode
}
}
}
}
}
}
}
}
}`
try {
const response = await shopify.graphql(query)
console.log('Orders:', JSON.stringify(response, null, 2))
} catch (error) {
console.error('Error fetching orders:', error)
}
}
fetchOrders()
Explanation
- Environment Variables: The
dotenv
package loads environment variables from the.env
file. - Shopify API Node: This package simplifies interaction with Shopify’s API.
- GraphQL Query: The query retrieves the first 10 orders along with their details such as ID, name, email, total price, and line items.
- Error Handling: The
try-catch
block captures any errors during the API call and logs them.
Step 3: Run the Script
To run the script, execute the following command in your terminal:
node fetchOrders.js
This will fetch the first 10 orders from your Shopify store and log the details to the console.
Additional Considerations
- Pagination: If you need to fetch more orders, you can handle pagination by using
cursor-based
pagination with theafter
argument in your query. - Error Handling: Implement robust error handling depending on your requirements.
- Data Processing: Process or save the fetched data as needed within the
try
block.
Complete Example Code
Here’s the complete example code for fetching and displaying Shopify orders:
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 fetchOrders = async () => {
const query = `
{
orders(first: 10) {
edges {
node {
id
name
email
totalPriceSet {
shopMoney {
amount
currencyCode
}
}
lineItems(first: 10) {
edges {
node {
title
quantity
priceSet {
shopMoney {
amount
currencyCode
}
}
}
}
}
}
}
}
}`
try {
const response = await shopify.graphql(query)
console.log('Orders:', JSON.stringify(response, null, 2))
} catch (error) {
console.error('Error fetching orders:', error)
}
}
fetchOrders()
By following these steps, you should be able to successfully fetch and display Shopify orders using the GraphQL API in a Node.js application. If you have any more questions or need further assistance, feel free to ask!
Leave a Reply