Last day we learnt what is API and why we need API as a developer. And Today we will learn and use our first API in our script. Like we said we will send the request to the server in a predefined format and the server will return the information we want.
For the fist call we choose an API which do not need any credential to use it. This gives us the foreign exchange reference rates both current and historical data. It was published by European Central Bank. And you can learn all the requests from this API website.
For this post we will use Request library on Python to get the data from the server.
At first we import the libraries and check out the request format from the website.
Then we will send the request. using the get function and the predefined URL to get the data which we will parse and print.
From the printed result we can see the result is in Json format, which most of the API responses are. And the base currency is Euro. And also the data is latest, which we know from the date.
Let us find out the exchange rate of EUR to USD.
Now let us look for ways to customize our request and extract different data.
From the API website we see we can extract historical currency data by swapping the ‘latest’ with a date and we can change the base currency adding it to the URL with ‘?base=’ at the end.
like we will use the URL
From the result we can see the base currency is changed to USD and the date is changed to September 28th, 2015.
You can use this API to do a lot of projects. Like you get your script hosted on cloud which sends you currency change analysis on schedule like in a certain time everyday.