简体   繁体   中英

Import CoinMarketCap Data into Google Sheets

I used CRYPTOFINANCE.ai with Google Spreadsheets for some months now and I want to move on, to be able to get cryptocurrency data by "myself".

I discover the CoinMarketCap API and that should do it. I succeed in import one or many quotes. Now I would like to import the full listings data so I can have all the prices updated, in order to get a realistic value of my portfolio.

Here is what I have now, but it isn't importing the full listings:

function price() {
  var sh1=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Feuille 4'); 
  var requestOptions = {
    method: 'GET',
  uri: 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest',
  qs: {
    'start': '1',
    'limit': '5000',
    'convert': 'USD'}, 
    'headers' : {'X-CMC_PRO_API_KEY': '**********'}, 
    'json': true, 
    'gzip': true};

  var url='https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=ETH'; 
  var result = UrlFetchApp.fetch(url, requestOptions); 
  var txt= result.getContentText();
  var d=JSON.parse(txt);
  sh1.getRange(1, 2).setValue(d.data.ETH.quote.USD.price)
}

I know it has something to deal with: https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest but I couldn't figure it out by myself.

I am not familiar with this site but when I try the URL on its own it returns an error stating the API key is missing

{
    "status": {
        "timestamp": "2021-03-31T12:29:45.203Z",
        "error_code": 1002,
        "error_message": "API key missing.",
        "elapsed": 0,
        "credit_count": 0
    }
}

Also, it looks like Yahoo Finance has crypto historical data for free, no API required, just make an appropriate web request. This one grabs BTC from March 31, 2020 thru March 31, 2021.

https://query1.finance.yahoo.com/v7/finance/download/BTC-USD?period1=1585662494&period2=1617198494&interval=1d&events=history&includeAdjustedClose=true

One would need to decipher the format for period1 and period2, I believe they are UNIX timestamps, I was able to confirm at this site:

https://www.unixtimestamp.com/

Then this code would download the data into the current sheet:

function importCSVFromWeb() {
  // Provide the full URL of the CSV file.
  var csvUrl = "function importCSVFromWeb() {
  // Provide the full URL of the CSV file.
  var csvUrl = "https://query1.finance.yahoo.com/v7/finance/download/BTC-USD?period1=1585662494&period2=1617198494&interval=1d&events=history&includeAdjustedClose=true";
  var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();
  var csvData = Utilities.parseCsv(csvContent);

  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}

BTW, it looks like Yahoo Finance gets their crypto data from CoinMarketCap

I was mistaken two requests: /listings and /quotes

  • Listing gives you a list of multiple currencies.

  • Quotes gives you data from one coin only

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM