Sample Code for Email Customer Search with Shopify REST Admin API - Google Apps Script (GAS) Edition
I wrote Google Apps Script (GAS) sample code for email customer search with Shopify REST Admin API, so I’ll introduce it.
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.
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.