JavaScript / Node.js

JavaScript Exchange Rate API

Fetch live and historical exchange rates in JavaScript and Node.js. Install our SDK with npm or call the REST API with fetch.

Quick Start

Install the official JavaScript SDK and fetch your first exchange rate in under a minute.

npm install @exchangerateapi/sdk
import ExchangeRateAPI from '@exchangerateapi/sdk';

const client = new ExchangeRateAPI({ apiKey: 'era_live_YOUR_API_KEY' });

// Get the latest USD to EUR rate
const { rates } = await client.latest({ base: 'USD', symbols: ['EUR'] });
console.log(`1 USD = ${rates.EUR} EUR`);
// Output: 1 USD = 0.9215 EUR
Get your free API keySign up here to get an API key that starts with era_live_. The free plan includes 300 requests per month.

Using the SDK

The @exchangerateapi/sdk npm package wraps every API endpoint and handles authentication, retries, and TypeScript types for you.

Get Latest Rates

import ExchangeRateAPI from '@exchangerateapi/sdk';

const client = new ExchangeRateAPI({ apiKey: 'era_live_YOUR_API_KEY' });

// Multiple currencies in one call
const { rates } = await client.latest({ base: 'USD', symbols: ['EUR', 'GBP', 'JPY'] });
console.log(rates); // { EUR: 0.9215, GBP: 0.7891, JPY: 151.42 }

Convert an Amount

// Convert 1000 USD to EUR
const result = await client.convert('USD', 'EUR', 1000);
console.log(`$1,000 = ${result.result} EUR`);

// Historical conversion at a specific date
const past = await client.convert('USD', 'EUR', 1000, { date: '2026-01-15' });

Historical Rates

// Rates for a specific date
const data = await client.forDate('2026-01-15', { base: 'EUR', symbols: ['USD', 'GBP'] });

// Historical rates by period
const history = await client.getHistoricalRates('USD', 'EUR', '30d');

List Supported Currencies

const { symbols } = await client.symbols();
console.log(symbols); // { USD: 'United States Dollar', EUR: 'Euro', ... }

Without SDK (Using fetch)

If you prefer to call the API directly without the SDK, use the native fetch API available in Node.js 18+ and all modern browsers.

const response = await fetch(
  'https://exchange-rateapi.com/api/v1/rates?source=USD&target=EUR',
  { headers: { 'Authorization': 'Bearer era_live_YOUR_API_KEY' } }
);
const data = await response.json();
console.log(`1 USD = ${data[0].rate} EUR`);
// Output: 1 USD = 0.9215 EUR

Multiple Targets

const response = await fetch(
  'https://exchange-rateapi.com/api/v1/rates?source=USD&target=EUR,GBP,JPY',
  { headers: { 'Authorization': 'Bearer era_live_YOUR_API_KEY' } }
);

const rates = await response.json();
rates.forEach(r => console.log(`1 USD = ${r.rate} ${r.target}`));
// Output:
// 1 USD = 0.9215 EUR
// 1 USD = 0.7891 GBP
// 1 USD = 151.42 JPY

Error Handling

async function getRate(source, target) {
  try {
    const response = await fetch(
      `https://exchange-rateapi.com/api/v1/rates?source=${source}&target=${target}`,
      { headers: { 'Authorization': 'Bearer era_live_YOUR_API_KEY' } }
    );

    if (!response.ok) {
      if (response.status === 401) throw new Error('Invalid API key');
      if (response.status === 429) throw new Error('Rate limit exceeded');
      throw new Error(`HTTP ${response.status}`);
    }

    const data = await response.json();
    return data[0].rate;
  } catch (error) {
    console.error('Failed to fetch rate:', error.message);
    throw error;
  }
}

Response Format

Single Target Response

[
  {
    "rate": 0.9215,
    "source": "USD",
    "target": "EUR",
    "time": "2026-05-15T12:00:00Z"
  }
]

Multiple Targets Response

[
  { "rate": 0.9215, "source": "USD", "target": "EUR", "time": "2026-05-15T12:00:00Z" },
  { "rate": 0.7891, "source": "USD", "target": "GBP", "time": "2026-05-15T12:00:00Z" },
  { "rate": 151.42, "source": "USD", "target": "JPY", "time": "2026-05-15T12:00:00Z" }
]
TypeScript support — The @exchangerateapi/sdk package includes full TypeScript type definitions out of the box. No need to install separate @types packages.

Start building with JavaScript

Get your free API key and start fetching exchange rates in JavaScript in under a minute.

Get Free API Key →