@syncx/currency-converter-package-demo
Version:
This is a node.js currency converter.
24 lines (19 loc) • 654 B
JavaScript
const axios = require("axios");
async function convertCurrency(amount, fromCurrency, toCurrency) {
try {
const url = `https://api.exchangerate-api.com/v4/latest/${fromCurrency}`;
const response = await axios.get(url);
const rate = response.data.rates[toCurrency];
if (!rate) {
throw new Error(
`Unable to get exchange rate for ${fromCurrency} to ${toCurrency}`
);
}
const convertedAmount = rate * amount;
return convertedAmount;
} catch (error) {
console.error("Error converting currency:", error.message);
}
}
module.exports = { convertCurrency };