Learn/JavaScript hızlı başlangıç JavaScript ve Node.js ile para birimi dönüştürme
fetch ile kuru alın, JSON sonucuyla tutarı hesaplayın ve miktar ile biçimlendirme için currency.js kullanın.
Tarayıcıda anahtarsız başlayabilirsiniz. Node.js ve sunucu uygulamalarında anahtarı ortam değişkeninde tutun; source ve gözlem zamanını da kaydedin.
Önemli noktalar
fetch Node 18+, Deno, Bun ve modern tarayıcılarda çalışır. Hesap anahtarını tarayıcıya göndermeyin. currency.js tutar ve biçim içindir; kur getirmez. HTTP durumunu kontrol edin ve 429 sonrasında Retry-After’a uyun. Gerekirse source ve effective_at alanlarını gösterin. Tarayıcıdan çağrı yapın
Küçük bir hesap makinesi veya test için anahtarsız çağrı yeterlidir. Sadece gereken symbols değerlerini isteyin ve timeout ayarlayın.
Anahtarı sunucuya taşıyın
Üretim uygulaması API’yi Node.js, Route Handler veya sunucu hizmetinden çağırmalıdır. Anahtar sunucu ortamında kalır.
Tutarı hesaplayın ve biçimlendirin
currency.js tutar, hassasiyet ve biçimlendirmeye yardımcı olur. Önce kuru alın, sonra tutarı çarpın.
Tazeliği ve hataları okuyun
sources, effective_at, source ve market_session alanlarını kontrol edin. 429’da geri çekilin; kurlar gösterge niteliğindedir.
Sık sorulanlar
javascript · no keycopy
const response = await fetch("https://api.exchangerate.dev/v1/latest/USD?symbols=EUR,GBP", {
signal: AbortSignal.timeout(10_000),
});
if (!response.ok) throw new Error(`HTTP ${response.status}; Retry-After: ${response.headers.get("Retry-After")}`);
const data = await response.json();
console.log(data.rates.EUR);
console.log(data.sources.EUR, data.effective_at.EUR);
console.log(data.market_session);javascript · authenticated call with error handlingcopy
// Node.js server only. Never put an account key in browser code.
const KEY = process.env.EXCHANGERATE_API_KEY;
if (!KEY) throw new Error("Set EXCHANGERATE_API_KEY on your server");
async function getLatest(base = "USD") {
const response = await fetch(`https://api.exchangerate.dev/v1/latest/${encodeURIComponent(base)}`, {
headers: { Authorization: `Bearer ${KEY}` },
redirect: "error",
signal: AbortSignal.timeout(10_000),
});
if (!response.ok) throw new Error(`HTTP ${response.status}; Retry-After: ${response.headers.get("Retry-After")}`);
return response.json();
}
const data = await getLatest("USD");
console.log(data.rates.GBP, data.effective_at.GBP);javascript · convert 100 USD to EURcopy
// Node.js server only.
const KEY = process.env.EXCHANGERATE_API_KEY;
if (!KEY) throw new Error("Set EXCHANGERATE_API_KEY on your server");
const response = await fetch("https://api.exchangerate.dev/v1/convert/USD/EUR/100", {
headers: { Authorization: `Bearer ${KEY}` },
redirect: "error",
signal: AbortSignal.timeout(10_000),
});
if (!response.ok) throw new Error(`HTTP ${response.status}; Retry-After: ${response.headers.get("Retry-After")}`);
const data = await response.json();
console.log(data.rate, data.converted);JavaScript · currency.jscopy
import currency from "currency.js";
const response = await fetch("https://api.exchangerate.dev/v1/latest/USD?symbols=EUR", {
signal: AbortSignal.timeout(10_000),
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const data = await response.json();
const rate = data.rates.EUR;
if (!Number.isFinite(rate) || rate <= 0) throw new Error("Invalid rate");
const euros = currency("100.00", { precision: 2, symbol: "€" }).multiply(rate);
console.log(euros.format());