How to Use a Bitcoin Price Python API

Bitcoin or Cryptocurrency has become a common language in financial circles. However, the way its price changes remains a puzzle even today. In other words, the prices are very volatile. In one second, a lot can happen in terms of price surging or crashing down. Therefore, an investor or user needs to be rightly equipped if they were to succeed in Bitcoin trading. The most basic pillar to understand and know, should be the Price projection or movement. Remember, it is next to impossible to predict precisely how the price of Bitcoin would behave at the end of any trading session. It is also very tiresome, time-consuming, and sometimes not realistic to keep on checking the latest prices on the voluminous sites all over the internet. To trade successfully, one needs to be on top when it comes to the latest Bitcoin prices. That is the reason a user has to use a system or App that gives all the latest BTC Prices. One of the best tools to use is the Bitcoin Price Python API.

What is Bitcoin Price Python API?

A Python Application Programming Language that interprets and monitors BTC Prices. It gives updates and notifications of the latest Bitcoin Prices. The process is very simple, looking at the BTC Price Data, Installing the Python Requests library, and finally, Selecting and Printing the BTC Price in USD. In this article, we will be highlighting the whole process from the data collection to the usage of the Bitcoin Price Python API.

We can achieve this by relying on Coindesk API and the Python Requests library. The reason is very simple; Coindesk has a BPI (Bitcoin Price Index) for calculating Average Prices across various Exchanges that are linked to it. It will enable the user to have a better understanding of average BTC Prices across many exchanges compared to one exchange. The good thing is that it is free and requires no authentication. Below is an in-depth breakdown of the entire process.

Retrieving the BTC Price Data from Coindesk API

To get the data, the user will have to open the below URL.

curl https://api.coindesk.com/v1/bpi/currentprice.json

The URL will retrieve the JSON Data that is framed in an Easy to Read Layout as formatted below.

1 {
2 "time":{
3 "updated":"Nov 18, 2020 05:31:00 UTC",
4 "updatedISO":"2020-11-18T05:31:00+00:00",
5 "updateduk":"Nov 18, 2020 at 05:31 GMT"
6 },
7 "disclaimer":"This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org",
8 "chartName":"Bitcoin",
9 "bpi":{
10 "USD":{
11 "code":"USD",
12 "symbol":"$",
13 "rate":"18,248.4250",
14 "description":"United States Dollar",
15 "rate_float":18248.425
16 },
17 "GBP":{
18 "code":"GBP",
19 "symbol":"£",
20 "rate":"13,760.7906",
21 "description":"British Pound Sterling",
22 "rate_float":13760.7906
23 },
24 "EUR":{
25 "code":"EUR",
26 "symbol":"€",
27 "rate":"15,375.6119",
28 "description":"Euro",
29 "rate_float":15375.6119
30 }
31 }
32 }

If the user wants to retrieve the price in USD, then running few commands on a Linux Prompt will be required.

To access the HTTP APIs and the sites easily, the user must request a Python Client Library. To launch the request easily, using PIP will be ideal, since it is a Python Package Installer that is compatible with most operating systems. If the user cannot get it in the Operating system being used, they can get it at get-pip.py.

After that, the user can install the Python Requests Library as illustrated below.

pip3 install requests. 

After getting the requests, the user can access the API by writing some code through this file, .the py extension. We are using the btc.py Python interpreter. It will look like below.

1 #!/usr/bin/env python
2 # This is my btc.py script.
3 import requests
4 response = requests.get('https://api.coindesk.com/v1/bpi/currentprice.json')
5 data = response.json()
6 print(data)

Let us elaborate on the above code

Line 1 shows the language interpreter used, which is Python.

Line2 is the users’ comment or command.

Line 3 is an importation of the earlier requests. It enables the user to access the code or script in the form of requests.

Line 4 initiates the get Function, to order by using URL as the edge. The result is a response that is assigned. The response is also known as the variable.

Line 5 inputs the JSON() Format to the created variable or response. As a result, the JSON triggers the creation of the Python dictionary that translates the outcome into data.

Line 6 prints out the data. The user should run the data on the Python Interpreter that in our case is btc.Py.

Python3 btc.py

Below is the resultant Script

{
"time":{
"updated":"Nov 18, 2020 06:40:00 UTC",
"updatedISO":"2020-11-18T06:40:00+00:00",
"updateduk":"Nov 18, 2020 at 06:40 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":"17,817.2600",
"description":"United States Dollar",
"rate_float":17817.26
},
"GBP":{
"code":"GBP",
"symbol":"£",
"rate":"13,429.9023",
"description":"British Pound Sterling",
"rate_float":13429.9023
},
"EUR":{
"code":"EUR",
"symbol":"€",
"rate":"15,005.9280",
"description":"Euro",
"rate_float":15005.928
}
}
}

Printing BTC USD Price

Finally, we can select the required data after updating the script or the code. The above data still needs refining by adding the “bpi” object. It will be possible by altering the last line of the code as below.

1 #!/usr/bin/env python
2 # This is my btc.py script.
3 import requests
4 response = requests.get('https://api.coindesk.com/v1/bpi/currentprice.json')
5 data = response.json()
6 print(data["bpi"])

We can still dig in and edit to include “USD” & “Rate” in the last line. It will lead to the below code which once ran would give the Latest Bitcoin Price Data.

1 #!/usr/bin/env python
2 # This is my btc.py script.
3 import requests
4 response = requests.get('https://api.coindesk.com/v1/bpi/currentprice.json')
5 data = response.json()
6 print(data["bpi"]["USD"]["rate"])

As you can see, we have created a functional BTC Price data update that uses the Python API. As previously stated BTC price keeps on fluctuating now and then, therefore, to get the latest Price it is important to use a BTC Price Python API. It is easy and simple to use. Let alone that it is compatible with most Apps. Finally, this simple BTC Price script can enable the user to integrate the Bitcoin Price on other platforms.