fawazahmed0 / exchange-api

Free Currency Exchange Rates API with 150+ Currencies & No Rate Limits
https://github.com/fawazahmed0/exchange-api#readme
Creative Commons Zero v1.0 Universal
370 stars 22 forks source link

Fallback mechanism #90

Open fawazahmed0 opened 4 months ago

fawazahmed0 commented 4 months ago

Fallback URL:

https://{date}.currency-api.pages.dev/{apiVersion}/{endpoint}

Pseudo code: For example, if you want to fetch eur:

Fetch https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/eur.min.json
If above url fails, then try fetching https://latest.currency-api.pages.dev/v1/currencies/eur.min.json
If above url fails, then try fetching https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/eur.json
If above url fails, then try fetching https://latest.currency-api.pages.dev/v1/currencies/eur.json
bobaikato commented 4 months ago

Whats the difference between eur.min.json vs eur.json?

fawazahmed0 commented 4 months ago

eur.min.json has no white space to reduce file size

gaganpreetsharma000 commented 4 months ago

brother please help.. i am new to programing. just making currency converter. you have migrated the files to github. I want to use this link https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/eur/jpy.json in my code but i can't. so as per the given instructions i figured out a little bit and i was able to make this link https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/inr.json but at the last of this link i need usd/inr.json Plz help me out

KerasirovED commented 4 months ago

brother please help.. i am new to programing. just making currency converter. you have migrated the files to github. I want to use this link https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/eur/jpy.json in my code but i can't. so as per the given instructions i figured out a little bit and i was able to make this link https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/inr.json but at the last of this link i need usd/inr.json Plz help me out

@gaganpreetsharma000, have you seen the migration instruction? It's pretty clear.

By the url, https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/inr.json you are fetching data in the rate INR/XXX, where XXX is another currecy from the got JSON, e.g. USD. In that case, one INR costs 0.012070684 USD.

As you need usd/inr, you have to fetch data USD/XXX: https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/usd.json, where you'll get a cost of one USD in different currencies, and select the needed one from the received JSON, in your case INR.

image

Try

async function getUsdInrRate() {
    // This url will give you currencies UDS/XXX
    const url = 'https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/usd.min.json'

    // Fetch data and get INR rate
    const inr = await fetch(url)
        .then(response => response.json())
        .then(data => data['usd'].inr)

    console.log(inr)
}

getUsdInrRate()

You'll get current rate

image

[!TIP] Use min versions of the files to get reduced files (whitespaces deleted), and faster responses, I assume. Instead of 'https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/usd.json' Use 'https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/usd.min.json' I used the second one in the code

vandana-khatri8 commented 2 months ago

brother please help.. i am new to programing. just making currency converter. you have migrated the files to github. I want to use this link https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/eur/jpy.json in my code but i can't. so as per the given instructions i figured out a little bit and i was able to make this link https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/inr.json but at the last of this link i need usd/inr.json Plz help me out

i face the same issue do you have its solution ???

owaismohammed79 commented 2 weeks ago

I've tried all the fallback URLs but I don't think they work when sent through a node environment. When I try the same on a browser they seem to work Screenshot 2024-06-30 111202

Gowthamssr commented 2 weeks ago

brother please help.. i am new to programing. just making currency converter. you have migrated the files to github. I want to use this link https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/eur/jpy.json in my code but i can't. so as per the given instructions i figured out a little bit and i was able to make this link https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/inr.json but at the last of this link i need usd/inr.json Plz help me out

i face the same issue do you have its solution ???

check out my github page https://gowthamssr.github.io/currency_converter/ are u looking for this functionality @KerasirovED helped me
this is problem faced because the API is changed refer this it may help u....

................................................................................................................................................................................................................................ const BASE_URL ="https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies"//usd.min.json" // "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies";

const dropdowns = document.querySelectorAll(".dropdown select"); const btn = document.querySelector("form button"); const fromCurr = document.querySelector(".from select"); const toCurr = document.querySelector(".to select"); const msg = document.querySelector(".msg");

for (let select of dropdowns) { for (currCode in countryList) { let newOption = document.createElement("option"); newOption.innerText = currCode; newOption.value = currCode; if (select.name === "from" && currCode === "USD") { newOption.selected = "selected"; } else if (select.name === "to" && currCode === "INR") { newOption.selected = "selected"; } select.append(newOption); }

select.addEventListener("change", (evt) => { updateFlag(evt.target); }); }

const updateExchangeRate = async () => { let amount = document.querySelector(".amount input"); let amtVal = amount.value; if (amtVal === "" || amtVal < 1) { amtVal = 1; amount.value = "1"; } const URL = ${BASE_URL}/${fromCurr.value.toLowerCase()}.min.json;///${toCurr.value.toLowerCase()}.json`; let response = await fetch(URL); let data = await response.json(); console.log(data); let from = fromCurr.value.toLowerCase(); console.log(from); let to = toCurr.value.toLowerCase(); console.log(to);

let rate = data[from][to];

let finalAmount = amtVal * rate; msg.innerText = ${amtVal} ${fromCurr.value} = ${finalAmount} ${toCurr.value}; };

const updateFlag = (element) => { let currCode = element.value; let countryCode = countryList[currCode]; let newSrc = https://flagsapi.com/${countryCode}/flat/64.png; let img = element.parentElement.querySelector("img"); img.src = newSrc; };

btn.addEventListener("click",(evt) => { evt.preventDefault(); // let amount = document.querySelector(".amount input"); // let amtVal = amount.value; // if(amtVal === "" || amtVal < 1) // { // amtVal = 1; // amount.value = "1"; // }

// const URL = ${BASE_URL}/${fromCurr.value.toLowerCase()}/${toCurr.value.toLowerCase()}.json // let response = await fetch(URL); // let data = await response.json; // let rate = data[toCurr.value.to.toLowerCase()]; // let finalAmount = amtVal*rate; // msg.innerText = ${amtVal} ${fromCurr} = ${finalAmount} ${toCurr.value}

updateExchangeRate(); });

window.addEventListener("load",() => { updateExchangeRate(); });

follow me on https://www.linkedin.com/in/gowtham-ssr-1a5858280/

Gowthamssr commented 2 weeks ago

I've tried all the fallback URLs but I don't think they work when sent through a node environment. When I try the same on a browser they seem to work Screenshot 2024-06-30 111202

i have posted just now

Aishaarain commented 2 weeks ago

I've tried all the fallback URLs but I don't think they work when sent through a node environment. When I try the same on a browser they seem to work Screenshot 2024-06-30 111202

i have posted just now

this URL is still not working , please any solution u can suggest to me, i have also tried all fallbacks url's, but still not working.

Aishaarain commented 2 weeks ago

Try other apis available on github or just share ur problem and code on 6281362727 On Mon, Jul 1, 2024, 2:13 AM Aishaarain @.> wrote: I've tried all the fallback URLs but I don't think they work when sent through a node environment. When I try the same on a browser they seem to work [image: Screenshot 2024-06-30 111202] https://private-user-images.githubusercontent.com/131255226/344445840-982ccec0-f005-449b-90b4-7de58195b1bf.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTk3MjgxNDcsIm5iZiI6MTcxOTcyNzg0NywicGF0aCI6Ii8xMzEyNTUyMjYvMzQ0NDQ1ODQwLTk4MmNjZWMwLWYwMDUtNDQ5Yi05MGI0LTdkZTU4MTk1YjFiZi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNjMwJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDYzMFQwNjEwNDdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0zZWEyZDljODQwMTFjNDgzYWIwZjA2YjZhOWU1NjkyZjBhZDU2ZDIxZDdiNmZkYzNlMTQ3Nzg0NjU2YTc4YWU2JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.b4Pw7_H8FKRwyOaY7XVaPSSsNqLYd_tv4WIdALRXU9Y i have posted just now this URL is still not working , please any solution u can suggest to me, i have also tried all fallbacks url's, but still not working. — Reply to this email directly, view it on GitHub <#90 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/BCEKQPAQ64QRNTMTEY5AQ6LZKBUXLAVCNFSM6AAAAABEG4MG2CVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOJYG42TCOJYGE . You are receiving this because you commented.Message ID: @.>

const Base_Url= "https://api.exchangeratesapi.io/v1/latest? access_key = API_KEY";

const dropdown=document.querySelectorAll(".drop_down select"); const btn=document.querySelector("form button"); const fromCurr=document.querySelector(".from select"); const toCurr=document.querySelector(".to select"); const msg=document.querySelector(".msg");

for(let select of dropdown){ for (currcode in countryList){ let newOption=document.createElement("option"); newOption.innerText=currcode; newOption.value=currcode; select.append(newOption); if(select.name==="from" && currcode==="USD"){ newOption.selected="selected"; } else if(select.name==="to" && currcode==="PKR"){ newOption.selected="selected"; } }

select.addEventListener("change",(evt)=>{ updateflag(evt.target); }); }

const updateExchangeRate=async()=>{ let amount=document.querySelector(".amount input"); let amtValue=amount.value; if(amtValue===" " || amtValue<1){ amtValue=1; amount.value="1"; }

const URL=`${Base_Url}/${fromCurr.value.toLowerCase()}/${toCurr.value.toLowerCase()}.json()`;
let response= await fetch(Base_Url);
let data= await response.json(); 
let rate= data[toCurr.value.toLowerCase()];

let finalamount =amtValue * rate;
msg.innerText=`${amtValue} ${fromCurr.value} = ${finalamount} ${toCurr.value}`;
console.log(rate);
console.log(data);
console.log(finalamount);
};

const updateflag=(element)=>{ let currcode= element.value; let countrycode=countryList[currcode]; let newsrc=https://flagsapi.com/${countrycode}/flat/64.png; let image=element.parentElement.querySelector("img"); image.src=newsrc; }; // btn.addEventListener("click",async(evt)=>{ evt.preventDefault(); updateExchangeRate(); });

document.addEventListener("load",()=>{ updateExchangeRate(); });

this is my JS code i have tried different apis but it does not run, i am getting this error=> 401 (Unauthorized)

Gowthamssr commented 2 weeks ago

@Aishaarain const API_KEY = 'your_api_key_here'; // Replace with your actual API key const Base_Url = https://api.exchangeratesapi.io/v1/latest?access_key=${API_KEY};

const dropdown = document.querySelectorAll(".drop_down select"); const btn = document.querySelector("form button"); const fromCurr = document.querySelector(".from select"); const toCurr = document.querySelector(".to select"); const msg = document.querySelector(".msg");

for (let select of dropdown) { for (let currcode in countryList) { let newOption = document.createElement("option"); newOption.innerText = currcode; newOption.value = currcode; select.append(newOption); if (select.name === "from" && currcode === "USD") { newOption.selected = "selected"; } else if (select.name === "to" && currcode === "PKR") { newOption.selected = "selected"; } }

select.addEventListener("change", (evt) => {
    updateFlag(evt.target);
});

}

const updateExchangeRate = async () => { let amount = document.querySelector(".amount input"); let amtValue = amount.value; if (amtValue === "" || amtValue < 1) { amtValue = 1; amount.value = "1"; }

const URL = `${Base_Url}&base=${fromCurr.value}&symbols=${toCurr.value}`;
let response = await fetch(URL);
if (!response.ok) {
    msg.innerText = "Error fetching exchange rate.";
    return;
}
let data = await response.json();
let rate = data.rates[toCurr.value];

let finalAmount = amtValue * rate;
msg.innerText = `${amtValue} ${fromCurr.value} = ${finalAmount} ${toCurr.value}`;
console.log(rate);
console.log(data);
console.log(finalAmount);

};

const updateFlag = (element) => { let currcode = element.value; let countrycode = countryList[currcode]; let newsrc = https://flagsapi.com/${countrycode}/flat/64.png; let image = element.parentElement.querySelector("img"); image.src = newsrc; };

btn.addEventListener("click", async (evt) => { evt.preventDefault(); updateExchangeRate(); });

document.addEventListener("DOMContentLoaded", () => { updateExchangeRate(); });

Aishaarain commented 2 weeks ago
  Sent from Mail for Windows From: GowthamSent: Monday, July 1, 2024 6:25 PMTo: fawazahmed0/exchange-apiCc: Aishaarain; MentionSubject: Re: [fawazahmed0/exchange-api] Fallback mechanism (Issue ***@***.*** API_KEY = 'your_api_key_here'; // Replace with your actual API keyconst Base_Url = https://api.exchangeratesapi.io/v1/latest?access_key=${API_KEY};const dropdown = document.querySelectorAll(".drop_down select");const btn = document.querySelector("form button");const fromCurr = document.querySelector(".from select");const toCurr = document.querySelector(".to select");const msg = document.querySelector(".msg");for (let select of dropdown) {for (let currcode in countryList) {let newOption = document.createElement("option");newOption.innerText = currcode;newOption.value = currcode;select.append(newOption);if (select.name === "from" && currcode === "USD") {newOption.selected = "selected";} else if (select.name === "to" && currcode === "PKR") {newOption.selected = "selected";}}select.addEventListener("change", (evt) => {    updateFlag(evt.target);});}const updateExchangeRate = async () => {let amount = document.querySelector(".amount input");let amtValue = amount.value;if (amtValue === "" || amtValue < 1) {amtValue = 1;amount.value = "1";}const URL = `${Base_Url}&base=${fromCurr.value}&symbols=${toCurr.value}`;let response = await fetch(URL);if (!response.ok) {    msg.innerText = "Error fetching exchange rate.";    return;}let data = await response.json();let rate = data.rates[toCurr.value]; let finalAmount = amtValue * rate;msg.innerText = `${amtValue} ${fromCurr.value} = ${finalAmount} ${toCurr.value}`;console.log(rate);console.log(data);console.log(finalAmount);};const updateFlag = (element) => {let currcode = element.value;let countrycode = countryList[currcode];let newsrc = https://flagsapi.com/${countrycode}/flat/64.png;let image = element.parentElement.querySelector("img");image.src = newsrc;};btn.addEventListener("click", async (evt) => {evt.preventDefault();updateExchangeRate();});document.addEventListener("DOMContentLoaded", () => {updateExchangeRate();});—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: ***@***.***> 
Aishaarain commented 2 weeks ago

@Gowthamssr , i think there is another issue in my code , i have tried alot APIS but none is working, do you know why it is happened. this error is displayed=> success: false, error: {…}}error: {code: 101, type: 'invalid_access_key', info: 'You have not supplied a valid API Access Key. [Technical Support: support@apilayer.com]'} success: false[[Prototype]]: Object

this is my code=> const Base_Url = "http://data.fixer.io/api/latest?access_key=7370ff2962d49abefc56c32f5bc74aa8";

const dropdown=document.querySelectorAll(".drop_down select"); const btn=document.querySelector("form button"); const fromCurr=document.querySelector(".from select"); const toCurr=document.querySelector(".to select"); const msg=document.querySelector(".msg");

for(let select of dropdown){ for (currcode in countryList){ let newOption=document.createElement("option"); newOption.innerText=currcode; newOption.value=currcode; select.append(newOption); if(select.name==="from" && currcode==="USD"){ newOption.selected="selected"; } else if(select.name==="to" && currcode==="PKR"){ newOption.selected="selected"; } }

select.addEventListener("change",(evt)=>{ updateflag(evt.target); }); }

const updateExchangeRate=async()=>{ let amount=document.querySelector(".amount input"); let amtValue=amount.value; if(amtValue===" " || amtValue<1){ amtValue=1; amount.value="1"; }

// const URL = `${Base_Url}&base=${fromCurr.value}&symbols=${toCurr.value}`;
const URL = `${Base_Url}/${fromCurr.value.toLowerCase()}/${toCurr.value.toLowerCase()}.json`;

let response = await fetch(URL); if (!response.ok) { msg.innerText = "Error fetching exchange rate."; return; } let data = await response.json(); let rate = data[toCurr.value.toLowerCase];

let finalAmount = amtValue * rate; msg.innerText = ${amtValue} ${fromCurr.value} = ${finalAmount} ${toCurr.value}; console.log(rate); console.log(data); console.log(finalAmount); };

const updateflag=(element)=>{ let currcode= element.value; let countrycode=countryList[currcode]; let newsrc=https://flagsapi.com/${countrycode}/flat/64.png; let image=element.parentElement.querySelector("img"); image.src=newsrc; }; // btn.addEventListener("click",async(evt)=>{ evt.preventDefault(); updateExchangeRate(); });

document.addEventListener("load",()=>{ updateExchangeRate(); });

Narendrag20 commented 3 days ago

Hello,

This link is not working Could you please help me to get a working URL? https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/eur/jpy.json