😊

アクセストークンを使用してAWS LambdaからShopify Admin APIにアクセスする方法

2023/05/06に公開
const https = require('https');

exports.handler = async (event) => {
  // Shopify APIの認証情報
  const accessToken = 'YOUR_ACCESS_TOKEN';
  const shopName = 'YOUR_SHOP_NAME.myshopify.com';

  // 顧客データ
  const postData = JSON.stringify({
    customer: {
      first_name: '山田',
      last_name: '太郎',
      email: 'yamada@example.com'
    }
  });

  // リクエストヘッダー
  const headers = {
    'Content-Type': 'application/json',
    'X-Shopify-Access-Token': accessToken
  };

  // リクエストオプション
  const options = {
    hostname: `${shopName}`,
    path: '/admin/api/2023-04/customers.json',
    method: 'POST',
    headers: headers
  };

  // リクエストを送信
  const response = await new Promise((resolve, reject) => {
    const req = https.request(options, (res) => {
      let data = '';

      res.on('data', (chunk) => {
        data += chunk;
      });

      res.on('end', () => {
        resolve({
          statusCode: res.statusCode,
          body: data
        });
      });
    });

    req.on('error', (err) => {
      reject(err);
    });

    req.write(postData);
    req.end();
  });

  return response;
};

Discussion