1

the following code works ok for me in GAS (Google Apps Script) for CoinMarketCap API

function cmcQuotes2() {
  
  var ss       = SpreadsheetApp.getActiveSpreadsheet();
  var sheet    = ss.getSheetByName('base');
  var lastRow  = sheet.getLastRow();
  var extension= lastRow - 2;
  
  var oldAsset=sheet.getRange(2, 1, extension).getValues();
  var rows=[];
  var j;
  for (j=0; j < oldAsset.length; j++) {
    rows.push(oldAsset[j][0]);}
  
  var apiKey = '*** your api key'; 
  var params = {
    'method'  : 'GET',
    'qs'      : {'start': '1', 'limit': '100', 'convert': 'USD'},
    'headers' : {'X-CMC_PRO_API_KEY': apiKey},
    'muteHttpExceptions': true};
  
  var url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest";

var data = UrlFetchApp.fetch(url, params);
  var data = JSON.parse(data.getContentText());
  var data = data.data; 
  
  
  var i;
  for (i=0; i < data.length; i++) {

var asset       = data[i].symbol;               
  var quote       = data[i].quote.USD.price;
    if (quote < 1) {var a = parseFloat(quote);   var a = a.toFixed(8);   var quote = parseFloat(a);} 
    if (quote >=1) {var a = parseFloat(quote);   var a = a.toFixed(2);   var quote = parseFloat(a);} 
    
  var row=rows.indexOf(asset)+2;   

if (row>=2){
       sheet.getRange(row, 2).setValue(quote);
  }  
  }
}

but

no matter what I try to get the price quoted in EUR instead of USD, I can't.

I simply can't.

convert EUR instead of USD? Doesn't work.

  var params = {
    'method'  : 'GET',
    'qs'      : {'start': '1', 'limit': '100', 'convert': 'EUR'},
    'headers' : {'X-CMC_PRO_API_KEY': apiKey},
    'muteHttpExceptions': true};

quote.EUR instead of quote.USD ?? Doesn't work ..

var quote       = data[i].quote.EUR.price;

first case simply ignores EUR, second case it returns:

12:59:50
Errore
TypeError: Cannot read property 'price' of undefined

What's your take about it? thanks

John Galassi
  • 309
  • 2
  • 16

1 Answers1

2

Modification points:

  • At params of UrlFethApp.fetch(url, params), the property of qs is not existing. I thought that this might be the reason of your issue.
  • From the official document of "Listings Latest", it seems that the value of qs is required to be used as the query parameters.

When above points are reflected to your script, it becomes as follows.

Modified script:

From:
  var apiKey = '*** your api key'; 
  var params = {
    'method'  : 'GET',
    'qs'      : {'start': '1', 'limit': '100', 'convert': 'USD'},
    'headers' : {'X-CMC_PRO_API_KEY': apiKey},
    'muteHttpExceptions': true};
  
  var url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest";

var data = UrlFetchApp.fetch(url, params);
To:
var apiKey = '*** your api key';
var url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?start=1&limit=100&convert=USD";
var params = {
  'method': 'GET',
  'headers': {'X-CMC_PRO_API_KEY': apiKey, 'Accept': 'application/json'},
  'muteHttpExceptions': true
};
var data = UrlFetchApp.fetch(url, params);

Note:

  • In this modified script, it supposes that your API key can be used for the API you want to use. Please be careful this.

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • thanks @Tanaike, this worked for me. I hope I can ask additional info for some details in case I don't understand while working on it, but for the time being it worked and I thank you for this. – John Galassi Apr 26 '21 at 13:53
  • @John Galassi Thank you for replying. I'm glad your issue was resolved. – Tanaike Apr 27 '21 at 01:14