Skip to content

Instantly share code, notes, and snippets.

@brotoo25
Created July 18, 2021 03:58
Show Gist options
  • Save brotoo25/c74d0bc17f253e9e5ada5f753110b69a to your computer and use it in GitHub Desktop.
Save brotoo25/c74d0bc17f253e9e5ada5f753110b69a to your computer and use it in GitHub Desktop.
Query Currency price using 'Api de Moedas'
/**
* 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