To do that, over inside Atom, we're going to kick things off by creating a const axios; we're going to require axios, and then we're going to make const for one of our two functions. This is for the first endpoint getExchangeRate. Now, getExchangeRate is going to take two pieces of information: the from currency code and the to currency code:
// USD CAD 23
// 23 USD is worth 28 CAD. You can spend these in the following countries:
const axios = require('axios');
const getExchangeRate = (from, to)
This is going to be a function. All we're going to do is use axios. That's axios.get, and we're going to pass in that URL that I just copied from the browser:
// USD CAD 23
// 23 USD is worth 28 CAD. You can spend these in the following countries:
const axios = require('axios');
const getExchangeRate = (from, to) => {
axios.get(`http://api.fixer.io/latest?base=USD)
}
Now we want to set the base equal to whatever currency we're coming from. So I can go ahead and use a template string, swapping out the static base value for a dynamic one, accessing from and then functions. We're going to use then real quick. We're going to use then here to just manipulate the value. This is going to return the promise from Axios with a bunch of information about the HTTP requests. The caller of getExchangeRate does not care about that. They shouldn't even know that an HTTP request was made. All they need back is a number, so that is exactly what we're going to give them.
In the then callback, we're going to have access to response, and on the response we're going to be able to get that currency code. We're going to return response.data. This gets us into the JSON object. Now, in here we have a rates object, which is key-value pairs, where the key is the currency, so we do want to access rates and we want to get the rate for whatever the to variable is:
// USD CAD 23
// 23 USD is worth 28 CAD. You can spend these in the following countries:
const axios = require('axios');
const getExchangeRate = (from, to) => {
axios.get(`http://api.fixer.io/latest?base=USD).then((response) => {
return response.data.rates[to]
});
}
So, in this case, we would have USD to Canadian dollars. We would call this URL with USD and we would get this value back: 1.2889. That is the exact value that we're going to return. Let's go ahead and test this out down below, getExchangeRate. I'm going to pass in USD to CAD (Canadian) dollars, then we'll get our rate back and we can log it out, console.log(rate):
// USD CAD 23
// 23 USD is worth 28 CAD. You can spend these in the following countries:
const axios = require('axios');
const getExchangeRate = (from, to) => {
return axios.get(`http://api.fixer.io/latest?base=USD).then((response) => {
return response.data.rates[to]
});
};
getExchangeRate('USD', 'CAD').then((rate) => {
console.log(rate);
});
I'm going to go ahead and save this. In the background, inside the Terminal, we can start up nodemon once again, and we're going to run the currency-convert file. Right here, we get that value 1.2889 is the current currency exchange rate:

I could put EUR in code:
getExchangeRate('USD', 'EUR').then((rate) => {
console.log(rate);
});
I could figure out what the Euro exchange rate is 0.80574, so there we go:

We got our first one all knocked out. Now, the other one that we're going to create real quick is going to be getCountries.