Non-Fungible Tokens (NFTs) have generated a lot of excitement over the last few years. Their use cases include digital art, music, event ticketing, virtual real estate, gaming etc. NFTs are bought and sold on NFT marketplaces such as OpenSea. Just like cryptocurrencies, their prices are determined by the economics of supply and demand. The key price metric is commonly referred to as the floor price, which is the lowest price at which a NFT is listed for sale within a given collection. Floor price gives a good indication of the popularity for a given NFT, which helps estimate its market demand and guide trading strategies.
NFT floor prices fluctuate on a daily basis following market cycles. It can often become difficult to keep track of trends for popular collections. Using monitoring tools like CoinGecko’s NFT Floor Price Tracker or apps like Floor might help, but they can be limited in customizability and functionality, tailored to personal preferences. Instead, we can take a different approach and make use of Python to build our own interactive NFT price tracker.
In order to get the NFT floor price data, we will leverage the CoinGecko API and call these commonly used endpoints:
- /nfts/list - List all supported NFT IDs
- /nfts/{id} - Get current data for a given NFT collection
- /nfts/markets - Data for all supported NFTs (paid exclusive endpoint)
- /nfts/{id}/market_chart - Historical data of a NFT collection (paid exclusive endpoint)
We will additionally be using the /ping endpoint to test the API connection.
Here’s a sneak peek of our NFT tracker in action:
How to Make API Calls in Python?
The simplest way to make API calls in Python and get NFT data is through the pycoingecko package, which can be installed via pip. However, as the repository hasn't been updated in a while, it might not support the latest changes to the API. In this guide, we will therefore write our own functions to make API requests and fetch the desired data. The schema for making requests is explained in the API documentation.
Before we begin, ensure that requests and json libraries are imported.
import requests as rq
import json
Let's start with some simple examples using a Jupyter notebook. The screenshot below shows a simple function which can be used to make a HTTP request to the CoinGecko API. In this case, we simply want to ping the server and verify that everything works as expected.
We check for the response code 200, which indicates that our request was successful.
A few things are worth mentioning in the above example. PUB_URL points to the public API, which is freely available with a keyed authentication. All you need to do is sign up and create a demo account. The key can then be generated using the developer dashboard.
The demo key is passed to our response function via the use_demo header.
Here's the code snippet you can use to retrieve your Demo API key:
How to Get NFT Floor Price Data with an API
The easiest way to fetch NFT floor price data via API is to use CoinGecko API’s /nfts/{id} endpoint, publicly accessible on the Demo plan.
To call this endpoint, we will require the relevant NFT ID. /nfts/list can be used to fetch a list of NFT IDs, and can be sorted according to market cap. Parse the response into a Pandas DataFrame to make it more readable.
Now that we know how to get the NFT ID, we can make use of the /nfts/{id} endpoint to fetch its floor price, amongst other NFT data. The following function makes this request and creates a dictionary with the relevant fields.
We can repeat this exercise for a number of NFT IDs by going through a for loop. In the end, the data can once be returned as a pandas DataFrame. The following example shows a function get_nft_floor_prices where we are able to fetch floor prices for top 10 NFTs in the descending order of market cap in USD.
How to Fetch Historical NFT Price Data in an Interactive App
Similar to fetching live NFT price data, you can call the CoinGecko API to pull historical NFT price data through the /nfts/markets and /nfts/{id}/market_chart endpoints. Historical price data can help to inform market trends. As these are paid exclusive endpoints, a Pro API key needs to be passed within a request header. An example structure is shown below:
use_pro = {
"accept": "application/json",
"x-cg-pro-api-key" : get_key()
}
We will now develop an interactive app using Python along with the Dash framework, instead of having the code sit within a Jupyter notebook. Dash provides an extremely versatile toolkit, which can be used to build apps rendered in the browser. Dash HTML components also enable a high degree of customization. For this demo, however, we’ll keep the dashboard simple and focus on two things:
- Fetch market data where number of results can be changed interactively and visualize results in a bar plot
- Fetch historical price data for a selected NFT and generate an interactive chart
We will make use of the Model View Controller (MVC) pattern for structuring our code. Model contains the logic used to fetch data and generate the plots. View contains the layout of the app. Controller forms the glue between the two containing the logic for callbacks, which are responsible for interactivity.
Let's start with defining the interactive components for our webpage. A drop down list of NFT ids can be created as shown below:
Here's an example of a radio button that we shall use:
The layout of the app would be structured by the following code:
The controller file contains the following logic for setting up and executing callbacks:
The app can be accessed via a port as specified below in the file app.py:
Running the app is rather straightforward and you can access the dashboard via the browser at http://127.0.0.1:7000/
poetry run python nft_tracker/app.py
How to Set Up NFT Trading Signals & Price Alerts
Set up NFT trading signals easily by leveraging CoinGecko API’s daily NFT floor price data – by calculating the simple moving average (SMA) and exponential moving average (EMA) for a given window size (larger than a day), we can examine long term trends and smoothen out the noise. For example, given a window size of 7 days, we can add a column SMA to our DataFrame as shown below:
df_hist['SMA'] = df_hist['Price_usd'].rolling(7).mean()
EMA places a greater weight on the most recent data points. It can be calculated as follows (window size = 7 days):
df_hist['EMA'] = df_hist['Price_usd'].ewm(span = 7).mean()
If the daily price is higher than SMA or EMA, it indicates an upward market trend. The converse is true when the daily prices fall below the SMA or EMA. These signals are shown in the plots on our dashboard.
We can also set up an email alert when the daily price crosses above or below the SMA. The function
send_price_alert
shown below takes the receiver email address, subject and body as the input arguments. It will create and send an email on behalf of the [email protected] account. A sender password is needed, which can be set up via: Google Account settings → Security → 2-Step Verification → App passwords → Enter a name and click on Create.
This can be used within the send_email function, where we specify the conditions under which an email alert should be triggered.
NFTs of interest can be selected from the drop down list in the app’s ‘Alert email’ mode.
Email alerts should be visible in your inbox:
Subscriber Benefits: Access Additional NFT & Crypto Data Endpoints
Crypto projects and developers seeking expanded capabilities with CoinGecko API can consider subscribing to our Analyst or Lite API plans to unlock exclusive paid endpoints. Beyond the endpoints covered earlier, here are some popular coin and NFT data endpoints:
- /nfts/{id}/tickers – Get the latest floor price and 24h volume of an NFT collection, on each NFT platform (eg. Blur, OpenSea).
- /nfts/{asset_platform_id}/contract/{contract_address}/market_chart – Get the historical market data of an NFT collection using a contract address. Market data includes floor price, market cap and 24h volume, by N days away from now.
- /coins/top_gainers_losers – Get the top 30 coins with largest price gain and loss within a specific duration.
- /coins/list/new – Get the latest 200 coins (id) listed on CoinGecko.
Enjoyed this tutorial? You might also appreciate this guide on building a crypto Twitter (X) bot in 4 easy steps.
Subscribe to the CoinGecko Daily Newsletter!