@tamtamchik/exchanger
v4.0.1
Published
Simple and free npm library to get realtime currency exchange rates.
Maintainers
Readme
Exchanger fetches currency exchange rates from Yahoo Finance without an API key. It supports ES modules and CommonJS, includes TypeScript types, and provides optional in-memory caching and typed errors. Node.js 20 or later is required.
Quick Start
Install with npm:
npm install @tamtamchik/exchangerOr with yarn:
yarn add @tamtamchik/exchangerFetch a rate:
import { getExchangeRate } from "@tamtamchik/exchanger";
const rate = await getExchangeRate("USD", "EUR");
console.log(`1 USD = ${rate} EUR`);Usage
Pass the source and target currency codes to getExchangeRate. It returns a
Promise<number> containing the rate for the pair. The Yahoo Finance request uses
uppercase currency codes.
ES Modules
import { getExchangeRate } from "@tamtamchik/exchanger";
const rate = await getExchangeRate("USD", "EUR");
console.log(`1 USD = ${rate} EUR`);CommonJS
const { getExchangeRate } = require("@tamtamchik/exchanger");
getExchangeRate("USD", "EUR")
.then((rate) => console.log(`1 USD = ${rate} EUR`))
.catch((error) => console.error(error));Caching
Caching is disabled by default. Set cacheDurationMs to reuse a fetched rate for
the same currency pair:
const rate = await getExchangeRate("USD", "EUR", {
cacheDurationMs: 3_600_000, // One hour, in milliseconds.
});The cache lives in memory and is lost when the process exits. Calls must enable caching to read cached rates. Use consistent letter case for currency codes; cache keys preserve the case you pass.
Error Handling
Exchanger exports three error classes, each extending Error:
| Error | Cause |
| -------------- | ------------------------------------------------------------------ |
| NetworkError | The request fails or the response cannot be parsed as JSON. |
| ServerError | Yahoo Finance returns an unsuccessful HTTP status. |
| DataError | The parsed response has a missing or invalid regularMarketPrice. |
import {
DataError,
NetworkError,
ServerError,
getExchangeRate,
} from "@tamtamchik/exchanger";
try {
const rate = await getExchangeRate("USD", "EUR");
console.log(`1 USD = ${rate} EUR`);
} catch (error) {
if (error instanceof NetworkError) {
console.error("Network or JSON parsing problem:", error.message);
} else if (error instanceof ServerError) {
console.error("Yahoo Finance HTTP error:", error.message);
} else if (error instanceof DataError) {
console.error("Unexpected response data:", error.message);
} else {
console.error("Unknown error:", error);
}
}Development Setup
Prerequisite: Node.js 24 (24.11 or later) or Node.js 26+ and its bundled npm.
CI tests Node.js 24 and 26.
The .nvmrc file selects Node.js 24; npm enforces the development runtime through
devEngines. The published library retains its Node.js 20+ runtime requirement.
Install dependencies:
npm ciRun the checks used in CI:
npm run check
npm run build
npm testBiome checks and formats TypeScript and JSON files. Markdown and YAML files are
not formatted by these commands. Coverage uses unit tests; npm test also runs
acceptance tests against Yahoo Finance.
Useful focused commands:
npm run fix # Format, organize imports, and apply safe lint fixes.
npm run dev # Rebuild when source files change.
npm run coverage # Run unit tests and generate coverage reports.Documentation
- npm package: published versions and installation details.
- Source: exchange-rate requests and cache behavior.
- Tests: rate retrieval, caching, errors, and package acceptance checks.
- Security policy: vulnerability reporting.
- CI: formatting, lint, build, and test results.
Security Notes
Rates depend on Yahoo Finance availability and data. Exchanger returns a numeric rate; it does not execute currency exchanges.
Report vulnerabilities through GitHub's private vulnerability reporting. See SECURITY.md for the reporting policy.
Contributing
Pull requests are welcome. For major changes, open an issue first to discuss the proposal.
Support
License
MIT.
