Created
July 18, 2021 03:58
-
-
Save brotoo25/c74d0bc17f253e9e5ada5f753110b69a to your computer and use it in GitHub Desktop.
Query Currency price using 'Api de Moedas'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get most recent price of any currency listed at 'API de Moedas'. | |
* https://docs.awesomeapi.com.br/api-de-moedas | |
* | |
* Params: (from) - Currency Code. Ex: USD, BRL, NZD | |
* (to) - Currency Code. Ex: USD, BRL, NZD | |
**/ | |
async function fetchCurrencyPrice(from, to) { | |
try { | |
if (from.toUpperCase() == to.toUpperCase()) { | |
return 1 | |
} | |
const response = await axios.get(`https://economia.awesomeapi.com.br/json/last/${from.toUpperCase()}-${to}`); | |
return response.data[`${from}${to}`]['high'] | |
} catch (error) { | |
console.error(error); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment