0xPhillan

Posted on May 29, 2022Read on Mirror.xyz

Use Excel to Fetch Crypto Market Data with API Queries

Follow me on Twitter: @0xPhillan

In this piece I will teach you how to use Microsoft Excel to connect to APIs of blockchain data aggregation services to pull aggregate price, market cap, trade volume and TVL data. The skills learned here are transferable to any public API – be creative and show me what you make with it!

We will cover three APIs that will teach you the basics of using Excel Power Query for API calls:

  • CoinGecko API*: basic Power Query API call*
  • CoinMarketCap API*: basic Power Query API call with a personal API key*
  • DeFi Llama API*: advanced Power Query API call with parameters and data transformation in Power Query*

Contents

  • Part 1 Introduction
  • Part 2: Getting API Access
  • Part 3 CoinGecko – API & Querying in Excel
    • Part 3.1 CoinGecko – Finding the API Link
    • Part 3.2 Using Excel to Query the CoinGecko API
    • Part 3.3 Transforming the retrieved CoinGecko data
  • Part 4: CoinMarketCap – API & Querying in Excel
    • Part 4.1 CoinMarketCap – Finding the API Link
    • Part 4.2 Using Excel to Query the CoinMarketCap API
    • Part 4.3 Transforming the retrieved CoinMarketCap data
  • Part 5 DeFi Llama – API & Querying in Excel
    • Part 5.1 DeFi Llama – Finding the API Link
    • Part 5.2 Using Excel to Query the DeFi Llama API – /protocols
    • Part 5.3 Transforming the retrieved DeFi Llama data – /protocols
    • Part 5.4 Using Excel to Query the DeFi Llama API - /protocol/{protocol}
    • Part 5.5 Transforming the retrieved DeFi Llama data – /protocol/{protocol}
  • Part 6 Other Notes
    • Part 6.1 Refreshing Data
    • Part 6.2 Important note on API call URLs
    1. Closing thoughts

Part 1 Introduction

When we look at the blockchain data that is available today, we can see massive amount of data in various forms from various sources at various levels of granularity. We can find data on prices, market caps, TVL, NFT sales and even aggregations of specific smart contract interactions – all the data is public, and the volume of data available is incomprehensible.

While having more granular data means you can do deeper analyses, you also face the challenge of having to transform the data into a human-readable format. This is an extremely challenging task, but thankfully we have data aggregation services that do the heavy lifting for us.

Various levels of blockchain data publicly available

From above illustration you may feel that with aggregate data at token or smart contract level you will miss the big picture, but the truth is quite the opposite: with aggregate data you get to see the big picture and visualize trends without being distracted by every detail. If you start your data analysis journey with on-chain data, you'll miss the forest for the trees!

So for today, let's see how we can use the CoinGecko API, CoinMarketCap API and DeFi Llama API to fetch aggregate token and TVL data.

Part 2 Getting API Access

Before we get started, we need to make sure we have API access. Some APIs are accessible publicly, while others require sign-up and a personal API key. Today we will use below three public APIs:

  • CoinGecko (free, no sign-up required | optional paid plans)
  • CoinMarketCap (free, sign-up required | optional paid plans)
  • DeFi Llama (free, no sign-up required)

Table of popular aggregated blockchain data APIs as of May 28th, 2022

CoinGecko and CoinMarketCap have mostly the same data available, however we will be using both APIs to demonstrate how to use Excel to pass your API key details when querying an API which requires you to register for an account.

Whenever working with any API, the API documentation is an extremely valuable document that explains what data can be accessed through which API calls. Below are the latest links to the API docs for our three data sources:

Since CoinGecko and DeFi Llama APIs are free, we only need to sign up for CoinMarketCap. In below link click the "Get Your API Key Now" button, follow the steps on-screen to get access to your API key:

https://coinmarketcap.com/api/

CoinMarketCap API sign-up process

Once you have completed the process, you can view and copy your API key by hovering over the "API Key" box in the CoinMarketCap developer backend. Your CoinMarketCap API key should look something like this:

8d441bdd-dccf-5a50-ac45-8a880cc52c1a

Note: This API key will not work and is for illustration purposes only. You must apply for a personal API key if you want to use the CoinMarketCap API.

