[Beginner] How to use the 'requests' library in Python to get information about cryptocurrency
Introduction
The requests
library in Python is a powerful tool in order to communicate with online resources and build useful applications. In this short tutorial I will show you have to get cryptocurrency data such as the conversion of Bitcoin to U.S. dollar in Python. Being able to programmatically get data regarding cryptocurrencies can be very useful and lead to more informed decisions regarding trading.
We will be using the Python programming language
Initial Setup
For this tutorial we will be using the Python programming language. Use of the language will be very minimal in order to get our desired result so you do not need to know very much about programming at all. To test that python is installed simply type python
in your system's terminal or command prompt and press enter. You should see something like this:
Python 2.7.13 (default, Dec 18 2016, 07:03:39)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
This is the Python interpreter you can use to test out various Python language features. Type quit()
and press enter to leave the interpreter.
Installing the requests
library
In order to grab data about a cryptocurrency we have to make a request to some website in order to grab that data. Every time you open up a browser and look for information you are making multiple requests. The requests that execute and grab information are called GET
requests and the requests where a user is posting data to a server such as login information or address information are called POST
requests, you can read more about these requests, you can read more about the different types of requests here.
To install the requests library for python, in a terminal or command prompt window run:
pip install requests
Grabbing the information
Many websites have these things called application programming interfaces (APIs) which allows people to interface with their data and grab specific information by making requests to their website. Luckily for us, CryptoCompare has a nice API which lets us grab accurate information regarding cryptocurrency prices by making requests.
Let's get started
Create a new file in any folder and call it get_bitcoin_price.py
open that file in your favorite text-editor, personally I use Vim. The first thing we're going to do is make the program aware of the requests library by importing it in, to do this we do:
# This is how you write a comment in python
# The below line imports the requests library
import requests
Finding the right endpoint to hit
We need to make a request to get the specific information we need in this case getting the US dollar or Euro conversion rate of Bitcoin. A quick glance at the documentation on the CryptoCompare API website shows us that the following endpoint will get us what we need:
https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=USD,EUR
Let's break this down, you might have noticed that there are often ?
in the URL address bar on some websites, this denotes the beginning of listing URL parameters. Think of going to this link, as running a function and the things after the question mark being the variables you pass into that function. In this case fsym is the from currency variable, and tsyms is the to currencies variable, and as you may be able to tell we are converting from Bitcoin to US dollar and EUR. Try pasting this link into a web browser and see what you get, you should get something like this:
{
USD: 971.57,
EUR: 887.61
}
What we just did was made a request in our browser to the API and received data regarding the current conversion rates for Bitcoin to USD and EUR. If we added INR to the tsyms list in our URL we could also get the Indian Rupee conversion rate.
Now that we have the right endpoint lets try grabbing in Python so we don't have to open a web browser every time.
Making a request in Python
Open get_bitcoin_price.py
again, we are now going to try to make a GET
request to the endpoint we learned about earlier. The file should now look like this:
# This is how you write a comment in python
# The below line imports the requests library
import requests
# make a GET request to the endpoint and store the response in a variable called response
response = requests.get("https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=USD,EUR")
# print the response text
print(response.text)
Save the file and close
Running the program
To run the program in your command prompt or terminal simply type:
python get_bitcoin_price.py
And you should get the following result:
{"USD":973.07,"EUR":886.96}
Conclusion
I hope this has been a very informative introduction in making requests to get useful data from an API for you, I highly recommend reading more regarding the CryptoCompare API, and utilizing its full ability to stay informed when trading. For those of you who are more technically advanced I definitely plan on doing more involved tutorials in the future, if you have any suggestions please comment below.
Cheers
~Np
Excellent tutorial on using APIs in Python programs, thanks!
I need to play with this as I have a Pi that could be showing me prices. Are you using Python 2.7? Any reason to not use 3?
I usually switch between the two depending on what libraries I'm using. Some libraries are only well supported on Python 2.7, here is a detailed page describing the benefits of one over the other. Thanks!
I've stuck with 3 for my personal projects and not had any issues. I expect there's less and less reasons to use 2 as things get ported.
This is an excellent tutorial, I've given you a follow mate.