@allratestoday/sdk 1.2.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,8 +1,25 @@
1
1
  # @allratestoday/sdk
2
2
 
3
- Official JavaScript/TypeScript SDK for the [AllRatesToday](https://allratestoday.com) exchange rate API.
3
+ [![npm version](https://img.shields.io/npm/v/@allratestoday/sdk.svg)](https://www.npmjs.com/package/@allratestoday/sdk)
4
+ [![license](https://img.shields.io/npm/l/@allratestoday/sdk.svg)](https://github.com/allratestoday/exchange-rates-api/blob/main/LICENSE)
5
+ [![TypeScript](https://badges.frapsoft.com/typescript/code/typescript.svg?v=101)](https://www.typescriptlang.org/)
6
+ [![zero dependencies](https://img.shields.io/badge/dependencies-0-brightgreen.svg)](https://www.npmjs.com/package/@allratestoday/sdk)
4
7
 
5
- Real-time mid-market exchange rates for 160+ currencies, sourced from Reuters (Refinitiv) and interbank market feeds.
8
+ **The most elegant way to access real-time mid-market exchange rates in Node.js and browsers**
9
+
10
+ ## Why Choose This Client?
11
+
12
+ - **Lightning Fast**: Zero runtime dependencies, pure TypeScript performance
13
+ - **Real-Time Data**: Rates updated every 60 seconds from Reuters (Refinitiv) and interbank feeds
14
+ - **Mid-Market Rates**: The true interbank rate — no hidden spread or markup
15
+ - **160+ Currencies**: Major, minor, and exotic currency pairs
16
+ - **Type-Safe**: Full TypeScript support with intelligent autocomplete
17
+ - **Universal**: Works seamlessly in Node.js 18+ and modern browsers
18
+ - **Developer-Friendly**: Simple API, extensive documentation, and great DX
19
+
20
+ ## Get Your API Key
21
+
22
+ Ready to start? Get your free API key from [allratestoday.com/register](https://allratestoday.com/register) — no credit card required. The free tier includes 300 requests/month.
6
23
 
7
24
  ## Installation
8
25
 
@@ -10,50 +27,299 @@ Real-time mid-market exchange rates for 160+ currencies, sourced from Reuters (R
10
27
  npm install @allratestoday/sdk
11
28
  ```
12
29
 
30
+ ```bash
31
+ yarn add @allratestoday/sdk
32
+ ```
33
+
34
+ ```bash
35
+ pnpm add @allratestoday/sdk
36
+ ```
37
+
13
38
  ## Quick Start
14
39
 
15
- Get your free API key at [allratestoday.com/register](https://allratestoday.com/register) (300 requests/month free).
40
+ Get up and running in seconds:
16
41
 
17
42
  ```typescript
18
43
  import AllRatesToday from '@allratestoday/sdk';
19
44
 
20
- const client = new AllRatesToday({ apiKey: 'art_live_your_key_here' });
45
+ // Set your API key
46
+ const client = new AllRatesToday({ apiKey: 'YOUR_API_KEY' });
47
+
48
+ // Get latest exchange rates
49
+ const { rates } = await client.latest({ base: 'USD', symbols: ['EUR', 'GBP', 'JPY'] });
50
+ console.log(rates);
51
+ ```
52
+
53
+ ## API Reference
54
+
55
+ - [Latest Rates](#-latest-exchange-rates) — Get current exchange rates
56
+ - [Historical Data](#-historical-exchange-rates) — Fetch rates for specific dates
57
+ - [Currency Conversion](#-currency-conversion) — Convert between currencies
58
+ - [Time Series](#-time-series-data) — Get rates over date ranges
59
+ - [Available Currencies](#-available-currencies) — List all supported currencies
60
+ - [Single Rate](#-single-rate) — Get one currency pair
61
+ - [Historical by Period](#-historical-rates-by-period) — Preset period lookups
62
+
63
+ ---
64
+
65
+ ### Latest Exchange Rates
66
+
67
+ Get the most current exchange rates with a single call:
68
+
69
+ ```typescript
70
+ // Get all rates from USD
71
+ const data = await client.latest();
72
+ console.log(data);
73
+
74
+ // Target specific currencies
75
+ const data = await client.latest({ base: 'USD', symbols: ['CHF', 'GBP', 'JPY'] });
76
+ console.log(data);
77
+ ```
78
+
79
+ **Response:**
80
+
81
+ ```javascript
82
+ {
83
+ base: 'USD',
84
+ date: '2026-04-09T14:30:00Z',
85
+ rates: {
86
+ CHF: 0.8821,
87
+ GBP: 0.7891,
88
+ JPY: 151.42
89
+ }
90
+ }
91
+ ```
92
+
93
+ ### Historical Exchange Rates
94
+
95
+ Travel back in time to get rates from any date:
96
+
97
+ ```typescript
98
+ // Using a string date
99
+ const data = await client.forDate('2026-01-15', { base: 'USD', symbols: ['EUR'] });
100
+ console.log(data);
101
+
102
+ // Using a Date object
103
+ const data = await client.forDate(new Date('2026-01-15'), { base: 'EUR', symbols: ['USD', 'GBP'] });
104
+ console.log(data);
105
+ ```
106
+
107
+ **Response:**
108
+
109
+ ```javascript
110
+ {
111
+ base: 'USD',
112
+ date: '2026-01-15',
113
+ rates: {
114
+ EUR: 0.9187
115
+ }
116
+ }
117
+ ```
118
+
119
+ ### Currency Conversion
120
+
121
+ Convert any amount between currencies — including historical conversions:
122
+
123
+ ```typescript
124
+ // Current conversion
125
+ const result = await client.convert('USD', 'EUR', 1000);
126
+ console.log(result);
127
+ ```
128
+
129
+ **Response:**
130
+
131
+ ```javascript
132
+ {
133
+ from: 'USD',
134
+ to: 'EUR',
135
+ amount: 1000,
136
+ result: 923.4,
137
+ rate: 0.9234
138
+ }
139
+ ```
140
+
141
+ ```typescript
142
+ // Historical conversion — what was $1,000 worth on Jan 15?
143
+ const result = await client.convert('USD', 'EUR', 1000, { date: '2026-01-15' });
144
+ console.log(result);
145
+ ```
146
+
147
+ **Response:**
148
+
149
+ ```javascript
150
+ {
151
+ from: 'USD',
152
+ to: 'EUR',
153
+ amount: 1000,
154
+ result: 918.7,
155
+ rate: 0.9187,
156
+ date: '2026-01-15'
157
+ }
158
+ ```
159
+
160
+ ### Available Currencies
161
+
162
+ Discover all 160+ supported currency symbols and names:
163
+
164
+ ```typescript
165
+ const data = await client.symbols();
166
+ console.log(data);
167
+ ```
168
+
169
+ **Response:**
170
+
171
+ ```javascript
172
+ {
173
+ symbols: {
174
+ AED: 'United Arab Emirates Dirham',
175
+ AFN: 'Afghan Afghani',
176
+ ALL: 'Albanian Lek',
177
+ AMD: 'Armenian Dram',
178
+ AUD: 'Australian Dollar',
179
+ BDT: 'Bangladeshi Taka',
180
+ BRL: 'Brazilian Real',
181
+ CAD: 'Canadian Dollar',
182
+ CHF: 'Swiss Franc',
183
+ CNY: 'Chinese Yuan',
184
+ EUR: 'Euro',
185
+ GBP: 'British Pound Sterling',
186
+ INR: 'Indian Rupee',
187
+ JPY: 'Japanese Yen',
188
+ KRW: 'South Korean Won',
189
+ USD: 'United States Dollar',
190
+ // ...160+ currencies
191
+ }
192
+ }
193
+ ```
194
+
195
+ ### Time Series Data
196
+
197
+ Get exchange rates over a date range for trend analysis and charting:
198
+
199
+ ```typescript
200
+ const data = await client.timeSeries('2026-01-01', '2026-03-31', {
201
+ base: 'USD',
202
+ symbols: ['EUR', 'GBP'],
203
+ });
204
+ console.log(data);
205
+ ```
206
+
207
+ **Response:**
208
+
209
+ ```javascript
210
+ {
211
+ base: 'USD',
212
+ startDate: '2026-01-01',
213
+ endDate: '2026-03-31',
214
+ rates: {
215
+ '2026-01-01': {
216
+ EUR: 0.9187,
217
+ GBP: 0.7834
218
+ },
219
+ '2026-01-02': {
220
+ EUR: 0.9195,
221
+ GBP: 0.7841
222
+ },
223
+ '2026-01-03': {
224
+ EUR: 0.9201,
225
+ GBP: 0.7856
226
+ },
227
+ // ...
228
+ }
229
+ }
230
+ ```
231
+
232
+ ### Single Rate
233
+
234
+ Get a single exchange rate between two currencies:
21
235
 
22
- // Get exchange rate
236
+ ```typescript
23
237
  const rate = await client.getRate('USD', 'EUR');
24
- console.log(`1 USD = ${rate.rate} EUR`);
238
+ console.log(rate);
239
+ ```
240
+
241
+ **Response:**
242
+
243
+ ```javascript
244
+ {
245
+ from: { currency: 'USD', amount: 1 },
246
+ to: { currency: 'EUR', amount: 0.9234 },
247
+ rate: 0.9234,
248
+ source: 'wise'
249
+ }
250
+ ```
251
+
252
+ ```typescript
253
+ // With amount
254
+ const rate = await client.getRate('USD', 'EUR', 500);
255
+ console.log(`$500 = €${rate.to.amount}`);
256
+ ```
257
+
258
+ ### Historical Rates by Period
25
259
 
26
- // Convert amount
27
- const result = await client.convert('USD', 'EUR', 100);
28
- console.log(`$100 = €${result.result}`);
260
+ Get historical rates using preset periods — no date math needed:
29
261
 
30
- // Get historical rates
262
+ ```typescript
31
263
  const history = await client.getHistoricalRates('USD', 'EUR', '30d');
32
- console.log(history.rates);
264
+ console.log(history);
33
265
  ```
34
266
 
35
- ## API Reference
267
+ **Response:**
36
268
 
37
- ### `new AllRatesToday(options)`
269
+ ```javascript
270
+ {
271
+ source: 'USD',
272
+ target: 'EUR',
273
+ period: '30d',
274
+ current: { rate: 0.9234, time: '2026-04-09T14:30:00Z' },
275
+ rates: [
276
+ { rate: 0.9198, time: '2026-03-10T00:00:00Z' },
277
+ { rate: 0.9210, time: '2026-03-11T00:00:00Z' },
278
+ // ...
279
+ ]
280
+ }
281
+ ```
38
282
 
39
- | Option | Type | Default | Description |
40
- |--------|------|---------|-------------|
41
- | `apiKey` | `string` | **required** | Your API key ([register free](https://allratestoday.com/register)) |
42
- | `baseUrl` | `string` | `https://allratestoday.com` | API base URL |
43
- | `timeout` | `number` | `10000` | Request timeout in ms |
283
+ **Available periods:** `1d` | `7d` | `30d` | `1y`
44
284
 
45
- ### Methods
285
+ ---
46
286
 
47
- | Method | Description |
48
- |--------|-------------|
49
- | `getRate(from, to, amount?)` | Get exchange rate between two currencies |
50
- | `convert(from, to, amount)` | Convert amount between currencies |
51
- | `getRates(source, target)` | Get rate data with metadata |
52
- | `getHistoricalRates(source, target, period)` | Historical rates (1d/7d/30d/1y) |
287
+ ## Configuration
288
+
289
+ ```typescript
290
+ const client = new AllRatesToday({
291
+ apiKey: 'YOUR_API_KEY', // Required — get one free at allratestoday.com/register
292
+ baseUrl: 'https://allratestoday.com', // Optional — API base URL
293
+ timeout: 10000, // Optional — request timeout in ms
294
+ });
295
+ ```
296
+
297
+ | Option | Type | Default | Description |
298
+ | --------- | -------- | --------------------------- | -------------------------------- |
299
+ | `apiKey` | `string` | — | Your API key |
300
+ | `baseUrl` | `string` | `https://allratestoday.com` | API base URL |
301
+ | `timeout` | `number` | `10000` | Request timeout in milliseconds |
53
302
 
54
- All methods require an API key.
303
+ ---
55
304
 
56
- ### Error Handling
305
+ ## Per-Request API Key Override
306
+
307
+ Every method supports a per-request API key override — useful for multi-tenant apps:
308
+
309
+ ```typescript
310
+ const client = new AllRatesToday({ apiKey: 'default_key' });
311
+
312
+ // Override for a specific request
313
+ const data = await client.latest({ apiKey: 'other_users_key' });
314
+ const result = await client.convert('USD', 'EUR', 100, { apiKey: 'tenant_key' });
315
+ const { symbols } = await client.symbols({ apiKey: 'another_key' });
316
+ ```
317
+
318
+ ---
319
+
320
+ ## Error Handling
321
+
322
+ All errors are thrown as `AllRatesTodayError` with an optional HTTP status code:
57
323
 
58
324
  ```typescript
59
325
  import AllRatesToday, { AllRatesTodayError } from '@allratestoday/sdk';
@@ -62,28 +328,170 @@ try {
62
328
  const rate = await client.getRate('USD', 'INVALID');
63
329
  } catch (err) {
64
330
  if (err instanceof AllRatesTodayError) {
65
- console.log(err.message); // Error description
66
- console.log(err.status); // HTTP status code
331
+ console.log(err.message); // "Currency not found"
332
+ console.log(err.status); // 404
333
+ }
334
+ }
335
+ ```
336
+
337
+ | Status | Meaning |
338
+ | ------ | ------------------------------------ |
339
+ | — | Missing API key (thrown before request) |
340
+ | `401` | Invalid API key |
341
+ | `404` | Currency code not found |
342
+ | `429` | Rate limit exceeded |
343
+ | `500` | Server error |
344
+
345
+ ---
346
+
347
+ ## TypeScript
348
+
349
+ The SDK is written in TypeScript and ships full type definitions. All interfaces are exported:
350
+
351
+ ```typescript
352
+ import AllRatesToday, {
353
+ AllRatesTodayError,
354
+ AllRatesTodayOptions,
355
+ RequestOptions,
356
+ RateResponse,
357
+ AuthRateResponse,
358
+ LatestOptions,
359
+ LatestResponse,
360
+ ForDateOptions,
361
+ ForDateResponse,
362
+ ConvertOptions,
363
+ ConvertResponse,
364
+ TimeSeriesOptions,
365
+ TimeSeriesResponse,
366
+ HistoricalRateResponse,
367
+ SymbolsResponse,
368
+ } from '@allratestoday/sdk';
369
+ ```
370
+
371
+ ---
372
+
373
+ ## CommonJS
374
+
375
+ ```javascript
376
+ const AllRatesToday = require('@allratestoday/sdk').default;
377
+
378
+ const client = new AllRatesToday({ apiKey: 'YOUR_API_KEY' });
379
+ client.latest().then(data => console.log(data.rates));
380
+ ```
381
+
382
+ ---
383
+
384
+ ## Examples
385
+
386
+ ### Currency Dropdown (React)
387
+
388
+ ```tsx
389
+ import { useEffect, useState } from 'react';
390
+ import AllRatesToday from '@allratestoday/sdk';
391
+
392
+ const client = new AllRatesToday({ apiKey: 'YOUR_API_KEY' });
393
+
394
+ function CurrencySelect({ value, onChange }) {
395
+ const [currencies, setCurrencies] = useState([]);
396
+
397
+ useEffect(() => {
398
+ client.symbols().then(({ symbols }) => {
399
+ setCurrencies(
400
+ Object.entries(symbols).map(([code, name]) => ({ code, name }))
401
+ );
402
+ });
403
+ }, []);
404
+
405
+ return (
406
+ <select value={value} onChange={e => onChange(e.target.value)}>
407
+ {currencies.map(c => (
408
+ <option key={c.code} value={c.code}>{c.code} — {c.name}</option>
409
+ ))}
410
+ </select>
411
+ );
412
+ }
413
+ ```
414
+
415
+ ### Price Display with Caching
416
+
417
+ ```typescript
418
+ import AllRatesToday from '@allratestoday/sdk';
419
+
420
+ const client = new AllRatesToday({ apiKey: 'YOUR_API_KEY' });
421
+ let cache: { rates: Record<string, number>; time: number } | null = null;
422
+ const TTL = 5 * 60 * 1000; // 5 minutes
423
+
424
+ async function getPrice(amountUSD: number, currency: string): Promise<string> {
425
+ if (!cache || Date.now() - cache.time > TTL) {
426
+ const data = await client.latest({ base: 'USD' });
427
+ cache = { rates: data.rates, time: Date.now() };
67
428
  }
429
+ const converted = (amountUSD * cache.rates[currency]).toFixed(2);
430
+ return `${converted} ${currency}`;
68
431
  }
432
+
433
+ console.log(await getPrice(99.99, 'EUR')); // "92.32 EUR"
434
+ console.log(await getPrice(99.99, 'GBP')); // "78.90 GBP"
69
435
  ```
70
436
 
437
+ ### Historical Chart Data
438
+
439
+ ```typescript
440
+ import AllRatesToday from '@allratestoday/sdk';
441
+
442
+ const client = new AllRatesToday({ apiKey: 'YOUR_API_KEY' });
443
+
444
+ const data = await client.timeSeries('2026-01-01', '2026-04-01', {
445
+ base: 'USD',
446
+ symbols: ['EUR'],
447
+ });
448
+
449
+ // Format for charting libraries (Chart.js, Recharts, etc.)
450
+ const labels = Object.keys(data.rates).sort();
451
+ const values = labels.map(date => data.rates[date].EUR);
452
+
453
+ console.log({ labels, values });
454
+ // { labels: ['2026-01-01', '2026-01-02', ...], values: [0.92, 0.921, ...] }
455
+ ```
456
+
457
+ ---
458
+
459
+ ## Methods Reference
460
+
461
+ | Method | Description |
462
+ | ------ | ----------- |
463
+ | `latest(options?)` | Get latest rates for one or more currencies |
464
+ | `forDate(date, options?)` | Get rates for a specific historical date |
465
+ | `convert(from, to, amount, options?)` | Convert amount between currencies (supports historical date) |
466
+ | `timeSeries(start, end, options?)` | Get rates across a custom date range |
467
+ | `symbols(options?)` | List all 160+ supported currency codes and names |
468
+ | `getRate(from, to, amount?, options?)` | Get a single exchange rate |
469
+ | `getRates(source, target, options?)` | Get rates with full metadata |
470
+ | `getHistoricalRates(source, target, period?, options?)` | Historical rates by preset period (1d/7d/30d/1y) |
471
+
472
+ ---
473
+
71
474
  ## Pricing
72
475
 
73
- | Plan | Requests/Month | Price |
74
- |------|---------------|-------|
75
- | Free | 300 | Free |
76
- | Small | 5,000 | €4.99/mo |
77
- | Medium | 10,000 | €9.99/mo |
78
- | Large | 100,000 | €49.99/mo |
476
+ | Plan | Requests/Month | Price |
477
+ | ------ | -------------- | ---------- |
478
+ | Free | 300 | Free |
479
+ | Small | 5,000 | €4.99/mo |
480
+ | Medium | 10,000 | €9.99/mo |
481
+ | Large | 100,000 | €49.99/mo |
482
+
483
+ All plans get the same features: real-time rates (60s updates), 160+ currencies, any base currency, full API access. Plans differ only in request volume.
484
+
485
+ ---
79
486
 
80
487
  ## Links
81
488
 
82
- - [API Documentation](https://allratestoday.com/docs)
489
+ - [API Documentation](https://allratestoday.com/developers)
83
490
  - [Register (Free)](https://allratestoday.com/register)
84
491
  - [Dashboard](https://allratestoday.com/profile)
85
492
  - [Pricing](https://allratestoday.com/pricing)
86
493
  - [Status](https://allratestoday.com/status)
494
+ - [GitHub](https://github.com/allratestoday/exchange-rates-api)
87
495
 
88
496
  ## License
89
497