Sample Code for Using Shopify REST Admin API from Google Apps Script (GAS)
I tried using the Shopify REST Admin API with Google Apps Script, so I’ll introduce the sample code.
I encountered a situation where I wanted to use the Shopify REST Admin API with Google Apps Script, so I wrote sample code.
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.