Skip to content

Guide

Exchange Rates in Google Sheets with Apps Script

Pull exchange rates into Google Sheets with a short Apps Script custom function: a cached FXBASE_RATES table, a converter formula and a safe key.

Updated

Spreadsheets are where a lot of currency conversion really happens: expense reports, price lists, budgets in several currencies. Google Sheets can call any HTTP API through Apps Script, the JavaScript that runs behind every sheet. This guide adds two custom functions, a rates table and a converter, backed by the FXbase exchange rate API, with the key kept out of the cells and a cache that keeps your request count low. No add-on is needed. The calls are documented in the API reference.

Step 1: Check your key

Create a key in the console; keys start with centy_live_. Before you touch the sheet, make sure the key works from a terminal. If this prints a JSON object with rates, the key is good:

Check the key from a terminal first
curl "https://api.fxbase.dev/v1/latest?base=USD&symbols=EUR,GBP,JPY" \
  -H "Authorization: Bearer $FXBASE_API_KEY"

A 401 with unauthorized means the key was mistyped or revoked; see authentication. Testing here first means that any later error is in the script, not the key.

Step 2: Store the key in Script Properties

In your sheet, open Extensions > Apps Script. In the editor, open Project Settings, scroll to Script Properties, and add a property named FXBASE_API_KEY with your key as its value. Never put the key in a cell or in the code itself: cells are visible to anyone who can view the sheet, and code gets copied.

Two caveats before you share the sheet:

  • Anyone who can edit the sheet can open its script and read its properties. Use a key made for this sheet alone, so you can revoke it without breaking anything else.
  • The requests come from Google's servers, not your computer. A key limited to your own IP addresses is refused with a 403 and forbidden; leave the IP rule off for a key used in Sheets.

Step 3: Add the script

Replace the contents of Code.gs with the script below and save it. It defines one custom function, FXBASE_RATES, and a helper that calls the API:

Code.gs (Extensions > Apps Script)
const API = "https://api.fxbase.dev";
const CACHE_SECONDS = 3600; // one hour; CacheService keeps entries for at most 21600 (six hours)

/**
 * Exchange rates per one unit of base, one row per currency.
 * @param {string} base ISO 4217 code, e.g. "USD"
 * @param {string} symbols comma-separated codes, e.g. "EUR,GBP,JPY"
 * @return {Array<Array<string|number>>}
 * @customfunction
 */
function FXBASE_RATES(base, symbols) {
  base = String(base).trim().toUpperCase();
  symbols = String(symbols).replace(/\s+/g, "").toUpperCase();
  const cacheKey = `latest:${base}:${symbols}`;
  const cache = CacheService.getScriptCache();
  const cached = cache.get(cacheKey);
  const body = cached ? JSON.parse(cached) : fetchLatest(base, symbols);
  if (!cached) cache.put(cacheKey, JSON.stringify(body), CACHE_SECONDS);
  return Object.entries(body.rates).map(([code, rate]) => [code, rate]);
}

function fetchLatest(base, symbols) {
  const key = PropertiesService.getScriptProperties().getProperty("FXBASE_API_KEY");
  const url = `${API}/v1/latest?base=${base}&symbols=${symbols}`;
  const res = UrlFetchApp.fetch(url, {
    headers: { Authorization: `Bearer ${key}` },
    muteHttpExceptions: true,
  });
  const body = JSON.parse(res.getContentText());
  if (res.getResponseCode() !== 200) throw new Error(`${body.code}: ${body.detail}`);
  return body;
}

UrlFetchApp makes the request with the key in an Authorization: Bearer header. muteHttpExceptions lets the script read the body of a failed request, so a bad currency code shows up in the cell as the API's own explanation, such as unknown_currency, instead of a generic failure. Every error is an RFC 9457 problem with a code and a detail.

CacheService keeps each answer for CACHE_SECONDS. Sheets can recalculate a custom function again, for example when its arguments change, and without a cache every recalculation would be a new request. A cached answer is shared by every cell that asks for the same base and currencies.

Step 4: Use it in the sheet

Type =FXBASE_RATES("USD", "EUR,GBP,JPY") in an empty cell. The function returns a two-column table, one currency per row with its rate per one US dollar, and Sheets spills it into the cells below and to the right. Both arguments can be cell references, so a base currency picked from a dropdown works too.

To convert amounts, look each one up in that table rather than calling the API per row. With the table in E2:F4 and an amount in A2:

=A2 * VLOOKUP("EUR", $E$2:$F$4, 2, FALSE)

If you prefer a single formula per cell, add this function to the same script. It reuses the cached table, so repeated pairs cost nothing:

A conversion formula on top
/**
 * Converts an amount with the cached latest rates.
 * @customfunction
 */
function FXBASE_CONVERT(amount, from, to) {
  if (String(from).toUpperCase() === String(to).toUpperCase()) return Number(amount);
  const rows = FXBASE_RATES(from, to);
  return Number(amount) * rows[0][1];
}

Then =FXBASE_CONVERT(A2, "USD", "EUR"). Each distinct pair is its own request the first time, so a column with dozens of different pairs is better served by one FXBASE_RATES table and lookups.

Step 5: Fit the cache to your plan

Each cached formula fetches at most once per CACHE_SECONDS. At the one-hour default that is up to 720 requests a month for a sheet that is recalculated around the clock, comfortably inside Startup's 30,000 and a good match for its hourly rate snapshots. The Developer plan has 500 requests a month and daily rates, previous day, so an hourly refresh gains nothing there: set CACHE_SECONDS to 21600, the longest CacheService allows, for at most 120 requests a month per formula.

The per-minute limit matters when a sheet first opens and every formula runs at once. The Developer plan allows 5 requests a minute, which is another reason to keep one table formula per base currency. Over a limit the API answers 429, which the script shows in the cell; the cell recovers on the next recalculation. The rate limits guide explains both limits.

Apps Script also has limits of its own: a custom function must finish within 30 seconds, and Google caps how many external requests a script makes per day. A cached table formula stays well clear of both.

What about Excel?

Excel can make the same request through Power Query: Web.Contents accepts a Headers record, where the Authorization header goes, and Json.Document parses the answer. The same caveat applies: the key is stored in the workbook, so use a dedicated key for any file you share.

Building something bigger than a sheet? The currency converter guide moves the same idea, one cached rates table, to a web server.

Try it with a free key

The Developer plan is free and includes latest rates and conversion. Every request in this guide is in the API reference. Get a free API key.