Home » Displaying the bitcoin price using the coindesk API

Displaying the bitcoin price using the coindesk API

by hotcoin71

In this article we will get the price of Bitcoin using the coindesk bpi api, this is  a free api that also does not require that you signup and have to use a key.

The CoinDesk Bitcoin Price Index (XBP) represents an average of bitcoin prices across leading global exchanges that meet criteria specified by the XBP

You can visit https://api.coindesk.com/v1/bpi/currentprice.json

This is the output you will see, something similar to this

{“time”:{“updated”:”Jan 23, 2021 21:09:00 UTC”,”updatedISO”:”2021-01-23T21:09:00+00:00″,”updateduk”:”Jan 23, 2021 at 21:09 GMT”},”disclaimer”:”This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org”,”chartName”:”Bitcoin”,”bpi”:{“USD”:{“code”:”USD”,”symbol”:”$”,”rate”:”32,161.5533″,”description”:”United States Dollar”,”rate_float”:32161.5533},”GBP”:{“code”:”GBP”,”symbol”:”£”,”rate”:”23,507.1688″,”description”:”British Pound Sterling”,”rate_float”:23507.1688},”EUR”:{“code”:”EUR”,”symbol”:”€”,”rate”:”26,417.1461″,”description”:”Euro”,”rate_float”:26417.1461}}}

You can also visit https://api.coindesk.com/v1/bpi/supported-currencies.json to get a list of available FIAT currencies

You can then access this by using an endpoint like the folllowing : https://api.coindesk.com/v1/bpi/currentprice/.json

So you could access the Chinese Yuan by typing the following in https://api.coindesk.com/v1/bpi/currentprice/CNY.json

Install the requests library

Requests is a library for HTTP which makes accessing web sites and HTTP APIs easy. The simplest way to install requests is using PIP. To install requests run this command:

pip3 install requests

Testing the API

Put the following code in a file with a .py extension. I called mine bitcoinprice.py and it looks like this

import requests # http://docs.python-requests.org/en/master/
response = requests.get('https://api.coindesk.com/v1/bpi/currentprice.json')
data = response.json()
print(data)

Lets look through this example one line at a time

  • 1 imports the requests library we installed.
  • 2 calls the get function of the requests library and passes in the coindesk api  URL as a parameter. It assigns what is returned to a variable called response.
  • 3 calls the json() method on the response object we just created. This method returns the JSON we received in the API call. It assigns the results to a variable called data.
  • 4 prints the returned JSON data on the command line

Open a command line and run the script using your Python interpreter. Like this.

python bitcoinprice.py

This is what you should see

coindesk api json

coindesk api json

Get the Bitcoin price in various currencies

Now take the bitcoinprice.py and update is as follows

import requests # http://docs.python-requests.org/en/master/

response = requests.get('https://api.coindesk.com/v1/bpi/currentprice.json')

data = response.json()
#print(data)
print("1 Bitcoin is worth")
print(data["bpi"]["USD"]["rate"] + " Dollars")
print(data["bpi"]["EUR"]["rate"] + " Euros")
print(data["bpi"]["GBP"]["rate"] + " Pounds")

You will see that there are a few new lines added in

print(data["bpi"]["USD"]["rate"] + " Dollars")

We want to get the rate from the JSON that is returned and we want this in dollars, euros and pounds

You should see something like

C:\Python38-32>python bitcoinprice.py
1 Bitcoin is worth
31,980.4783 Dollars
26,268.4131 Euros
23,374.8194 Pounds

You may also like