This tutorial will show you how to get real-time cryptocurrency prices with Python. You’ll use the pycoingecko library, a Python wrapper for the CoinGecko API for retrieving real-time cryptocurrency prices. CoinGecko is a data analysis platform for the digital currency market that provides real-time and historical cryptocurrency information.
The following pip
command installs the pycoingecko
library for Python.
Installing Pycoingecko Library
pip install pycoingecko
List All Supported Cryptocurrencies
To begin using pycoingecko, you need to import the CoinGeckoAPI
class from the pycoingecko
module. Then, call the get_coins()
method to see a list of all CoinGecko supported cryptocurrencies. We’ll demonstrate by displaying the first 10 cryptocurrencies using the following script.
from pycoingecko import CoinGeckoAPI
import pandas as pd
cg_client = CoinGeckoAPI()
all_currencies = cg_client.get_coins()
pd.DataFrame(all_currencies)['id'].head(10)
Output:
0 bitcoin
1 ethereum
2 tether
3 usd-coin
4 binancecoin
5 ripple
6 binance-usd
7 dogecoin
8 cardano
9 matic-network
Name: id, dtype: object
Similarly, the get_supported_vs_currencies()
method returns a list of all destination currencies.
from pycoingecko import CoinGeckoAPI
import pandas as pd
cg_client = CoinGeckoAPI()
vs_curr = cg_client.get_supported_vs_currencies()
print(vs_curr)
Output:
['btc', 'eth', 'ltc', 'bch', 'bnb', 'eos', 'xrp', 'xlm', 'link', 'dot', 'yfi', 'usd', 'aed', 'ars', 'aud', 'bdt', 'bhd', 'bmd', 'brl', 'cad', 'chf', 'clp', 'cny', 'czk', 'dkk', 'eur', 'gbp', 'hkd', 'huf', 'idr', 'ils', 'inr', 'jpy', 'krw', 'kwd', 'lkr', 'mmk', 'mxn', 'myr', 'ngn', 'nok', 'nzd', 'php', 'pkr', 'pln', 'rub', 'sar', 'sek', 'sgd', 'thb', 'try', 'twd', 'uah', 'vef', 'vnd', 'zar', 'xdr', 'xag', 'xau', 'bits', 'sats']
As you can see from the above output, you can convert cryptocurrencies into various other FIAT currencies, and cryptocurrencies.
Get Real-Time Price for a Single Cryptocurrency
To get the latest price for a single cryptocurrency, you need to import the CoinGeckoAPI
class from the pycoingecko
module, and call its get_price()
method. You need to pass the name of your source cryptocurrency in the string format to the get_price()
method’s ids
parameter. The name of the destination currency is passed to the vs_currencies
parameter.
For example, the following script returns the real-time price of Bitcoin in USD. The output returns a dictionary where the dictionary key corresponds to the source currency, while the corresponding value shows the destination currency name and value.
from pycoingecko import CoinGeckoAPI
crypto_currency = 'bitcoin'
destination_currency = 'usd'
cg_client = CoinGeckoAPI()
prices = cg_client.get_price(ids = crypto_currency,
vs_currencies = destination_currency)
print(prices)
Output:
{'bitcoin': {'usd': 17492.13}}
You can retrieve the integer value of a cryptocurrency using a script like this:
print(prices[crypto_currency][destination_currency])
Output:
17492.13
Code More, Distract Less: Support Our Ad-Free Site
You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.
Get Real-time Prices for Multiple Cryptocurrencies
To retrieve cryptocurrency prices for multiple types of cryptocurrency, you need to pass a comma separated list of source cryptocurrencies to the ids
attribute of the get_price()
method. Similarly, passing comma separated list of destination currencies to the vs_currencies
attribute returns prices in multiple destination currencies.
For example, the following script returns prices for Bitcoin, Ethereum, and Dogecoin in USD and GBP.
from pycoingecko import CoinGeckoAPI
crypto_currency = 'bitcoin, ethereum, dogecoin'
destination_currency = 'usd, gbp'
cg_client = CoinGeckoAPI()
prices = cg_client.get_price(ids = crypto_currency,
vs_currencies = destination_currency)
print(prices)
Output:
{'bitcoin': {'usd': 17495.12, 'gbp': 14176.59}, 'dogecoin': {'usd': 0.087139, 'gbp': 0.07061}, 'ethereum': {'usd': 1274.34, 'gbp': 1032.62}}
You can also retrieve the price of one cryptocurrency in another cryptocurrency. For example, the code below returns Bitcoin and Dogecoin prices in Ethereum.
from pycoingecko import CoinGeckoAPI
crypto_currency = 'bitcoin, dogecoin'
destination_currency = 'eth'
cg_client = CoinGeckoAPI()
prices = cg_client.get_price(ids = crypto_currency,
vs_currencies = destination_currency)
print(prices)
Output:
{'bitcoin': {'eth': 13.727775}, 'dogecoin': {'eth': 6.837e-05}}
Get Cryptocurrency Prices after Regular Time Intervals
You can automate getting real-time cryptocurrency prices after regular intervals with a straightforward Python script.
For example, the following script incorporates a while
loop to retrieve real-time Bitcoin prices every two seconds. The Python time.sleep()
method adds a delay of two seconds to each while
loop iteration. (Note: This frequency is excessive since the CoinGecko API claims its endpoints are refreshed every 1 to 2 minutes, on average, but we’re presenting it every 2 seconds for demonstration purposes).
import time
from pycoingecko import CoinGeckoAPI
import datetime
cg_client = CoinGeckoAPI()
crypto_currency = 'bitcoin'
destination_currency = 'usd'
while True:
prices = cg_client.get_price(ids = crypto_currency,
vs_currencies = destination_currency)
print('Prices at', datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), ':', prices)
time.sleep(2)
Output:
Prices at 2022-12-15 15:24:57 : {'bitcoin': {'usd': 17489.7}}
Prices at 2022-12-15 15:24:59 : {'bitcoin': {'usd': 17489.7}}
Prices at 2022-12-15 15:25:02 : {'bitcoin': {'usd': 17489.7}}
Prices at 2022-12-15 15:25:04 : {'bitcoin': {'usd': 17533.85}}
Prices at 2022-12-15 15:25:06 : {'bitcoin': {'usd': 17533.85}}
Prices at 2022-12-15 15:25:08 : {'bitcoin': {'usd': 17533.85}}
In the same way, you can get real-time prices for multiple cryptocurrencies after regular intervals, as the following script demonstrates.
import time
from pycoingecko import CoinGeckoAPI
import datetime
cg_client = CoinGeckoAPI()
crypto_currency = 'bitcoin, ethereum, dogecoin'
destination_currency = 'usd, gbp'
while True:
prices = cg_client.get_price(ids = crypto_currency,
vs_currencies = destination_currency)
print('Prices at', datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), ':', prices)
time.sleep(2)
Output:
Prices at 2022-12-15 15:49:54 : {'bitcoin': {'usd': 17490.02, 'gbp': 14197.26}, 'dogecoin': {'usd': 0.087175, 'gbp': 0.070763}, 'ethereum': {'usd': 1274.85, 'gbp': 1034.84}}
Prices at 2022-12-15 15:49:56 : {'bitcoin': {'usd': 17490.02, 'gbp': 14197.26}, 'dogecoin': {'usd': 0.087175, 'gbp': 0.070763}, 'ethereum': {'usd': 1274.85, 'gbp': 1034.84}}
Prices at 2022-12-15 15:49:58 : {'bitcoin': {'usd': 17490.02, 'gbp': 14197.26}, 'dogecoin': {'usd': 0.087175, 'gbp': 0.070763}, 'ethereum': {'usd': 1274.85, 'gbp': 1034.84}}
-----------------------------------------------------------------------------
Prices at 2022-12-15 15:52:03 : {'bitcoin': {'usd': 17465.32, 'gbp': 14181.96}, 'dogecoin': {'usd': 0.087002, 'gbp': 0.070646}, 'ethereum': {'usd': 1272.94, 'gbp': 1033.64}}
Prices at 2022-12-15 15:52:05 : {'bitcoin': {'usd': 17465.32, 'gbp': 14181.96}, 'dogecoin': {'usd': 0.087002, 'gbp': 0.070646}, 'ethereum': {'usd': 1272.94, 'gbp': 1033.64}}
In addition to retrieving real-time cryptocurrency prices, you can programmatically retrieve a lot of other useful information about cryptocurrencies using pycoingecko. When you’re ready to pull more than prices, refer to this great CoinGecko API Python Tutorial.
Code More, Distract Less: Support Our Ad-Free Site
You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.