Let’s learn how to get BitCoin price using CoinGecko api and Python?
Using bitcoin api
First thing I checked is how to use CoinGecko api. This is how I got url to api which will return my BitCoin price I need. There is also a possibility to try the link out directly in CoinGecko site.
I assigned api url as a python variable.
Next thing is to request this url using requests python library.
I’d like to load this as a json. Luckily Python offers dedicated json library. Loads function will do the job.
Last thing is to format requested string as I like the most.
import json import requests api_url = 'https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd' response = requests.get(api_url).text response = json.loads(response) response = response["bitcoin"]["usd"] print(f"Bitcoin price is currently {response} USD.")
While I was preparing this article the BitCoin price was 55083 USD. I wonder how it will change in the future. I hope it is much higher when you are reading this.