Intermediate8 min read
Get Top-Selling Shopify Products Using Node.js
Learn how to fetch the top-selling products from your Shopify store using Node.js and the Storefront GraphQL API. This tutorial demonstrates how to retrieve product details, variants, and prices programmatically for building dashboards or e-commerce frontends.
Node JSShopify
C
Code Shelf
Published on March 5, 2026
94 views0 likes
1. Introduction
Shopify’s Storefront API allows developers to access product data programmatically. In this tutorial, we will fetch the top-selling products using Node.js and GraphQL, parse the data, and return it in a usable format.
2. Prerequisites
Before starting, ensure you have:
- A Shopify store
- Storefront API token
- Node.js installed
- Basic knowledge of JavaScript and Express
- dotenv package installed
3. Setup Environment Variables
Create a .env file in your project root:
.env
bash1 SHOPIFY_DOMAIN=your-store-name.myshopify.com 2 SHOPIFY_STOREFRONT_TOKEN=your_storefront_api_token
4. Install Dependencies
Install the necessary Node.js packages:
1 npm install graphql-request dotenv
5. Initialize API Config
Create a config file to store API URL and tokens:
1 import { request } from "graphql-request"; 2 import dotenv from "dotenv"; 3 dotenv.config(); 4 5 const SHOPIFY_DOMAIN = process.env.SHOPIFY_DOMAIN; 6 const STOREFRONT_API_TOKEN = process.env.SHOPIFY_STOREFRONT_TOKEN; 7 const API_URL = `https://${SHOPIFY_DOMAIN}/api/2025-01/graphql.json`;
6. Fetch Top-Selling Products
Use GraphQL to query the top-selling products:
1 export const getTopSelling = async (req, res) => { 2 try { 3 const GET_TOP_SELLING = ` 4 { 5 products(first: 4, sortKey: BEST_SELLING) { 6 edges { 7 node { 8 id 9 title 10 handle 11 featuredImage { 12 url 13 } 14 variants(first: 1) { 15 edges { 16 node { 17 price { 18 amount 19 currencyCode 20 } 21 compareAtPrice { 22 amount 23 currencyCode 24 } 25 } 26 } 27 } 28 } 29 } 30 } 31 } 32 `; 33 34 const data = await request( 35 API_URL, 36 GET_TOP_SELLING, 37 {}, 38 { "X-Shopify-Storefront-Access-Token": STOREFRONT_API_TOKEN } 39 ); 40 41 const products = data.products.edges.map((edge) => { 42 const node = edge.node; 43 const variant = node.variants.edges[0]?.node || {}; 44 return { 45 id: node.id, 46 title: node.title, 47 handle: node.handle, 48 featuredImage: node.featuredImage, 49 price: variant.price || null, 50 compareAtPrice: variant.compareAtPrice || null, 51 }; 52 }); 53 54 res.json(products); 55 } catch (err) { 56 console.error("Top selling error:", err); 57 res.status(500).json({ error: "Failed to fetch top selling products" }); 58 } 59 };
7. Testing the Endpoint
Use Postman or Thunder Client:
1 GET /api/top-selling
8. Conclusion
You have successfully implemented a Node.js endpoint to fetch Shopify top-selling products. You can now display this data on dashboards, homepages, or e-commerce frontends.
Comments (0)
Loading...
No comments yet. Be the first to comment!