bank-of-thailand
v1.0.1
Published
TypeScript/JavaScript client for Bank of Thailand API - Works with Node.js, Bun, and browsers
Maintainers
Readme
bank-of-thailand-js
TypeScript/JavaScript client for Bank of Thailand API - Works with Node.js, Bun, and browsers
Modern, type-safe client for the Bank of Thailand API with built-in analytics, CSV export, and universal runtime support (Node.js 20+, Bun 1.3+, browsers).
Features
- ✅ 11 API Resources - Complete BOT API coverage
- ✅ Rich Analytics - Built-in statistics, change analysis, volatility calculations
- ✅ CSV Export - Save data to CSV (Node.js) or download in browser
- ✅ Type-Safe - Full TypeScript 5.7 support with excellent IntelliSense
- ✅ Universal - Works in Node.js, Bun, Deno, and browsers
- ✅ Zero Dependencies - Lightweight and fast
- ✅ Modern Stack - Built with 2025 best practices
Installation
# npm
npm install bank-of-thailand
# pnpm (recommended)
pnpm add bank-of-thailand
# yarn
yarn add bank-of-thailand
# bun
bun add bank-of-thailandQuick Start
1. Get API Token
Get your API token from BOT API Portal
2. Set Up Environment
Create .env file:
BOT_API_TOKEN=your_api_token_hereFor Bun: .env files are automatically loaded (no package needed)
For Node.js: Install dotenv:
npm install dotenvThen import at the top of your file:
import 'dotenv/config';Or use Node.js 20.6+ built-in:
node --env-file=.env your-script.js3. Use the Client
import { createClient } from 'bank-of-thailand';
// Bun: .env automatically loaded
// Node.js: Make sure to import 'dotenv/config' first
const client = createClient({
apiToken: process.env.BOT_API_TOKEN,
});
// Fetch exchange rates
const rates = await client.exchangeRate.daily({
startPeriod: '2025-01-01',
endPeriod: '2025-01-31',
});
// Use built-in analytics
console.log('Average:', rates.average('rate'));
console.log('Trend:', rates.trend('rate'));
console.log('Volatility:', rates.volatility('rate'));
// Export to CSV
await rates.saveCSV('rates.csv');API Resources
1. Exchange Rate
// Daily weighted-average interbank exchange rate
const daily = await client.exchangeRate.daily({
startPeriod: '2025-01-01',
endPeriod: '2025-01-31',
});
// Monthly, quarterly, annual
const monthly = await client.exchangeRate.monthly({ ... });
const quarterly = await client.exchangeRate.quarterly({ ... });
const annual = await client.exchangeRate.annual({ ... });2. Average Exchange Rate
const avgRates = await client.averageExchangeRate.daily({
startPeriod: '2025-01-01',
endPeriod: '2025-01-31',
});See full documentation for all 11 API resources.
Response Analytics
All API responses include powerful analytics methods:
const rates = await client.exchangeRate.daily({ ... });
// Statistics
rates.min('rate'); // Minimum value
rates.max('rate'); // Maximum value
rates.average('rate'); // Average (mean)
rates.sum('rate'); // Sum of all values
// Change Analysis
const change = rates.change('rate');
console.log(change.percentage); // Percentage change
console.log(change.absolute); // Absolute change
// Volatility
const vol = rates.volatility('rate');
// Trend Detection
const trend = rates.trend('rate'); // 'up' | 'down' | 'flat'
// CSV Export
await rates.saveCSV('rates.csv');See Analytics Guide for complete documentation.
Error Handling
import {
AuthenticationError,
RateLimitError,
NotFoundError
} from 'bank-of-thailand';
try {
const rates = await client.exchangeRate.daily({ ... });
} catch (error) {
if (error instanceof AuthenticationError) {
console.error('Invalid API token');
} else if (error instanceof RateLimitError) {
console.error(`Rate limited. Retry after ${error.retryAfter}s`);
}
}Environment Configuration
The library requires an API token which should be loaded from environment variables for security.
Quick Setup:
- Copy
.env.exampleto.env - Add your token:
BOT_API_TOKEN=your_token_here - Bun: Auto-loaded ✅ | Node.js: Use
dotenvor--env-file
See the complete Environment Variables Guide for:
- Node.js, Bun, Deno setup
- Production deployment (Docker, Kubernetes, AWS, Vercel, etc.)
- Best practices and security tips
- Troubleshooting common issues
Requirements
- Node.js: 20.0.0 or higher
- Bun: 1.3.0 or higher
- Browsers: Modern browsers with native fetch support
Development
Built with the 2025 modern stack:
- Bun Test - 35x faster than Jest
- Biome - All-in-one formatter & linter
- tsup - Fast TypeScript bundler
- pnpm - Efficient package management
# Install dependencies
pnpm install
# Run tests
bun test
# Build
pnpm build
# Lint and format
pnpm lint:fixLicense
MIT
Links
Built with the 2025 modern JavaScript stack
