Read Shopify Inventory Levels

Updated: Oct 30, 2023

This example shows how to read inventory levels for a Shopify store's location and save them in an Excel file.  The target can be changed to any format DataPipeline supports (like JSON, XML, Parquet, etc.).  The pipeline's job can also include filters, transformations, and lookups to fit your use-case. 

For this example, we'll need:

  1. A Shopify custom app with read_inventory Admin API access scope.
  2. An ACCESS_TOKEN and the store's DOMAIN.

For information on how to create a custom app and obtain an access key visit Shopify custom apps.

Java Code Listing

package com.northconcepts.datapipeline.examples.shopify;

import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.core.DataWriter;
import com.northconcepts.datapipeline.excel.ExcelDocument;
import com.northconcepts.datapipeline.excel.ExcelWriter;
import com.northconcepts.datapipeline.job.Job;
import com.northconcepts.datapipeline.shopify.ShopifyInventoryLevelReader;
import com.northconcepts.datapipeline.shopify.client.ShopifyInventoryLevelCriteria;

import java.io.File;

public class ReadShopifyInventoryLevels {

    private static final String DOMAIN = "https://your-store-domain.com";
    private static final String ACCESS_TOKEN = "ACCESS_TOKEN";
    
    public static void main(String[] args) {
        DataReader reader = new ShopifyInventoryLevelReader(DOMAIN, ACCESS_TOKEN)
            .setInventoryLevelCriteria(new ShopifyInventoryLevelCriteria()
                .setLocationIds("92972351787")
                .setInventoryItemIds("49129939829035", "49129939796267", "49129939763499"));

        ExcelDocument document = new ExcelDocument();
        DataWriter writer = new ExcelWriter(document).setFieldNamesInFirstRow(true);

        Job.run(reader, writer);

        document.save(new File("data/output/inventory-levels.xls"));
    }
    
}


Code Walkthrough

  1.  An instance of ShopifyInventoryLevelReader which takes DOMAIN and ACCESS_TOKEN as arguments is created. This will be used to read a list of Shopify inventory levels of the specified location/s.
  2.  .setLocationIds("92972351787") is used to filter inventory levels for the location we are interested in.
  3.  .setInventoryItemIds("49129939829035", "49129939796267", "49129939763499") is used to filter for specific inventory item ids. For more information on query parameters visit Shopify Admin API
  4. Job.run() is then used to transfer data from the reader to ExcelWriter.

 

Mobile Analytics