BeginnerCodingPython

Getting Started With APIs – Part 2

In our previous article, we looked at what APIs are and what they are used for. In this article, we will create a simple Python program that fetches some data from an API and displays it for us.

We will fetch some currency exchange rate data from an API. I chose exchange rate data because it is something of general interest in many situations and it changes frequently, so we can collect data from many different points in time and it is easy to understand.

We will use the API from ExchangeRatesAPI.io because the free plan allows us to fetch historical data (that is, exchange rates from years ago, not just the latest data). So first we create an account at ExchangeRatesAPI.io and get a free API Key.

JSON

Modern APIs will usually provide data in a format known as JSON.
JSON stands for JavaScript Object Notation. You can think of it as text that is a combination of nested lists and dictionaries.

Required Libraries

We will use the requests library to retrieve our data and then use matplotlib to draw a chart.
So if you have not installed them on your computer already, do so using the following commands:

pip install requests
pip install matplotlib

Writing the Program

Now we open our code editor and import the libraries we need

import requests
import matplotlib.pyplot as plt

Put our API key in a variable

my_key = “my_very_secret_key:-)”

The website tells us how to format the URL from which we will get data.
To get data for a particular date, we build our URL as follows:

my_url=”http://api.exchangeratesapi.io/v1/2022-06-15?access_key=”+my_key

Of course, our program will include our API key using our variable.

Now we use the requests library to actually request data from the API,
and we put the data we get in a variable that we call response.

response = requests.get(my_url)

Hopefully, our request succeeds and we get data back in our response variable.

Now we decode the JSON data and create a dictionary.

my_dict = response.json()

The API work is done and we have the data we wanted in a Python dictionary.

Now we need to see what the dictionary contains so that we can write code to read the data that we want.

print(my_dict)

This gives us a dictionary similar to the one below:

{‘success’: True, ‘timestamp’: 1686873599, ‘historical’: True, ‘base’: ‘EUR’, ‘date’: ‘2023-06-15’, ‘rates’: {‘AED’: 4.020203, ‘AFN’: 95.768652, ‘ALL’: 108.047527, ‘AMD’: 423.09078, ‘ANG’: 1.974017, ‘AOA’: 771.664273, ‘ARS’: 271.969319, ‘AUD’: 1.590929, ‘AWG’: 1.972942, ‘AZN’: 1.862114, ‘BAM’: 1.975735, ‘BBD’: 2.211514, ‘BDT’: 118.566312, ‘BGN’: 1.955594, ‘BHD’: 0.412651, ‘BIF’: 3108.547253, ‘BMD’: 1.094559, ‘BND’: 1.470457, ‘BOB’: 7.568468, ‘BRL’: 5.265597, ‘BSD’: 1.095367, ‘BTC’: 4.2789129e-05, ‘BTN’: 89.91324, ‘BWP’: 14.604557, ‘BYN’: 2.764734, ‘BYR’: 21453.354281, ‘BZD’: 2.207807, ‘CAD’: 1.447374, ‘CDF’: 2599.577405, ‘CHF’: 0.976188, ‘CLF’: 0.031478, ‘CLP’: 868.576114, ‘CNY’: 7.794386, ‘COP’: 4540.230284, ‘CRC’: 593.092317, ‘CUC’: 1.094559, ‘CUP’: 29.005811, ‘CVE’: 110.767138, ‘CZK’: 23.783701, ‘DJF’: 195.020391, ‘DKK’: 7.452197, ‘DOP’: 60.036308, ‘DZD’: 148.488214, ‘EGP’: 33.819024, ‘ERN’: 16.418383, ‘ETB’: 59.587725, ‘EUR’: 1, ‘FJD’: 2.404635, ‘FKP’: 0.862334, ‘GBP’: 0.856115, ‘GEL’: 2.851369, ‘GGP’: 0.862334, ‘GHS’: 12.422979, ‘GIP’: 0.862334, ‘GMD’: 64.962433, ‘GNF’: 9473.407221, ‘GTQ’: 8.581587, ‘GYD’: 231.660586, ‘HKD’: 8.562444, ‘HNL’: 27.057605, ‘HRK’: 7.635765, ‘HTG’: 152.793754, ‘HUF’: 373.211501, ‘IDR’: 16313.305725, ‘ILS’: 3.890687, ‘IMP’: 0.862334, ‘INR’: 89.658547, ‘IQD’: 1433.324869, ‘IRR’: 46253.320449, ‘ISK’: 149.308898, ‘JEP’: 0.862334, ‘JMD’: 169.148999, ‘JOD’: 0.776484, ‘JPY’: 153.535419, ‘KES’: 153.130492, ‘KGS’: 95.872728, ‘KHR’: 4507.393904, ‘KMF’: 497.282543, ‘KPW’: 985.111936, ‘KRW’: 1390.500264, ‘KWD’: 0.335909, ‘KYD’: 0.912823, ‘KZT’: 495.910438, ‘LAK’: 20019.482106, ‘LBP’: 16708.441214, ‘LKR’: 336.27293, ‘LRD’: 192.203727, ‘LSL’: 19.943151, ‘LTL’: 3.231947, ‘LVL’: 0.662088, ‘LYD’: 5.270323, ‘MAD’: 10.946139, ‘MDL’: 19.578544, ‘MGA’: 4887.205568, ‘MKD’: 61.614093, ‘MMK’: 2300.139493, ‘MNT’: 3798.768281, ‘MOP’: 8.829589, ‘MRO’: 390.757336, ‘MUR’: 49.85683, ‘MVR’: 16.806933, ‘MWK’: 1120.828595, ‘MXN’: 18.748589, ‘MYR’: 5.063395, ‘MZN’: 69.230773, ‘NAD’: 19.943322, ‘NGN’: 668.77519, ‘NIO’: 40.006138, ‘NOK’: 11.491999, ‘NPR’: 143.862835, ‘NZD’: 1.755612, ‘OMR’: 0.42137, ‘PAB’: 1.095266, ‘PEN’: 3.98255, ‘PGK’: 3.853141, ‘PHP’: 61.066515, ‘PKR’: 314.631085, ‘PLN’: 4.452066, ‘PYG’: 7922.940974, ‘QAR’: 3.984739, ‘RON’: 4.961199, ‘RSD’: 117.232733, ‘RUB’: 91.373514, ‘RWF’: 1253.269931, ‘SAR’: 4.105339, ‘SBD’: 9.122395, ‘SCR’: 15.44215, ‘SDG’: 658.376667, ‘SEK’: 11.608766, ‘SGD’: 1.463031, ‘SHP’: 1.331804, ‘SLE’: 24.660449, ‘SLL’: 21617.538357, ‘SOS’: 623.34455, ‘SRD’: 41.159858, ‘STD’: 22655.159157, ‘SVC’: 9.583698, ‘SYP’: 2750.109355, ‘SZL’: 19.942876, ‘THB’: 37.860899, ‘TJS’: 11.949936, ‘TMT’: 3.830956, ‘TND’: 3.366313, ‘TOP’: 2.565972, ‘TRY’: 25.892667, ‘TTD’: 7.43007, ‘TWD’: 33.568263, ‘TZS’: 2613.80642, ‘UAH’: 40.446956, ‘UGX’: 4041.780803, ‘USD’: 1.094559, ‘UYU’: 42.10483, ‘UZS’: 12560.062875, ‘VEF’: 2953639.788524, ‘VES’: 29.631276, ‘VND’: 25760.443521, ‘VUV’: 129.856769, ‘WST’: 2.963242, ‘XAF’: 662.652115, ‘XAG’: 0.045838, ‘XAU’: 0.000559, ‘XCD’: 2.9581, ‘XDR’: 0.8215, ‘XOF’: 663.851026, ‘XPF’: 119.853865, ‘YER’: 273.907364, ‘ZAR’: 19.952769, ‘ZMK’: 9852.341387, ‘ZMW’: 21.139584, ‘ZWL’: 352.447517}}

Our interest is the nested dictionary called ‘rates’ which contains currency codes and the exchange rates on the date we selected.

To get the ‘rates’ dictionary, we use:

my_dict[‘rates’]

and to get the ‘KES’ rate within the ‘rates’ dictionary, we use:

my_dict[‘rates’][‘KES’]

Conclusion

And there we have it! We have written a simple program that connects to an API, gets JSON data, converts it to a dictionary and extracts the specific information we want.

In our next article, we will look at getting data for multiple dates, so that we can generate a chart showing a trend.