This notebook demonstrates basic functionality offered by the Coin Metrics Python API Client using Coin Metrics Community Data.
Coin Metrics offers a vast assortment of data for hundreds of cryptoassets. The Python API Client allows for easy access to this data using Python without needing to create your own wrappers using requests and other such libraries.
Prerequisites
First, Python must be installed. Download and install from python.org. The Coin Metrics API Client is best used with Python 3.8 or later.
Then, install the Python API Client:
pip install coinmetrics-api-client
Some of the optional libraries such as pandas, numpy, and seaborn are used in the notebook to make the examples more interactive. These libraries are not required to use the Coin Metrics API Client.
You are now ready to run the code in the rest of the notebook.
Resources
To understand the data that Coin Metrics offers, feel free to peruse the resources below.
The Coin Metrics API v4 website contains the full set of endpoints and data offered by Coin Metrics.
The Coverage Tool shows what assets, metrics, and other data types are covered.
Setup
from os import environimport sysimport pandas as pdimport numpy as npimport seaborn as snsimport loggingfrom datetime import date, datetime, timedeltafrom coinmetrics.api_client import CoinMetricsClientimport matplotlib.pyplot as plt%matplotlib inline
# We recommend privately storing your API key in your local environment.# Uncomment below if you have an API Key. Otherwise we will use Community data.# try:# api_key = environ["CM_API_KEY"]# logging.info("Using API key found in environment")# except KeyError:# api_key = ""# logging.info("API key not found. Using community client")client =CoinMetricsClient()# client = CoinMetricsClient(api_key)
The Coin Metrics API contains two types of catalog endpoints (Python client functions in paranthesis): the catalog (catalog_*_v2) and catalog-all (catalog_full_*_v2).
The catalog endpoint displays the set of data available to your API key. The catalog-all endpoint displays the full set of data for our dataset.
Catalog objects return a list of dictionaries. For catalog_asset_metrics_v2, each element of the list is an asset, while each dictionary is a set of metadata for that specific asset.
print(f"Asset Metrics Catalog metadata includes: {list(asset_metrics_catalog[0].keys())}")for i in asset_mapping:print(f"Asset {asset_mapping[i]} has {len(asset_metrics_catalog[i]['metrics'])} metrics in catalog.")print(f"Asset {asset_mapping[i]} has {len(full_asset_metrics_catalog[i]['metrics'])} metrics in catalog-all.")
Asset Metrics Catalog metadata includes: ['asset', 'metrics']
Asset btc has 147 metrics in catalog.
Asset btc has 696 metrics in catalog-all.
Asset eth has 146 metrics in catalog.
Asset eth has 699 metrics in catalog-all.
Asset Metrics Catalog metadata includes: ['asset', 'metrics']
Asset btc has 147 metrics in catalog.
Asset btc has 513 metrics in catalog-all.
Asset eth has 146 metrics in catalog.
Asset eth has 500 metrics in catalog-all.
For more details on what metrics are covered, see Coverage
Getting Timeseries Data
Next, we will pull timeseries data. Typically there are two types of timeseries data that you can pull: raw observations such as trades and aggregated metrics. We will explore these two below.
Asset Metrics
First, we will use the asset-metrics endpoint to get metrics for BTC and ETH.
btc_metrics = [m['metric']for m in asset_metrics_catalog[0]['metrics']]eth_metrics = [m['metric']for m in asset_metrics_catalog[1]['metrics']]
The other common timeseries data type that you will encounter are individual observations.
First, we will need to familiarize ourselves with the market convention which we can find on faqs. You can see a full list of markets by using the reference-data endpoint.
The Python API Client is often used for transforming data for State of the Network. Below are some examples of data transformations done to produce the data visualizations.
Example 1: Get returns by coin in the CM reference rates universe over the last 10-years
In State of the Network #128, we looked at the returns for each asset dating back the last 10 years.
We can generate this data by weaving in the catalog_asset_metrics_v2 and get_asset_metrics endpoint. The code snippets below demonstrate how to do this with a small list of assets.
# Get all assets that have a reference rate assets_refrate = client.catalog_asset_metrics_v2(metrics="ReferenceRateUSD")# Get list of assets with daily ref rate # uncomment the top line to look at *every* asset with reference rates# asset_with_ref_rates = assets_refrate[0]["frequencies"][0]["assets"]asset_with_ref_rates = ['btc','eth','bnb','ada','doge','xrp']#Query API for prices, daily CM reference rates as dataframemetrics ="ReferenceRateUSD"frequency ="1d"logging.info("Getting prices...")df_prices = client.get_asset_metrics( assets=asset_with_ref_rates, metrics=metrics, frequency=frequency, start_time=start_time, end_time=end_time).to_dataframe()# Assign datatypesdf_prices["time"]= pd.to_datetime(df_prices.time)df_prices["ReferenceRateUSD"]= df_prices.ReferenceRateUSD.astype(float)# Reshape dataset so assets are in columns, dates are the rows, and the values are pricesdf_prices_pivot = df_prices.pivot( index="time", columns="asset", values="ReferenceRateUSD")# Index each asset's time series to 1 for col in df_prices_pivot.columns: logging.info(f"Calculating Reference rate for {col}....")# First price in time series first_price = df_prices_pivot[df_prices_pivot[col].notnull()][col].iloc[0]# Index time series df_prices_pivot[col]= df_prices_pivot[col]/first_price# Fill forward for Null values df_prices_pivot[col]= df_prices_pivot[col].ffill()
2024-10-25 15:23:44 INFO Getting prices...
2024-10-25 15:23:45 INFO Calculating Reference rate for ada....
2024-10-25 15:23:45 INFO Calculating Reference rate for bnb....
2024-10-25 15:23:45 INFO Calculating Reference rate for btc....
2024-10-25 15:23:45 INFO Calculating Reference rate for doge....
2024-10-25 15:23:45 INFO Calculating Reference rate for eth....
2024-10-25 15:23:45 INFO Calculating Reference rate for xrp....
2024-09-13 16:56:10 INFO Getting prices...
2024-09-13 16:56:11 INFO Calculating Reference rate for ada....
2024-09-13 16:56:11 INFO Calculating Reference rate for bnb....
2024-09-13 16:56:11 INFO Calculating Reference rate for btc....
2024-09-13 16:56:11 INFO Calculating Reference rate for doge....
2024-09-13 16:56:11 INFO Calculating Reference rate for eth....
2024-09-13 16:56:11 INFO Calculating Reference rate for xrp....
df_prices_pivot.tail()
Example 2: Get daily spot trading volume on Coinbase for USDC markets
We can replicate similar data behind chart using just coinbase spot markets at 2021. Here, we derive volume from our get_market_candles endpoint.
candles_coinbase = client.get_market_candles( markets="coinbase-*-usdc-spot", # wildcards can be passed to get all asset pairs start_time="2024-01-01", end_time="2024-09-30", frequency="1d").to_dataframe()candles_coinbase["candle_usd_volume"]= candles_coinbase.candle_usd_volume.astype(float)candles_coinbase["time"]= pd.to_datetime(candles_coinbase.time)
2024-10-25 15:23:45 INFO Sleeping for a rate limit window because 429 (too many requests) error was returned. Pleasesee Coin Metrics APIV4 documentation for more information: https://docs.coinmetrics.io/api/v4/#tag/Rate-limits
candles_coinbase.head()
We can also break this down by month. Note that for this example, the volume numbers will look smaller because we are using fewer exchanges.