npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

cbsl-exchange-rates

v1.0.0

Published

Fetch live exchange rates from the Central Bank of Sri Lanka (CBSL)

Readme

cbsl-exchange-rates

A lightweight Node.js package to fetch live foreign exchange rates published by the Central Bank of Sri Lanka (CBSL). Returns Indicative, Buy, and Sell rates for any supported currency against the Sri Lankan Rupee (LKR).

npm version license node


Table of Contents


What is this?

The Central Bank of Sri Lanka publishes daily exchange rates on their website. This package scrapes those rates programmatically so you can use them in your own Node.js applications — whether you are building a finance app, a currency converter, a REST API, or just need quick LKR exchange rate data.

No API key required. No registration needed.


Installation

npm install cbsl-exchange-rates

Requirements: Node.js >= 14.0.0


Quick Start

const { fetchRate, fetchRates } = require("cbsl-exchange-rates");

// Single currency
const rate = await fetchRate("usd");
console.log(rate);
// { currency: 'USD/LKR', indicative: 310.94, buy: 307.17, sell: 314.87 }

// Multiple currencies at once
const rates = await fetchRates(["usd", "eur", "gbp"]);
console.log(rates);
// [
//   { currency: 'USD/LKR', indicative: 310.94, buy: 307.17, sell: 314.87 },
//   { currency: 'EUR/LKR', indicative: 358.86, buy: 353.19, sell: 364.81 },
//   { currency: 'GBP/LKR', indicative: 415.98, buy: 409.65, sell: 422.49 }
// ]

API Reference

fetchRate(currency)

Fetches the exchange rate for a single currency against LKR.

Parameter:

| Name | Type | Required | Description | |------------|----------|----------|--------------------------------------------------------------------| | currency | string | Yes | ISO 4217 currency code. Case-insensitive (e.g. "usd", "USD") |

Returns: Promise<RateResult>

{
  currency:   string,   // Pair label, e.g. "USD/LKR"
  indicative: number,   // Mid-market indicative rate
  buy:        number,   // Bank buying rate
  sell:       number    // Bank selling rate
}

Example:

const { fetchRate } = require("cbsl-exchange-rates");

const rate = await fetchRate("jpy");
console.log(rate);
// { currency: 'JPY/LKR', indicative: 2.07, buy: 2.04, sell: 2.10 }

Throws: Error if the network request fails or the currency is not supported by CBSL.


fetchRates(currencies)

Fetches exchange rates for multiple currencies concurrently. All requests are made in parallel for maximum speed.

Parameter:

| Name | Type | Required | Description | |--------------|------------|----------|-----------------------------------| | currencies | string[] | Yes | Array of ISO 4217 currency codes |

Returns: Promise<Array<RateResult | ErrorResult>>

Each item in the returned array is either a successful RateResult (see above), or an ErrorResult if that particular currency failed:

{
  currency: string,   // e.g. "XYZ/LKR"
  error:    string    // Reason the fetch failed
}

Example:

const { fetchRates } = require("cbsl-exchange-rates");

const rates = await fetchRates(["usd", "eur", "gbp", "inr"]);

rates.forEach(rate => {
  if (rate.error) {
    console.log(`${rate.currency} - Failed: ${rate.error}`);
  } else {
    console.log(`${rate.currency} → Buy: ${rate.buy}, Sell: ${rate.sell}`);
  }
});

Unlike fetchRate(), this function never throws even if individual currencies fail — errors are returned inline per currency so the rest of your results are unaffected.


CURRENCIES

A pre-defined array of all currency codes known to be supported by CBSL.

const { CURRENCIES } = require("cbsl-exchange-rates");

console.log(CURRENCIES);
// ['usd', 'eur', 'gbp', 'jpy', 'aud', 'cny']

Use this to fetch all supported rates at once:

const { fetchRates, CURRENCIES } = require("cbsl-exchange-rates");

const allRates = await fetchRates(CURRENCIES);
console.table(allRates);

Supported Currencies

These are the currencies verified to have active pages on the CBSL website:

| Code | Currency | |-------|-------------------------| | usd | US Dollar | | eur | Euro | | gbp | British Pound Sterling | | jpy | Japanese Yen | | aud | Australian Dollar | | cny | Chinese Yuan Renminbi |

Currency codes are case-insensitive"USD", "usd", and "Usd" all work.

Note: CBSL does not publish pages for all world currencies. If you need a currency not listed above, pass it to fetchRate() directly — it will throw an error if the page does not exist.


Error Handling

Single currency — use try/catch

const { fetchRate } = require("cbsl-exchange-rates");

try {
  const rate = await fetchRate("usd");
  console.log(rate);
} catch (err) {
  console.error("Error:", err.message);
  // e.g. "Failed to fetch rate for USD: timeout of 10000ms exceeded"
  //      "No rate data found for XYZ. The currency code may be unsupported."
}

Multiple currencies — errors returned inline

const { fetchRates } = require("cbsl-exchange-rates");

const rates = await fetchRates(["usd", "xyz"]);
// [
//   { currency: 'USD/LKR', indicative: 310.94, buy: 307.17, sell: 314.87 },
//   { currency: 'XYZ/LKR', error: 'No rate data found for XYZ...' }
// ]

Express.js Integration

Wrap the package inside an Express route to expose exchange rates as a REST API:

const express = require("express");
const { fetchRate, fetchRates, CURRENCIES } = require("cbsl-exchange-rates");

const app = express();

// GET /api/rate/usd  →  single rate
app.get("/api/rate/:currency", async (req, res) => {
  try {
    const data = await fetchRate(req.params.currency);
    res.json(data);
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

// GET /api/rates  →  all supported rates
app.get("/api/rates", async (req, res) => {
  try {
    const data = await fetchRates(CURRENCIES);
    res.json(data);
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

app.listen(3000, () => {
  console.log("CBSL FX API running on http://localhost:3000");
});

Sample response for GET /api/rate/usd:

{
  "currency": "USD/LKR",
  "indicative": 310.9475,
  "buy": 307.1786,
  "sell": 314.8701
}

How It Works

  1. Sends an HTTP GET request to the CBSL currency page for the requested currency:
    https://www.cbsl.gov.lk/cbsl_custom/charts/{currency}/indexsmall.php
  2. Parses the returned HTML using cheerio (a lightweight jQuery-style HTML parser).
  3. Searches all <p> tags for the keywords Indicative, Buy, and Sell.
  4. Extracts the numeric value from the end of each matching line.
  5. Returns a plain JavaScript object with the parsed values.

No DOM rendering. No headless browser. Fast and lightweight.


Data Source

All exchange rate data is sourced directly from the Central Bank of Sri Lanka:

  • Website: https://www.cbsl.gov.lk
  • Rates are updated periodically on Sri Lanka business days.
  • This package does not cache data — every call fetches fresh data from CBSL.

Disclaimer: This package is not affiliated with or endorsed by the Central Bank of Sri Lanka. Use the data at your own discretion for informational purposes.


License

MIT © GeekHirushaDev