Download real-time crypto market data from CoinMarketCap.com into a CSV file with midas

Published 03-10-2018 21:23:38

Sometimes it could be handy to have a CSV file of your crypto portfolio with the current prices provided by CoinMarketcCap.com. In this post, I’ll show you how to get this easily done by using an open source tool called midas.

Preparation

Before we can start setting up our CSV portfolio, we have to take care of some ingredients. The first thing we need to do is to create a CSV file that contains the names of the coins we want to have in our portfolio file. It is very important that the names are spelled correctly since this will be the input for the CoinMarketCap API later. Secondly, we have to understand the CoinMarketCap API and what it returns. Thirdly, we have to set up a midas job that downloads and synchronizes the CoinMarketCap data into our CSV.



The CSV base file

This file will represent our crypto portfolio. The only thing that we have to do, is to fill it with the names of the cryptocurrencies that we want to include in our portfolio.

Example:

currency
bitcoin
ethereum
monero
ripple

The first line currency is treated as a header row and will be ignored, each other row must be a correctly spelled currency name.



CoinMarketCap API

CoinMarketCap provides several publicly accessible APIs for free (cheers!). For our little task, we will use the Ticker endpoint for a specific currency. The concrete endpoint looks like the following:

https://api.coinmarketcap.com/v1/ticker/{NAME_OF_CURRENCY}

Let’s assume that we want to get the ticker data of Bitcoin. The specific call would look like this (just copy and paste it into your browser):

https://api.coinmarketcap.com/v1/ticker/bitcoin

As a result, we get a JSON response that contains all the information that we need.

[
    {
        "id": "bitcoin",
        "name": "Bitcoin",
        "symbol": "BTC",
        "rank": "1",
        "price_usd": "573.137",
        "price_btc": "1.0",
        "24h_volume_usd": "72855700.0",
        "market_cap_usd": "9080883500.0",
        "available_supply": "15844176.0",
        "total_supply": "15844176.0",
        "max_supply": "21000000.0",
        "percent_change_1h": "0.04",
        "percent_change_24h": "-0.3",
        "percent_change_7d": "-0.57",
        "last_updated": "1472762067"
    }
] 



midas

midas is an open source tool for fusing and combining data from multiple sources. It will take care of getting the data from the CSV file and filling it with the information from CoinMarketCap.

  1. Install midas via npm install midas-core -g
  2. Create a new folder and put your CSV file there
  3. Download or clone the CoinMarketCap enricher for midas from Github: https://github.com/midas-science/enricher-coinmarketcap
  4. Create a new file called portfolio-sync.json
  5. Fill it with the following configuration and ajust the paths:
{
	"source": {
		"csv": {
			"path": "ABSOLUTE_PATH_TO_YOUR_CSV_PORTFOLIO/portfolio.csv",
			"delimiter": ","
		}
	},
	"target": {
		"csv": {
			"path": "ABSOLUTE_PATH_TO_YOUR_CSV_PORTFOLIO/portfolio.csv",
			"delimiter": ","
		}
	},
	"enrichers": [
		{
			"name": "CoinMarketCap",
			"path": "ABSOLUTE_PATH_TO_THE_DOWNLOADED_OR_CLONED_ENRICHER/enricher-coinmarketcap",
			"config": {
				"input_parameter": "$..currency",
				"target_property": "cmc_data"
			}
		}
	],
	"name": "CoinMarketCap_EnrichmentProcess"
}

In the last step, start the process via midas enrich -c “ABSOLUTE_PATH_TO_YOUR_PORTFOLIO_SYNC/portfolio-sync.json”

That’s it, just wait a few seconds until everything is downloaded and then your portfolio file should be updated with the most recent prices and market data!