Integration guide

Add currency exchange rates and conversion to React

Call your own backend from React so the API key never reaches the browser. This example pairs a small Express proxy with a useEffect converter.

1. Get a free API key

Sign up at https://currencyexchangerate-api.com/auth/login, then create a key under API keys in the console. The free plan includes 3,000 requests per month with no credit card. Store the secret as CXR_API_KEY in server-side environment configuration; never expose it in browser code.

.env

shell
CXR_API_KEY=cxr_your_secret_key

2. Create a reusable client and cache rates

Use the standard HTTP client. Cache every /latest response until nextUpdateAt: rates change at most once by then, and reusing the response saves quota. The helper also exposes /convert.

server.js

JavaScript
import express from 'express'; const API_BASE_URL = 'https://api.currencyexchangerate-api.com/api/v1';const app = express(); app.get('/api/convert', async (req, res) => {  const query = new URLSearchParams({    from: String(req.query.from),    to: String(req.query.to),    amount: String(req.query.amount),  });  const response = await fetch(API_BASE_URL + '/convert?' + query, {    headers: { Authorization: 'Bearer ' + process.env.CXR_API_KEY },  });  res.status(response.status).json(await response.json());}); app.listen(3000);

3. Use it in React

Call the helper from a server route, controller, view, command, or script. Format converted values for the user’s locale.

Converter.jsx

JavaScript / JSX
import { useEffect, useState } from 'react'; export function Converter() {  const [result, setResult] = useState(null);   useEffect(() => {    fetch('/api/convert?from=EUR&to=GBP&amount=100')      .then((response) => response.json())      .then(setResult);  }, []);   if (!result) {    return <p>Loading...</p>;  }   const formatted = new Intl.NumberFormat('en-GB', {    style: 'currency',    currency: result.to,  }).format(result.result);  return <p>{formatted}</p>;}

4. Handle API errors

A 401 means the key is invalid. A 429 can mean a rate limit or quota limit; read Retry-After in seconds before retrying. Other errors use application/problem+json.

errors.js

JavaScript
// In get(), replace the generic throw with://   if (!response.ok) throw await apiError(response);async function apiError(response) {  const problem = await response.json().catch(() => ({}));  if (response.status === 401) {    return new Error('Check CXR_API_KEY');  }  if (response.status === 429) {    const retryAfter = response.headers.get('Retry-After');    return new Error('Retry after ' + retryAfter + ' seconds');  }  return new Error(problem.detail || 'Rates API status ' + response.status);}

5. Format the amount for display

Use locale-aware currency formatting instead of adding a symbol manually.

format.js

JavaScript
const formatter = new Intl.NumberFormat('en-GB', {  style: 'currency',  currency: 'EUR',});const formatted = formatter.format(85.249105);