CoinGecko API: Getting Crypto Values
A use case for the CoinGecko API
Last updated
A use case for the CoinGecko API
Last updated
This section describes a use case for the CoinGecko API.
This use case has been developed by me in Python and the full code is on my .
To use the CoinGecko API you first need to create an account on .
Then, you need to create an . The currently allows you to make 10k API calls per month.
The program uses the match
statement that, at the time of writing this documentation (November 2023) requires Python 3.10 (or newer versions).
As the program calls an API, you also need the requests
library. You can install it via:
$ pip install requests
When you use this software you need to launch the initial.py
file via $ python3 initial.py
. This will create a file, called settings.py
, which stores your email and your API token: this way, every time you use this software, you won't need to insert them
After that, you can launch the main file via $ python3 main.py
.
Here's how the initial.py
file works (code reference ):
The user inserts the email and the API KEY via CLI.
The program checks if the settings.py
file exists in the settings folder., then
If exists, it warns the user that the file already exists.
If it doesn't exist, it creates it and stores the email and the API KEI inserted by the user.
Here's what it does:
It imports the email and the API token from the settings.py
file.
It prints a welcome message reporting the version of the software.
It tries to authenticate the user.
With a while
loop, it has a menù that gives the user to choose between three options:
Option 1: the user can visualize the price of one crypto in a currency of their choice. Also, the user can choose to store the value of the crypto in a CVS file with the current date.
Option 2: the user can visualize the difference between today's and yesterday's price of a crypto.
Option 0: it closes the program by breaking the loop.
Here we document all the functions used:
The authentication function authenticates the user by verifying their email che API token.
This method has no parameters and returns 200 if the authentication is successful.
In Python, we can test it as follows:
price
moduleHere's how this method can be used to show the current price of one crypto with respect to a currency:
For example, suppose we want to know the current price of bitcoin in USD. Here's what you'll see:
Here's how this method can be used to calculate the current price of a crypt with respect to a currency and its yesterday's price. We can also calculate the change in price during the day and print it:
For example, suppose we want to know the price of Ethereum in EUR. We'd get the following:
Note that the response to the GET
call is the following:
This is why the slicing used is:
current_value = data["prices"][1][1]
yesterday_value = data["prices"][0][1]
cvs_creation
moduleThe function new_csv()
creates a CSV writing the header, if it doesn't exist.
The function write_data()
writes the current value of a crypto in a currency on the current date.
The main file is built as a menu with two options:
Option 1 writes the current value of a crypto and stores its value in the CVS, if the user wants-
Option 2 prints the difference in the value of a crypto between today and yesterday.
Let's show how the software is expected to work:
After the , always invoke the main file via $ python3 main.py
.
This is a use case of the GET/ping
method shown in the .
Were the authentication is performed from the email and the API token imported from the settings.py
file like so (complete code reference ):
has two functions that retrieve the price of a currency using the API and make some calculations.
The function visualize_price()
calls the API using the GET/simple/price
method (for more details on this method, refer to the ).
The function price_change()
calls the API using the GET/coins/{id}/market_chart
method (for more details on this method, refer to the ).
has two functions: