Shopify REST Admin API を Google Apps Script (GAS) から使うサンプルコード

Shopify REST Admin API を Google Apps Script で使ってみたので、サンプルコードをご紹介します。

Shopify | ショッピファイ

背景 Shopify REST Admin API を Google Apps Script で使いたい

Shopify REST Admin API を Google Apps Script で使いたいシチュエーションに遭遇したので、サンプルコードを書いてみました。

サンプルコード GAS + Shopify REST Admin API で customers 検索

Shopify REST Admin API /customers/search.json を Google Apps Script から呼び出して、顧客情報を email 検索してみたサンプルコードは以下のとおりです。

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

以上、Shopify REST Admin API を Google Apps Script で使ってみた、現場からお送りしました。

参考情報