Part 3 CoinGecko – API & Querying in Excel

We will start off with CoinGecko since it is the easiest API to use.

Every API will have a special link you need to use initiate an API query. In below section we will first look at how to read API documentations to find the right links, before moving on to Excel and Power Query to fetch and transform the data. Some APIs provide you with the API link directly, while others require you to look around a little bit.

Part 3.1 CoinGecko – Finding the API Link

First let's open the API documentation again:

https://www.coingecko.com/en/api/documentation

If you scroll down should see something like this:

CoinGecko's API documentation

CoinGecko does not directly share the API link with you, however we can easily find it by searching through their API commands. If you expand the GET ribbon under "ping", you will see an option to try out the ping command.

CoinGecko /ping API endpoint

If you click "Try it out" and then "execute" you should see the following:

Results of CoinGecko /ping API call

Here we can see the following important information:

  • Request URL: https://api.coingecko.com/api/v3/ping – the link used to initiate the API call
  • Server Response:
    • Response code: 200 – a code which signifies the server response was successful
    • Response body: "gecko_says": "(V3) To the Moon!" – the API query response

When you execute an API query call, all of the call parameters are included in the request URL. Or phrased differently: based on what is included in the link, the API knows which data or information to return. You can test this by pasting the request URL in your browser and you will receive the same response:

Executing an API call in your browser

Let's do another API call, but this time using the /coins/markets call under the coins section. This will "List all supported coins price, market cap, volume, and market related data". Once you click "Try it out", you will see a long list of parameter names and input fields in which we can specify our query parameters.

CoinGecko /coins/markets API call

Apart from the field marked "required" which we must fill out (I put "usd"), we can leave the other fields as per default. Click the "Execute" button and wait for the API call. Once completed, you will see the below:

CoinGecko /coins/markets API call server response

In the response body we can now see a huge JSON response with a lot of data about the cryptocurrencies CoinGecko tracks. Awesome!

Next let's see how we can run this API call in Excel and pull the data into Excel.

Part 3.2 Using Excel to Query the CoinGecko API

From the server response from the previous step copy the request URL:

https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=100&page=1&sparkline=false

Then open a fresh instance of Excel and navigate to the data tab (1). Within the Data tab under “Get & Transform Data”, click on “From Web” (2):

Click "Data > From Web" in a fresh instance of Excel

Once you do this, the following pop-up window will appear. Paste the request URL into the box (1) and press "OK" (2):

Excel's basic API query "From Web" settings screen

If this is your first time connecting to the CoinGecko API, you will see a window like the below. Simply press connect.

Note: In below window I first connected to the ping API call, your link will be to /coins/markets.

Excel's anonymous access settings

You should see below screen as it connects.

Excel attempting the API call

Part 3.3 Transforming the retrieved CoinGecko data

Followed by the Excel Power Query editor automatically popping up, as seen below. The Power Query editor lets you further apply data transformations to your query.

Data is pulled into Excel Power Query

The Power Query editor follows Excel's general design language with a top ribbon with commands, and settings to the side.

First, let us rename our Query to something more readable and then let us convert the JSON response we received from CoinGecko into a Table:

Change Name of Query and convert data to table

You will see below pop-up; press OK to proceed.

"To Table" pop-up window delimiter options

Once you press OK, the Power Query editor will refresh and you will see a few changes applied to it. First, the header ribbon becomes activated and the "List Tools & Transform" ribbon disappears (1). Then, your raw JSON data has now been transformed into a Power Query table. This is added to the "Applied Steps" list on the right side (2) and can be reviewed in the formula bar above the list entries (3). Finally, although the data has been turned into a table, it still doesn't give us any valuable information. Currently Excel is showing you the highest level of data from the JSON response, which is just a list of records (4). We need to go one level deeper to see the data, and to do this we need to expand the records of Column1.

More Power Query options unlock once data is converted to table

To expand Column1, click on the two outward facing arrows (1) which will open a sub-menu with all the column headers of data available at the next level of depth. Simply press OK (2) to expand the column and refresh the table.

Preview of what data is included in all the "Records". Press OK to expand.

