Skip to content

Instantly share code, notes, and snippets.

@jalcantarab
Last active June 15, 2022 19:36
Show Gist options
  • Save jalcantarab/903121ac2a1b8879746224bf0feee770 to your computer and use it in GitHub Desktop.
Save jalcantarab/903121ac2a1b8879746224bf0feee770 to your computer and use it in GitHub Desktop.
Simple Shopify API calls examples

Shopify API - Script examples

Simple examples of calls to the Shopify API using Google Apps Script (.gs). So far:

  • ssCatalogPublicationStatus.gs - Meant as an addon for Google Sheets to check the publication status of items.
    • getCatalogJSON() - Returns complete JSON catalog for an instance.
    • checkPublicationStatus() - Checks a list of handle-ids in Google Sheets against a JSON catalog Compares the first column of a google spreadsheet to the JSON Shopify catalog. Adds published date to third column, and verifies date to published status.
function getCatalogJSON() {
var basicToken = var basicToken = Utilities.base64Encode('<username>' + ":" + '<password>');
var settings = {
"async": true,
"crossDomain": true,
"url": "<shopify_url>",
"method": "GET",
"headers": {
"authorization": "Basic ",
"cache-control": "no-cache"
}
}
var headers = {
Authorization : "Basic" + basicToken
};
var options = {
method : "GET",
"headers" : headers,
followRedirects : true,
muteHttpExceptions : true
};
var result = UrlFetchApp.fetch(settings.url, options);
if (result.getResponseCode() == 200) {
return JSON.parse(result.getContentText());
}else{
Logger.log("error");
return
}
}
function checkPublicationStatus(){
var catalog_json = getCatalogJSON();
var sh = SpreadsheetApp.getActiveSpreadsheet();
var ss = sh.getActiveSheet();
for (var i=0; i<catalog_json.products.length; i++){
var cell = ss.getActiveCell();
cell.setBackground('#ffff55');
var value = catalog_json.products[i].handle;
var data = ss.getDataRange().getValues();
var noDate = catalog_json.products[i].published_at;
var ssPublish = '';
for(var step=0;step<data.length;step++){
Logger.log(step+' -- '+value+' = '+data[step][0]);
if(data[step][0]==''){ return };
if(value.toString().toLowerCase()==data[step][0].toString().toLowerCase()){
ss.getRange(step+1,2).activate().setBackground('#fffff5');
ss.getRange(step+1,3).activate().setValue(noDate);
ssPublish = data[step][1];
if(ssPublish==false){ss.getRange(step+1,2).activate().setBackground('#ff0000');}
if(noDate=='' && ssPublish==false){ss.getRange(step+1,2).activate().setBackground('#f10500');}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment