Public
Script
1
2
3
4
5
6
7
8
9
10
11
12
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON";
export let currency = async (desired, base = "usd", amount = 1) => {
let { rates } = await fetchJSON(`https://open.er-api.com/v6/latest/${base}`);
if (rates && rates[desired.toUpperCase()]) return amount * (rates[desired.toUpperCase()]);
else {
let { rates } = await fetchJSON(
`https://api.coingecko.com/api/v3/exchange_rates?&x_cg_demo_api_key=${Deno.env.get("COINGECKO_API_KEY")}`,
);
return amount * rates[desired.toLowerCase()]?.value / rates[base.toLowerCase()]?.value;
}
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
August 7, 2024