You will see another step has been added to your applied steps (1) and that the formula bar has also been updated with code reflecting the expand step (2). But most importantly, all the detailed data from our API call is now visible (3)!

From here you can decide to make more transformations to your data such as changing data types, removing columns you don't need or adding certain calculations. If you are happy with the data, as a next step press "Close & Load" (4). Since this is a new query, this will automatically load your Power Query table into a new Excel sheet as an Excel table.

Queried data is now in a human-readable format

The Power Query editor will close and you will be brought back to your Excel sheet. In the Queries and Connections panel and within the sheet itself you will see Excel attempting to fetch the data again. Simply wait up to a minute for the API call to fetch the data, and Power Query to apply the transformations.

Excel running the query and fetching the requested data

Once complete, your data will appear in Excel and the Queries and Connections tab will let you know how many rows of data have been loaded.

Latest data from our query appears in Excel in table format

That's it! From here on out you can use Excel to play with the data, transform the data, make charts, tables graphs or just browse the full extent of data you pulled from CoinGecko in table format. Congratulations!

Below is a simple charting example that only took a few seconds to produce with the data. Using a pivot table on the query result, we can easily create pivot charts that summarize the data in a visual manner.

Simple bar chart and pie chart using market cap data of CoinGecko's top 10 coins

You can follow these steps for other API links within CoinGecko's documentation to fetch other data.

Part 4: CoinMarketCap – API & Querying in Excel

While the process for CoinMarketCap will mostly follow that of CoinGecko, there is one key difference: CoinMarketCap requires an API key to access their API. If you followed the steps in Part 2, you should already have an API key at your disposal. KEEP YOUR API KEY PRIVATE AND SAFE. Your API key should not be shared with others, as in the future you may start paying for other API access, and anybody with your API key will be able to use your access.

Part 4.1 CoinMarketCap – Finding the API Link

Just like in part 3, let us open the API documentation for CoinMarketCap:

https://coinmarketcap.com/api/documentation/v1/

Here you can view all API calls. Let us, however, use an API call that requests similar data to the one we used earlier for CoinGecko, namely the "Listings Latest" call:

https://coinmarketcap.com/api/documentation/v1/#operation/getV1CryptocurrencyListingsLatest

If you click the top right GET command box, you can see the API call request URL. Copy it:

https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest

CoinMarketCap API Documentation for /v1/cryptocurrency/listings/latest API endpoint

The CoinMarketCap free API membership is the “Basic” API plan. Hence we can use all endpoints that support that plan.

Part 4.2 Using Excel to Query the CoinMarketCap API

This step will be slightly different to the previous step with CoinGecko, because we need to use an API key to access the data. If we look at the "Authentication" section of the CoinMarketCap API Documentation, we will find that CoinMarketCap explains that to connect to their services we must use a custom header and our API key. The custom header that CoinMarketCap recommends is:

X-CMC_PRO_API_KEY

CoinMarketCap API Documentation for API key authentication

With the custom header and API key at hand (log in to the CoinMarketCap Developers portal to find it, see Part 2 of this guide), let us navigate to the data tab (1) and under "Get & Transform Data", click on "From Web" (2) just like we did with CoinGecko:

Click "Data > From Web" to open the "From Web" query window

This time when the “From Web“ pop-up appears, click on "Advanced" (1) and input the request URL in "URL parts" (2), then add the HTTP request header (3) as well as your API key (4). Once all is input, press OK (5).

URL:

https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest

HTTP request header:

X-CMC_PRO_API_KEY

API key:

Log in to your personal CoinMarketCap Developer account and fetch from the dashboard

Excel's advanced API query "From Web" settings screen with header options

If this is your first time connecting to the CoinMarketCap API, you will see a window like the below we saw before for CoinGecko. Simply press OK.

Note: The link at the top will show your request URL for CoinMarketCap instead of CoinGecko as below.

Excel's anonymous access settings

You should see below screen as it connects.

Excel attempting the API call

Part 4.3 Transforming the retrieved CoinMarketCap data

The PowerQuery editor will automatically pop-up.

Data is pulled into Excel Power Query, and split into "status" and "data"

