Sample Code for Using Shopify REST Admin API from Google Apps Script (GAS)

Tadashi Shigeoka ·  Thu, April 21, 2022

I tried using the Shopify REST Admin API with Google Apps Script, so I’ll introduce the sample code.

Shopify | ショッピファイ

Background: Want to Use Shopify REST Admin API with Google Apps Script

I encountered a situation where I wanted to use the Shopify REST Admin API with Google Apps Script, so I wrote sample code.

Sample Code: Customer Search with GAS + Shopify REST Admin API

The sample code for calling Shopify REST Admin API /customers/search.json from Google Apps Script to search customer information by email is as follows:

const SHOPIFY_API_KEY = PropertiesService.getScriptProperties().getProperty('SHOPIFY_API_KEY');
const SHOPIFY_PASSWORD = PropertiesService.getScriptProperties().getProperty('SHOPIFY_PASSWORD');
const SHOPIFY_API_URL = 'https://***.myshopify.com/admin/api/2022-04';

function searchCustomers() {
  const response = requestShopifyAPI_('/customers/search.json?query=email:[email protected]', 'get');
  Logger.log(response);

  response.customers.forEach(customer => {
    Logger.log(`id: ${customer.id}, email: ${customer.email}`);
  });
}

function requestShopifyAPI_(url, method, params) {
  const options = {
    method: method,
    payload: JSON.stringify(params),
    headers: {
      "Authorization": "Basic " + Utilities.base64Encode(`${SHOPIFY_API_KEY}:${SHOPIFY_PASSWORD}`),
      "Content-type": 'application/json',
    },
    muteHttpExceptions: false,
  };

  const response = UrlFetchApp.fetch(`${SHOPIFY_API_URL}${url}`, options);
  return JSON.parse(response.getContentText());
}

Above, I tried using the Shopify REST Admin API with Google Apps Script.

That’s all from the Gemba.

Reference Information