CoinGecko API: Getting Crypto Values
A use case for the CoinGecko API
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 GitHub.
Prerequisites
To use the CoinGecko API you first need to create an account on their website.
Then, you need to create an API token. The free plan currently allows you to make 10k API calls per month.
Requirements
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
Getting started
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 here):
The user inserts the email and the API KEY via CLI.
The program checks if the
settings.py
file exists in the settings folder., thenIf 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.
Structure of the main file
After the first usage, always invoke the main file via $ python3 main.py
.
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
The authentication function authenticates the user by verifying their email che API token.
This is a use case of the GET/ping
method shown in the documentation.
This method has no parameters and returns 200 if the authentication is successful.
In Python, we can test it as follows:
Were the authentication is performed from the email and the API token imported from the settings.py
file like so (complete code reference here):
The price
module
price
moduleThis module 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 API documentation).
Here'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:
The function price_change()
calls the API using the GET/coins/{id}/market_chart
method (for more details on this method, refer to the API documentation).
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]
The cvs_creation
module
cvs_creation
moduleThis module has two functions:
The 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.
Using the main file
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:
Last updated