You will notice that it looks different to the CoinGecko screen – instead of “List Tools”, we are given a “Record Tools” ribbon at the top. This is because the CoinMarketCap API returns both the data we requested, as well as a record of our API call request (“status”), which it uses to track how much data we have pulled. In the CoinMarketCap API, there is a daily limit of how much data can be requested and is measured in credits. If you look at the CoinMarketCap developer dashboard, you will see with every API call your credits used will increase.

The above API call used 5 credits:

CoinMarketCap Developer Dashboard credits used

To find the data we want from the Power Query editor, click on "List".

Click on "List" to expand underlying data entries in Power Query

The result will be a table that looks similar to our previous table from CoinGecko. From here on out all steps are the same as with CoinGecko:

Press "To Table"

A list of records appears in Power Query

Then click OK on the pop-up:

"To Table" pop-up window delimiter options

In the resulting column, click the two outward facing arrows at the top of the column (1), then press OK (2).

Preview of what data is included in all the "Records". Press OK to expand.

As with CoinGecko, all CoinMarketCap data will be expanded. Again, click "Close & Load" to load this data as a table into Microsoft Excel.

Queried data is now in a human-readable format

You will see the Query running while Excel attempts to fetch the data.

Excel running the query and fetching the requested data

Once the API query call is complete, you will see all data in table format in your Excel.

Latest data from our query appears in Excel in table format

If you have been paying attention, you may have noticed that we did not rename our query this time. If you need to make any edits to your query, including any transformation steps within the query or the query name, simply double-click on the query on the right side to re-open the Power Query editor.

Double-clicking the Query in the "Queries & Connections" panel re-opens Power Query

From here we can now change the name (1) and hit "Close & Load" (2) to save the changes to our query and reload it into our Excel.

Change the query name in Power Query and hit "Save & Close"

The updated name will be reflected accordingly. If you make any other changes to your query, they will also be updated.

Refreshed Excel table

Part 5 DeFi Llama – API & Querying in Excel

Unlike the CoinMarketCap API, the DeFi Llama API is free. Hence, this process will much more closely resemble that of CoinGecko. However this time, we will run two Power Query API calls. We want to:

  1. Get a full list of all protocols tracked by DeFi Llama
  2. Get detailed timeseries data for a specific protocol tracked by DeFi Llama

Part 5.1 DeFi Llama – Finding the API Link

Again, let's open the API documentation. This time for DeFi Llama:

https://defillama.com/docs/api

In your browser you should see the below screenshot. Here we can see a list of various Total Value Locked (TVL) data retrieval API calls. If we use the first call command /protocols, we will get a table similar to that of CoinGecko and CoinMarketCap which lists all the protocols and related information. From there we can find which protocol we want more detailed information for, and then use the /protocol/{protocols} API call to retrieve that information.

In summary, the steps we must undertake are:

  1. Get the protocol list using the /protocols API command
  2. Within the data retrieved from the /protocols API command identify the "slug" (unique identifier) of the protocol that we want to retrieve our timeseries data for (this is a requirement listed in the /protocol/{protocols} API call)
  3. Use the /protocol/{protocols} command to get the timeseries data for our selected protocol

DeFi Llama API documentation

Same as with CoinGecko, we can expand the /protocols API call command and try it out on the website directly. This will show us the request URL we need to use in Excel, as well as show us the format of the data.

DeFi Llama API server response for calling /protocols API endpoint

The request URL we need is:

https://api.llama.fi/protocols

Just like with CoinGecko, we can observe the data returned. This can be helpful if you want to understand what data an API request will return. From the above screenshot, we can already see the slug for MakerDao: "makerdao". We could skip doing the first API call from Excel and go straight to the second API query. However, we want to have a full list of all query-able slugs so that we can change our query in the future if we want to. For this we must still do the first API call requesting the full protocol list.

Part 5.2 Using Excel to Query the DeFi Llama API – /protocols

Open Excel, and once again navigate to the data tab (1). Within the Data tab under "Get & Transform Data", click on "From Web" (2):

Click "Data > From Web" to open the "From Web" query window

In the pop-up window, paste the retrieval URL https://api.llama.fi/protocols then press OK.

Excel's basic API query "From Web" settings screen

If you are asked to apply Access Web content settings, leave it with the default setting and press connect.

Excel's anonymous access settings

A connecting pop-up will appear indicating that Excel is attempting the API call.

Excel attempting the API call

Part 5.3 Transforming the retrieved DeFi Llama data – /protocols

Once the API call was successful, the Power Query editor will open automatically once again, resembling our first CoinGecko API call. Once again, let us click "To Table" to convert the retrieved data to a table format.

Data is pulled into Excel Power Query as a list of records

When the "To Table" pop-up appears, keep all settings at default and click OK.

"To Table" pop-up window delimiter options

Once complete, click the two outward facing arrows (1) then click OK (2) to expand the underlying columns.

Preview of what data is included in all the "Records". Press OK to expand.

The result will be the full expanded table of all protocols listed on Defi Llama. Click "Close & Load" to load the data as a table into a new sheet.

Queried data is now in a human-readable format

Excel will now work to retrieve the data from DeFi Llama.

Excel running the query and fetching the requested data

Once retrieved, you will have a data table including information of protocols with key data that is listed on DeFi Llama.

Latest data from our query appears in Excel in table format

From here we can navigate to the "slug" column to see all the slugs that DeFi Llama uses (press CTRL+F on Windows to search for the keyword "slug" to find it quickly).

"Slug" column with all the unique identifiers for each protocol listed on DeFi Llama

Part 5.4 Using Excel to Query the DeFi Llama API - /protocol/{protocol}

Now that we have a full list of available slugs that we can refresh whenever we want, let us pick a slug. For this example I have picked "makerdao".

Let us navigate back to the API documentation https://defillama.com/docs/api and take a closer look at the /protocol/{protocol} command.

Again, we can try the API call directly on the DeFi Llama website first to get a feeling for the kind of data we will receive. Let us input "makerdao" into the protocol slug field and press execute.

DeFi Llama API Documentation for /protocol/{protocol} API endpoint

In the resulting server response we can see a complex JSON data structure.

DeFi Llama API server response for calling /protocols/{makerdao} API endpoint

At the highest level we receive more details about the protocol, and if we scroll further down the server response, we can also see what other data is included in the API response.

DeFi Llama API server response for calling /protocols/{makerdao} API endpoint

From our previous API call https://api.llama.fi/protocols that used the /protocols command, we can guess that the API address for this call should now be

Adding everything together the address we need to use is: https://api.llama.fi/protocol/makerdao

Again in Excel we click the "Data" (1) tab and select "From Web" (2).

Click "Data > From Web" to open the "From Web" query window

In the pop-up, paste the URL we just created:

Excel's basic API query "From Web" settings screen

A connecting pop-up will appear indicating that Excel is attempting the API call.

Excel attempting the API call

Part 5.5 Transforming the retrieved DeFi Llama data – /protocol/{protocol}

Since I've worked with API before, I know to access "tvl" to get the total MakerDAO TVL over time. However, when you work with APIs you have not yet worked with before and the API documentation isn't very descriptive, you may need to click around the API responses to find what you are looking for.

In the result we can see all details of the MakerDAO protocol that DeFi Llama provides through their API as of this moment in time. From here we click on "record" next to "tvl" to expand that entry.

API call response from /protocol/{protocols} endpoint

From here we transform these entries into a table by clicking on "To Table".

List of records ready to be converted into a Power Query table

When the below pop-up appears, click OK.

"To Table" pop-up window delimiter options

We must once again click the outwards facing arrows (1) on our column of data to open the sub-menu, then click OK (2) to expand the data.

Preview of what data is included in all the "Records". Press OK to expand.

We can now see data and TVL data or phrased differently, a timeseries of Total Value Locked in MakerDAO. But you may have noticed that the dates currently do not resemble dates. That is because the dates have been returned as UNIX timestamps, which count seconds from January 1st, 1997 onwards. Excel does not recognize this time format. If we click the cell format dropdown (1) and convert this data to date format (2), then…

Column format sub-menu

We receive an error. Excel is not capable of directly transforming from a UNIX timestamp to an Excel time format. To do this, we need to manually calculate what the Excel time equivalent would be.

First, undo the last action by clicking on the "X" next to "Changed Type" in the "Applied Steps" window on the right.

Converting UNIX time using the Power Query "Date" format leads to errors

Now that we have returned to our previous view, select the "Add Column" tab and click "Custom Column".

Add a custom column for custom calculations and formulas

This will open the Custom Column editor where we can input a custom formula. Power Query formulas are different to Excel formulas, so if you want to do complex calculations, please refer to the Power Query formula documentation by Microsoft: https://docs.microsoft.com/en-us/powerquery-m/power-query-m-function-reference

What we must do now is write a custom formula that converts the timestamp from UNIX to Excel format. UNIX time counts time in seconds from January 1st, 1970, while Excel counts time as days from January 1st, 1990. So to convert the time, we must take UNIX time and multiply by the amount of seconds within a day (86,400), then add the number of days from January 1st 1900 to January 1st 1970 (25,569).

Let's add the formula below and click OK.

Custom column formula editor

A new column has been added with numbers, but these numbers still don't look like dates. The values are correct, but the format is not.

Custom column is added to the Power Query table

Hence as a next step, we need to change the format to "Date".

Column format sub-menu

And there we go, now our data looks alright.

Converting the custom column using the "Date" format does not lead to errors anymore

Now let's clean up our table by removing the first column which is now obsolete. To do this right click on the column header (1) and then click "Remove" (2).

Right click the column header to unveil further column options

And then let's move the new date column to the left (simply drag it to the left).

Column re-organization is a simple drag-and-drop action

Now our data is ready to be loaded into an Excel sheet. Click "Close & Load".

Queried data is now cleaned up and ready for import to our Excel sheet

Now the timeseries TVL data for MakerDAO has been loaded into Excel as a data table. Congratulations!

Latest data from our query appears in Excel in table format

From here we could, for example, use the data to add a line chart to visualize MakerDAO's TVL over time.

Simple timeseries line chart using DeFi Llama's TVL data on MakerDAO

Part 6 Other Notes

Here we will briefly cover how to refresh data after you have built a query, and share some other important notes on API call URLs.

Part 6.1 Refreshing Data

To refresh this data when you open your Excel file in the future, simply navigate to the Data tab and click "Refresh All" (1).

Refresh all runs all data queries

Another quick way is to just right click anywhere within your table (1) and click "Refresh" (2).

Refreshing a single table runs only the data query of that table

Alternatively, if you only want to refresh a specific query, you can click anywhere in the data table (1) then navigate to the data tab (2) and click the down arrow under the "Refresh All" button (3) and click "Refresh" (4).

Refreshing a single table runs only the data query of that table

Or within the data tab (1) you can open the Queries & Connections panel (2), right click on your specific query (3) and click "Refresh" (4).

Refreshing a single query runs only that data query and updates and related tables

Part 6.2 Important note on API call URL

In our first CoinGecko example we used the below API call link provided by the CoinGecko API documentation:

https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=100&page=1&sparkline=false

This link is actually comprised of two parts, namely the base URL and the API call parameters:

The first parameter is prepended by a question mark (?), while every other parameter is prepended with an ampersand (&). Each parameter consists of the parameter name (left of the =) and the parameter setting (right of the =). You can change these parameters as needed for your specific API call. When manually building API call URLs, make sure that you include the required parameters as stated in the API documentation, otherwise your API call will return an error.

Error codes and their meanings are usually listed in API documentations as well to help you with troubleshooting.

7. Closing thoughts

From here on out you can use multiple API queries to fetch and update information at regular intervals and create entire dashboards of only the data that is important to you. Gone are the days where you have to click through multiple websites and multiple pages to see the data that you care about – instead you can build it all in Excel!

If you have previous experience with Excel, you can combine these data fetching techniques with other Excel data transformation techniques to combine queries from multiple APIs to get a strong view on the crypto market. For example, you could combine CoinGecko's or CoinMarketCap's AAVE token pricing, market cap and volume details with DeFi Llama's TVL data to get an immediate overview of how the AAVE token is performing.

If you found this helpful, please consider sharing this and following me on Twitter @0xPhillan. And if you have any questions, feel free to DM me on Twitter!

Thanks, and have a great day!