How to get Bitcoin prices using Python

As a follow-up from last week’s post about getting cryptocurrency prices using Google Spreadsheets, we can use Python as well to get prices via API endpoints. And this will give us much more flexibility to build our own tools later on.

For this example I’ll use a different website, in this case Coinbase API. The core concept is the same as before: You get data from an API and then work with such output as you see fit. We’ll use 2 Python libraries for this, requests and json.

  • Requests : This module is pretty straightforward, has a get() method which will pull data from that URL, in this case the Coinbase API endpoint for BTC-USD price. Even better, in order to get this data you don’t need to authenticate at all, as is a publicly accessible one:
  • JSON : This URL downloads a response.json file . It has a bit more information than the previous Spreadsheets example but the core principle hasn’t changed: You have a response that needs to be parsed, and filter only the data that you want to use. We’ll use the JSON module to convert the this response into a Python datatype that we can operate with.

Finally we just need to access to the parameters inside the data object:

data = response.json()
currency = data["data"]
price = data["data"]["amount"]

Full script:

def get_btc():
	response = requests.get("")
	data = response.json()
	currency = data["data"]["base"]
	price = data["data"]["amount"]
	print "Currency: " +  currency + " Price: " + price

Run the script in your command line and, voilá:

$ Currency: BTC Price: 6603.865

Curious about crypto? You’ll receive 9.15€ if you register and trade in Coinbase using this link.



Leave a Reply

%d bloggers like this: