Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback mechanism #90

Open
fawazahmed0 opened this issue Mar 5, 2024 · 16 comments
Open

Fallback mechanism #90

fawazahmed0 opened this issue Mar 5, 2024 · 16 comments

Comments

@fawazahmed0
Copy link
Owner

fawazahmed0 commented Mar 5, 2024

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
Copy link

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

@fawazahmed0
Copy link
Owner Author

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

@gaganpreetsharma000
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

Gowthamssr commented Jun 30, 2024

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
Copy link

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
Copy link

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
Copy link

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
Copy link

@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
Copy link

Aishaarain commented Jul 2, 2024 via email

@Aishaarain
Copy link

@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
Copy link

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

@abadothali
Copy link

plese slove this problem, i can't convert...

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

const dropdowns = document.querySelectorAll('.dropdown select');
const btn = document.querySelector('form button');

const fromCurency = document.querySelector('.from select');
const toCurency = document.querySelector('.to select');

for (let select of dropdowns){
for (let currCode in countryList){
let newOPtions = document.createElement('option');
newOPtions.innerText = currCode;
newOPtions.value = currCode;

   if(select.name === 'from' && currCode === 'USD'){
    newOPtions.selected = 'selected';
   }else if(select.name === 'to' && currCode === 'INR'){
    newOPtions.selected = 'selected';
   }

   select.append(newOPtions);

}

select.addEventListener('change', (evn)=>{
    updateFlg(evn.target);
});

}

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

btn.addEventListener('click', async(evn)=>{
evn.preventDefault();
let amount = document.querySelector('.amount input');
let amountValue = amount.value;

if(amountValue == ' ' || amountValue < 1){
    amount.value = 1;
    amountValue = 'Wrong input, enter atlest 1';
}

console.log(amountValue);


const URL = `${BASE_URL}/${fromCurency.value.toLowerCase().min.json}/${toCurency.value.toLowerCase().min.json}`;
let response = await fetch(URL);
let data = await response.json()
let rate = data[toCurency.value.toLowerCase()];
console.log(rate);

});

@rajawatajay
Copy link

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

firstly make your github account public then proivde the reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests