If you’ve ever browsed CryptoCompare.com, you’ll find it to be a wealth of crypto knowledge on anything from mining to ICO’s to exchanges. It’s user friendly website is built to be accessed by anyone regardless of experience, but what many people don’t realize is the vast amount of information that can be used for analytics through CryptoCompare’s Public API. In this guide I will show you how to use the API, along with Python, to pull and analyze some cryptocurrency information.
Note: If you want to just access the data in a raw .csv file, download it here.
Getting Python Ready
In this lesson we are using Python 3 and the pandas and requests libraries. If you have Anaconda, you will already have these packages preinstalled. Otherwise you can install them manually: Requests and Pandas. It’s assumed that you have basic knowledge of DataFrames (essentially spreadsheets for python).
Getting a Coin List
Before we begin using any type of analytics to search for buy/sell opportunities, we can gather a complete list of CryptoCompare’s coins.
CryptoCompare’s API has a nice URL for us to access all of their coins: https://www.cryptocompare.com/api/data/coinlist/ – we access it in python the following way.
import pandas as pd import requests req = requests.get('https://www.cryptocompare.com/api/data/coinlist/').json()
If we print out the JSON dictionary that’s returned, we get something like this:
Good news – we’re getting our coin list! Bad news – it’s a mess. If we print out the dictionary’s keys, we notice that the Data key holds all the critical information about the coins. Let’s add a few lines of code to get this information out, put our coin list into a DataFrame, and write it as an Excel-Readable .csv file:
info = req['Data'] coinList = pd.DataFrame(info) coinList = coinList.transpose() coinList.to_csv('coinList.csv')
Just for future use and code cleanup, we can write our entire program in a single function:
def getCoinList(): req = requests.get('https://www.cryptocompare.com/api/data/coinlist/').json() info = req['Data'] coinList = pd.DataFrame(info) coinList = coinList.transpose() coinList.to_csv('coinList.csv') return coinList
Executing the function will return a DataFrame with all the coins and their corresponding features and store this information in a .csv called coinList.csv.
Getting Additional Coin Data
CryptoCompare’s API has an additional coin information method that we can use to help with our future analysis: CoinSnapshotFullById. You simply pass in a coin’s Id, and you’re returned a lengthly list of information about that coin. For example if we wanted information on Litecoin (Id = 3808), we could use a get request like before:
req = requests.get('https://www.cryptocompare.com/api/data/coinsnapshotfullbyid/?id=3808').json()['Data']['General']
We use the Data>General keys to view the relevant information. When we print out Litecoin’s information we get something like this:
Which holds lots of valuable information for future analysis. Now we need to find a way to extract all of this information for each coin. Since we already have a list of ID’s from our first DataFrame, we can just iterate through this list with a for loop! Let’s create a function that takes a list of coins and executes this.
def getCoinDetails(coins): details =  for coinID in coins: try: req = requests.get('https://www.cryptocompare.com/api/data/coinsnapshotfullbyid/?id='+str(coinID)).json()['Data']['General'] del(req['Sponsor']) del(req['ImageUrl']) details.append(req) except: pass return pd.DataFrame(details) ids = list(getCoinList()['Id']) additionalInfo = getCoinDetails(ids)
Since not every coin has additional information, we need to use try-except statements to handle errors. If the request goes through we collect the information we need for that coin ID and append it to a list. When the for loop completes, we return a DataFrame with all the coins’ additional information! Please note that this code will take some time to run, since you are accessing the API hundreds of times.
Note: You can store this as a .csv file, I’m storing mine as ‘extra data.csv,’ but we will be merging the two dataframes.
Merging our DataFrames
Merging the two DataFrames is a fairly simple operation:
mergedData = pd.merge(getCoinDetails(), getCoinList(), how='outer', on= 'Id')
mergedData is now a DataFrame containing all of the information shared by coins with the same ID’s.
Other API Calls
CryptoCompare’s API has vast amounts of useful information when it comes to finding information on coins. Some of my favorites are:
Please leave a comment if you’d like me to delve into any more of these using Python or if you have any questions in general! Be on the lookout for an analytics lesson with CryptoCompare’s API!