function doGet(e) {
var API_KEY;
// スクリプトプロパティからAPI_KEYを取得する
const scriptProperties = PropertiesService.getScriptProperties();
const properties = scriptProperties.getProperties();
for (const key in properties) {
if (key == "API_KEY"){
API_KEY = properties[key];
break
}
}
var url = 'https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest';
// -- cryptocurrency ids -- //
// solana : 5426
// green-satoshi-token : 16352
// bnb : 1839
// green-satoshi-token-bsc : 20236
// green-metaverse-token : 18069
// bitcoin : 1
// usd-coin : 3408
var cryptocurrencyIds = ["5426","16352","1839","20236","18069","1","3408"];
// リクエストのオプション設定
var queryParams = {
'id':cryptocurrencyIds.join(),
'convert':'USD'
};
var headers = {
'Accepts':'application/json',
'X-CMC_PRO_API_KEY':API_KEY
};
var options = {
'headers':headers,
'muteHttpExceptions': true
};
// 各仮想通貨のUSD価格情報を取得
requestUrl = generateRequestUrl(url, queryParams);
var cryptocurrencyDetails = JSON.parse(UrlFetchApp.fetch(requestUrl,options).getContentText());
// USD → JPYのレート取得のため別途APIを実行
var queryParams = {
'id':'5426',
'convert':'JPY'
};
requestUrl = generateRequestUrl(url, queryParams);
var solJpyDetails = JSON.parse(UrlFetchApp.fetch(requestUrl,options).getContentText());
// USD → JPYレート
solUsd = cryptocurrencyDetails['data']['5426']['quote']['USD']['price']
solJpy = solJpyDetails['data']['5426']['quote']['JPY']['price']
usdToJpy = solJpy/solUsd
// レスポンス用にデータ整形
var responseData = [];
for (index in cryptocurrencyIds) {
var data = cryptocurrencyDetails['data'][cryptocurrencyIds[index]];
var details = {
'id':data['id'],
'slug':data['slug'],
"name":data['name'],
"symbol": data['symbol'],
"jpy": data['quote']['USD']['price']*usdToJpy,
"usd": data['quote']['USD']['price'],
"dtLastUpdated": data['last_updated']
};
responseData.push(details)
}
// 「Content service TextOutput object」としてレスポンスを生成
var res = ContentService.createTextOutput();
res.setMimeType(ContentService.MimeType.JSON);
res.setContent(JSON.stringify(responseData));
return res;
}