Sample Code for Email Customer Search with Shopify REST Admin API - Google Apps Script (GAS) Edition

Tadashi Shigeoka ·  Sat, April 23, 2022

I wrote Google Apps Script (GAS) sample code for email customer search with Shopify REST Admin API, so I’ll introduce it.

Shopify | ショッピファイ

Background: Want to Search Customers by Email with Shopify REST Admin API

I encountered a situation where I wanted to perform exact email search for customers using Shopify REST Admin API, so I wrote sample code with Google Apps Script.

This is a continuation of Shopify REST Admin API を Google Apps Script (GAS) から使うサンプルコード - CodeNote.

Sample Code: Customer Email Search with Shopify REST Admin API

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

function main(){
  const customer = findCustomerByEmail('[email protected]');
  Logger.log(customer);
}

const SHOPIFY_API_KEY = PropertiesService.getScriptProperties().getProperty('SHOPIFY_API_KEY');
const SHOPIFY_PASSWORD = PropertiesService.getScriptProperties().getProperty('SHOPIFY_PASSWORD');
const SHOPIFY_API_URL = PropertiesService.getScriptProperties().getProperty('SHOPIFY_API_URL');

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());
}

function searchCustomersByEmail(email) {
  const response = requestShopifyAPI_(`/customers/search.json?query=email:${email}`, 'get');
  return response.customers;
}

function findCustomerByEmail(email) {
  const customers = searchCustomersByEmail(email);
  const customer = customers.find(c => c.email === email);
  return customer;
}

Above, I wrote Google Apps Script (GAS) sample code for customer email search with Shopify REST Admin API.

That’s all from the Gemba.