@allratestoday/sunat-exchange-rate 0.0.0-stage → 1.3.29

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 AllRatesToday (https://allratestoday.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,323 @@
1
- # Temporary Holding Version
1
+ # SUNAT Exchange Rates API — sunat-exchange-rate
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![npm version](https://img.shields.io/npm/v/@allratestoday%2Fsunat-exchange-rate.svg)](https://www.npmjs.com/package/@allratestoday/sunat-exchange-rate)
4
+ [![license](https://img.shields.io/npm/l/@allratestoday%2Fsunat-exchange-rate.svg)](https://github.com/AllRates-Today/sunat-exchange-rate/blob/main/LICENSE)
5
+ [![zero dependencies](https://img.shields.io/badge/dependencies-0-brightgreen.svg)](https://www.npmjs.com/package/@allratestoday/sunat-exchange-rate)
6
+ [![TypeScript](https://img.shields.io/badge/TypeScript-types%20included-3178C6.svg)](https://www.typescriptlang.org/)
7
+ [![USD/PEN today](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fallratestoday.com%2Fapi%2Fopen%2Fcentral-bank%2Fsunat%3Fsource%3DUSD%26target%3DPEN&query=%24.rate&label=USD%2FPEN%20published%20by%20SUNAT&color=0A7E8C&cacheSeconds=3600)](https://allratestoday.com/tax-authority-rates-api/sunat/)
8
+ [![rate date](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fallratestoday.com%2Fapi%2Fopen%2Fcentral-bank%2Fsunat%3Fsource%3DUSD%26target%3DPEN&query=%24.rate_date&label=rate%20date&color=555&cacheSeconds=3600)](https://allratestoday.com/tax-authority-rates-api/sunat/)
9
+
10
+ **Official SUNAT (Peru) daily exchange rates for Node.js and TypeScript. The published tax authority rates behind tax filings, customs valuations, audits, and compliant invoicing — not market estimates, but the numbers SUNAT itself prints, every business day.**
11
+
12
+ ## 🚀 Why this client?
13
+
14
+ - 🏛️ **Official published rates** — SUNAT's own table, with the publisher's own `rate_date` on every response
15
+ - 📅 **History from 2026** — this source publishes only its current table, so the archive builds forward from the day we started collecting
16
+ - 🔀 **Published vs derived, always flagged** — computed inverse/cross pairs carry `derived: true`, never mixed with official prints
17
+ - ⚡ **Zero dependencies** — pure ESM + CJS over global `fetch`; Node 18+, Bun, Deno, and edge runtimes
18
+ - 🔷 **Type-safe** — full TypeScript definitions shipped with the package
19
+ - 🧾 **Compliance-grade metadata** — `rate_type`, publication date, and source disclaimer on every response
20
+
21
+ > **Official rate, not mid-market:** every value here is a number SUNAT itself published, fixed once printed and carrying the tax authority's own `rate_date` — what filings and audits require. Need the live mid-market rate for pricing or display instead? Use the [mid-market API](https://allratestoday.com/docs/) or [`@allratestoday/sdk`](https://www.npmjs.com/package/@allratestoday/sdk). The two can diverge by several percent.
22
+
23
+ ## 🔑 Get your API key
24
+
25
+ Get a free API key at [allratestoday.com/register](https://allratestoday.com/register) — no credit card required. Latest rates are on every plan, including free.
26
+
27
+ ## 📈 Latest published table
28
+
29
+ [![Latest SUNAT table](https://allratestoday.com/api/open/central-bank/sunat/table.svg)](https://allratestoday.com/tax-authority-rates-api/sunat/)
30
+
31
+ The image is rendered live from the keyless endpoint, so it follows the tax authority's publications on npm and on GitHub alike without a new release. The same table as text is folded below for search and copy-paste: on GitHub [a daily Action](.github/workflows/daily-table.yml) refreshes it whenever the tax authority publishes a new table; on npm it is as of the package's publish date.
32
+
33
+ <!-- daily-table:start -->
34
+ <details>
35
+ <summary>Table as text: published 2026-11-06 by SUNAT — 2 rates. Updated 2026-11-08.</summary>
36
+
37
+ | Base | Quote | Type | Rate |
38
+ | --- | --- | --- | ---: |
39
+ | USD | PEN | buy | 3.446 |
40
+ | USD | PEN | sell | 3.452 |
41
+
42
+ </details>
43
+
44
+ Source: [Official rates published by SUNAT, served by AllRatesToday](https://allratestoday.com/tax-authority-rates-api/sunat/). Rates are as printed by the tax authority; AllRatesToday is not affiliated with it.
45
+ <!-- daily-table:end -->
46
+
47
+ ## 📦 Installation
48
+
49
+ ```bash
50
+ npm install @allratestoday/sunat-exchange-rate
51
+ ```
52
+
53
+ ```bash
54
+ yarn add @allratestoday/sunat-exchange-rate
55
+ ```
56
+
57
+ ```bash
58
+ pnpm add @allratestoday/sunat-exchange-rate
59
+ ```
60
+
61
+ Requires Node 18+ (global `fetch`); also runs on Bun, Deno and edge runtimes. Scoped mirror of [`sunat-exchange-rate`](https://www.npmjs.com/package/sunat-exchange-rate) — same code, same versions.
62
+
63
+ ## 🏁 Quick start
64
+
65
+ ```js
66
+ import { getRate } from '@allratestoday/sunat-exchange-rate';
67
+
68
+ const pair = await getRate('USD', 'PEN', { apiKey: 'art_live_...' });
69
+ console.log(pair.rate, pair.rate_date); // the official SUNAT rate, on the tax authority's own date
70
+ ```
71
+
72
+ ## 📚 API reference
73
+
74
+ - [Latest pair rate](#latest-pair-rate) — one pair from the latest published table
75
+ - [Full published table](#full-published-table) — everything the tax authority printed, in one call
76
+ - [Table for a date](#table-for-a-date) — the official table for an invoice or filing date
77
+ - [Daily time series](#daily-time-series) — one pair across a date range
78
+
79
+ ---
80
+
81
+ ### Latest pair rate
82
+
83
+ Free plan and up. Pairs the tax authority does not print directly are resolved from its table and flagged (see *Published vs derived rates* below).
84
+
85
+ ```js
86
+ const pair = await getRate('USD', 'PEN', { apiKey: 'art_live_...' });
87
+ ```
88
+
89
+ **Response:**
90
+
91
+ ```javascript
92
+ {
93
+ bank: 'sunat',
94
+ name: 'SUNAT',
95
+ rate_date: '2026-11-06', // SUNAT's own publication date
96
+ source: 'USD',
97
+ target: 'PEN',
98
+ rate: 3.452,
99
+ rate_type: 'sell',
100
+ derived: false,
101
+ method: 'published',
102
+ disclaimer: 'Official rates as published by the named central bank. On weekends/holidays the most recent published rate_date is returned.'
103
+ }
104
+ ```
105
+
106
+ ### Full published table
107
+
108
+ Free plan and up. The complete table for the latest publication date.
109
+
110
+ ```js
111
+ import { getLatestRates } from '@allratestoday/sunat-exchange-rate';
112
+
113
+ const table = await getLatestRates({ apiKey: 'art_live_...' });
114
+ console.log(table.rate_date, table.rates.length);
115
+ ```
116
+
117
+ **Response:**
118
+
119
+ ```javascript
120
+ {
121
+ bank: 'sunat',
122
+ name: 'SUNAT',
123
+ rate_date: '2026-11-06',
124
+ rates: [
125
+ { "base": "USD", "quote": "PEN", "type": "sell", "value": 3.452 },
126
+ { "base": "USD", "quote": "PEN", "type": "buy", "value": 3.446 },
127
+ // … the rest of the published table (1 currency vs PEN)
128
+ ],
129
+ disclaimer: '…'
130
+ }
131
+ ```
132
+
133
+ ### Table for a date
134
+
135
+ Paid plans. The official table for any date since 2026 — weekends and holidays return the most recent published date, flagged via `published_on_requested_date`, which is exactly the in-force rate a filing needs.
136
+
137
+ ```js
138
+ import { getRatesForDate } from '@allratestoday/sunat-exchange-rate';
139
+
140
+ const day = await getRatesForDate('2026-06-30', { apiKey: 'art_live_...' });
141
+ // Optionally narrow to one pair:
142
+ const one = await getRatesForDate('2026-06-30', { apiKey: 'art_live_...', source: 'USD', target: 'PEN' });
143
+ ```
144
+
145
+ **Response:**
146
+
147
+ ```javascript
148
+ {
149
+ bank: 'sunat',
150
+ requested_date: '2026-06-30',
151
+ rate_date: '2026-06-30', // the date actually published
152
+ published_on_requested_date: true, // false when a weekend/holiday fell back
153
+ rates: [ /* the full table for that date */ ],
154
+ disclaimer: '…'
155
+ }
156
+ ```
157
+
158
+ ### Daily time series
159
+
160
+ Paid plans. One resolved rate per publication date — ready for charting, revaluation runs, or audit workpapers.
161
+
162
+ ```js
163
+ import { getHistory } from '@allratestoday/sunat-exchange-rate';
164
+
165
+ const series = await getHistory(
166
+ { source: 'USD', target: 'PEN', from: '2026-01-01', to: '2026-11-06' },
167
+ { apiKey: 'art_live_...' }
168
+ );
169
+ ```
170
+
171
+ **Response:**
172
+
173
+ ```javascript
174
+ {
175
+ bank: 'sunat',
176
+ source: 'USD',
177
+ target: 'PEN',
178
+ from: '2026-01-01',
179
+ to: '2026-11-06',
180
+ count: 152,
181
+ rates: [
182
+ // one entry per publication date
183
+ { date: '2026-11-06', rate: 3.452, rate_type: 'sell', derived: false, method: 'published' },
184
+ // …
185
+ ],
186
+ disclaimer: '…'
187
+ }
188
+ ```
189
+
190
+ Pass `{ symbol: 'USD' }` instead of `source`/`target` to get the raw published rows for one currency (all rate types, no pair resolution).
191
+
192
+ ---
193
+
194
+ ## 🗺️ Currencies covered
195
+
196
+ SUNAT currently publishes rates covering **1 currency** against the PEN (as of the latest table):
197
+
198
+ 🇺🇸 `USD`
199
+
200
+ ## 🏛️ Source
201
+
202
+ SUNAT, Peru's national tax and customs authority, publishes the tipo de cambio oficial every business day: the closing buy and sell rate for the U.S. dollar fixed by the banking regulator SBS on the previous business day. It is the rate Peruvian tax rules require for converting dollar invoices into soles — the buy rate for income and assets, the sell rate for expenses and liabilities — and the rate customs declarations and accounting records cite. The API serves it as USD per soles buy and sell under the date SUNAT publishes it, which is the day the rate applies.
203
+
204
+ - Publisher's own page: [Tipo de cambio oficial (consulta)](https://e-consulta.sunat.gob.pe/cl-at-ittipcam/tcS01Alias) · [www.sunat.gob.pe](https://www.sunat.gob.pe/)
205
+ - Publication: every business day; the exact schedule, freshness status and any current delay are on the [SUNAT rates page](https://allratestoday.com/tax-authority-rates-api/sunat/)
206
+ - Values are stored unmodified, with the publisher's own `rate_date` on every row — see the [methodology](https://allratestoday.com/official-rates-methodology/)
207
+
208
+ ## 🧭 Reading the numbers
209
+
210
+ - `value` is always **quote currency per 1 unit of base currency** (`base: "EUR", quote: "USD", value: 1.15` means 1 EUR = 1.15 USD).
211
+ - SUNAT prints **soles per 1 US dollar** (`base: "USD", quote: "PEN"`), one `buy` and one `sell` rate: the tipo de cambio oficial used for Peruvian tax and customs. A table dated D carries the SBS closing rate of the previous business day, stored under the date SUNAT gives it.
212
+ - Need the other way round? Ask `getRate(target, source)` and the API inverts or crosses for you, flagged `derived: true` — never divide a published rate yourself in a compliance workflow.
213
+ - `rate_type` tells you which of the tax authority's series a row belongs to (`sell` here); some publishers print buy/sell or several fixings for the same pair.
214
+
215
+ ## 🧩 ERP & accounting systems
216
+
217
+ Loading the official SUNAT rate into an accounting system is a supported workflow, not a hack. Step-by-step guides with the direction each system expects:
218
+
219
+ - [Dynamics 365 Business Central](https://allratestoday.com/docs/integrations/business-central/) — built-in Currency Exchange Rate Service, no code
220
+ - [Xero](https://allratestoday.com/docs/integrations/xero/) · [QuickBooks Online](https://allratestoday.com/docs/integrations/quickbooks/) · [SAP S/4HANA and ECC](https://allratestoday.com/docs/integrations/sap/) · [Odoo](https://allratestoday.com/docs/integrations/odoo/)
221
+
222
+ The same keyed endpoints return `?format=csv`, `?format=xml` and `?format=xlsx`, and accept the key as `?api_key=` on the URL for importers that cannot send headers:
223
+
224
+ ```bash
225
+ curl "https://allratestoday.com/api/v1/central-bank/sunat/latest?format=xml&api_key=art_live_..."
226
+ ```
227
+
228
+ ## 🤖 AI agents
229
+
230
+ - MCP server: `npx -y @allratestoday/central-bank-mcp` (stdio) or the hosted endpoint `https://allratestoday.com/api/mcp` — tools for official rates, history, cross-bank comparison and publication calendars
231
+ - Already using the general SDK or MCP server? Since 2026-10-01 [`@allratestoday/sdk`](https://www.npmjs.com/package/@allratestoday/sdk) 1.4+ has `officialRates('sunat')` and [`@allratestoday/mcp-server`](https://www.npmjs.com/package/@allratestoday/mcp-server) 0.6+ has a `get_official_rates` tool — both return this source's latest table with no key, so you can add it without a second dependency
232
+ - Claude Code plugin (no key): `/plugin marketplace add AllRates-Today/claude-code-plugin` then `/plugin install allratestoday@allratestoday` — bundles both MCP servers plus an `/official-rate sunat ...` command
233
+ - Evaluating? Agents can self-issue a trial key with no signup: `POST https://allratestoday.com/api/agent/keys` (send `Content-Type: application/json`) returns a 100-call key valid 7 days that covers the free-plan methods (latest table, pairs); dated tables and history need a [paid plan](https://allratestoday.com/pricing/). The MCP servers expose it as `request_trial_key`
234
+ - Machine-readable site guide: [llms.txt](https://allratestoday.com/llms.txt) · [for-ai-agents](https://allratestoday.com/for-ai-agents/)
235
+
236
+ ## ⚖️ Published vs derived rates
237
+
238
+ If SUNAT does not print a pair directly, the API resolves it from the tax authority's own table and says so — official and computed values are never confused:
239
+
240
+ | `method` | `derived` | Meaning |
241
+ | --- | --- | --- |
242
+ | `published` | `false` | The tax authority printed this pair directly |
243
+ | `inverse` | `true` | Computed as 1 ÷ the published opposite direction |
244
+ | `cross` | `true` | Computed via PEN from two published rates |
245
+
246
+ ## 🛡️ Error handling
247
+
248
+ Errors are thrown as `Error` with `status` (HTTP code) and `body` (the API's JSON error) attached:
249
+
250
+ ```js
251
+ try {
252
+ const pair = await getRate('USD', 'XXX', { apiKey: 'art_live_...' });
253
+ } catch (err) {
254
+ console.log(err.message); // human-readable reason
255
+ console.log(err.status); // e.g. 404
256
+ }
257
+ ```
258
+
259
+ | Status | Meaning |
260
+ | ------ | ------- |
261
+ | — | Missing `apiKey` (thrown before any request) |
262
+ | `400` | Malformed date or parameters |
263
+ | `401` | Invalid API key |
264
+ | `403` | Endpoint needs a [paid plan](https://allratestoday.com/pricing/) (historical dates & series) |
265
+ | `404` | Pair or date range not covered by SUNAT |
266
+ | `429` | Monthly quota exceeded |
267
+
268
+ ## 🔷 TypeScript
269
+
270
+ Full definitions ship with the package — no `@types` install:
271
+
272
+ ```ts
273
+ import type { LatestRates, PairRate, DatedRates, RateEntry, HistoryQuery, RequestOptions } from '@allratestoday/sunat-exchange-rate';
274
+ ```
275
+
276
+ ## 📦 CommonJS
277
+
278
+ ```javascript
279
+ const { getRate } = require('@allratestoday/sunat-exchange-rate');
280
+
281
+ getRate('USD', 'PEN', { apiKey: 'art_live_...' }).then((pair) => console.log(pair.rate));
282
+ ```
283
+
284
+ ## 💡 Quota tips
285
+
286
+ - Rates change once per business day — cache the published table locally and a small monthly quota goes a long way.
287
+ - Every request counts toward your AllRatesToday quota, shared across all AllRatesToday endpoints on your key.
288
+
289
+ ## 📖 Methods reference
290
+
291
+ | Method | Plan | Description |
292
+ | ------ | ---- | ----------- |
293
+ | `getRate(source, target, { apiKey })` | Free | Latest rate for one pair, resolved from the published table |
294
+ | `getLatestRates({ apiKey })` | Free | The tax authority's full latest published table |
295
+ | `getRatesForDate(date, { apiKey, source?, target? })` | Paid | The official table (or one pair) for a YYYY-MM-DD date |
296
+ | `getHistory({ symbol \| source+target, from?, to? }, { apiKey })` | Paid | Daily series since 2026 |
297
+
298
+ ## 📥 Bulk data (no key)
299
+
300
+ Need the whole archive rather than an API call? The same published tables are mirrored daily as open data:
301
+
302
+ - Hugging Face: [AllRatesToday/central-bank-exchange-rates](https://huggingface.co/datasets/AllRatesToday/central-bank-exchange-rates) — one CSV per institution (`rates/sunat.csv`)
303
+ - Kaggle: [allratestoday/central-bank-exchange-rates](https://www.kaggle.com/datasets/allratestoday/central-bank-exchange-rates)
304
+ - Raw JSON on GitHub: `https://raw.githubusercontent.com/AllRates-Today/central-bank-exchange-rates/main/data/sunat/latest.json`
305
+
306
+ ## 🔗 Links
307
+
308
+ - **Website:** [allratestoday.com](https://allratestoday.com)
309
+ - [SUNAT rates page](https://allratestoday.com/tax-authority-rates-api/sunat/) — live table, publication cadence, FAQ
310
+ - [All tax authority sources](https://allratestoday.com/tax-authority-rates-api/)
311
+ - [Download central bank rates](https://allratestoday.com/download-central-bank-rates/) — every bank's latest table and history as Excel, CSV or JSON
312
+ - [Package docs on the site](https://allratestoday.com/docs/sdk/sunat-exchange-rate/) · [ERP integration guides](https://allratestoday.com/docs/integrations/)
313
+ - [API documentation](https://allratestoday.com/docs/#central-bank) · [Interactive reference](https://allratestoday.com/api-reference/) · [Methodology](https://allratestoday.com/official-rates-methodology/)
314
+ - [Register (Free)](https://allratestoday.com/register) · [Dashboard](https://allratestoday.com/profile) · [Pricing](https://allratestoday.com/pricing/)
315
+ - [Status](https://allratestoday.com/status)
316
+ - [Postman collection](https://allratestoday.com/postman/)
317
+ - [ChatGPT app](https://chatgpt.com/plugins/plugin_asdk_app_6ae1e76098f48191a285ac3f5ab7a83b) — ask ChatGPT for live and official central bank rates
318
+ - [OpenAPI reference on Bump.sh](https://bump.sh/allratestoday/doc/allratestoday-api) — also on [SwaggerHub](https://app.swaggerhub.com/apis-docs/allratestoday/allratestoday-exchange-rate-central-bank-rates-api/2.2.0)
319
+ - [GitHub](https://github.com/AllRates-Today/sunat-exchange-rate)
320
+
321
+ ## 📜 License
322
+
323
+ MIT
package/index.cjs ADDED
@@ -0,0 +1,47 @@
1
+ 'use strict';
2
+ // SUNAT exchange rates via the AllRatesToday API.
3
+ const BASE = 'https://allratestoday.com/api/v1/central-bank/sunat';
4
+ const KEY_HINT = 'An apiKey is required — get a free key at https://allratestoday.com/register (no credit card required).';
5
+
6
+ async function request(path, params, options) {
7
+ const apiKey = options && options.apiKey;
8
+ if (!apiKey) throw new Error(KEY_HINT);
9
+ const url = new URL(BASE + path);
10
+ for (const [k, v] of Object.entries(params || {})) {
11
+ if (v !== undefined && v !== null) url.searchParams.set(k, v);
12
+ }
13
+ const res = await fetch(url, { headers: { Authorization: 'Bearer ' + apiKey } });
14
+ let body;
15
+ try { body = await res.json(); } catch { body = {}; }
16
+ if (!res.ok) {
17
+ const err = new Error(body.error || ('Request failed: HTTP ' + res.status));
18
+ err.status = res.status;
19
+ err.body = body;
20
+ throw err;
21
+ }
22
+ return body;
23
+ }
24
+
25
+ /** Latest published table (all plans, including free). */
26
+ async function getLatestRates(options) {
27
+ return request('/latest', {}, options);
28
+ }
29
+
30
+ /** Latest rate for one pair, e.g. getRate('USD', 'PEN', { apiKey }). */
31
+ async function getRate(source, target, options) {
32
+ return request('/latest', { source, target }, options);
33
+ }
34
+
35
+ /** Published table for a date (YYYY-MM-DD). Paid plans. Weekends/holidays return the most recent published date. */
36
+ async function getRatesForDate(date, options) {
37
+ if (!/^\d{4}-\d{2}-\d{2}$/.test(String(date))) throw new Error('date must be YYYY-MM-DD');
38
+ return request('/' + date, options && options.source ? { source: options.source, target: options.target } : {}, options);
39
+ }
40
+
41
+ /** Daily time series. Pass { symbol } or { source, target }, optional from/to (YYYY-MM-DD). Paid plans. */
42
+ async function getHistory(query, options) {
43
+ const { symbol, source, target, from, to } = query || {};
44
+ return request('/history', { symbol, source, target, from, to }, options);
45
+ }
46
+
47
+ module.exports = { getLatestRates, getRate, getRatesForDate, getHistory };
package/index.d.ts ADDED
@@ -0,0 +1,52 @@
1
+ export interface RequestOptions {
2
+ /** AllRatesToday API key — free at https://allratestoday.com/register */
3
+ apiKey: string;
4
+ }
5
+
6
+ export interface RateEntry {
7
+ base: string;
8
+ quote: string;
9
+ type: string;
10
+ value: number;
11
+ }
12
+
13
+ export interface LatestRates {
14
+ bank: 'sunat';
15
+ name: string;
16
+ rate_date: string;
17
+ rates: RateEntry[];
18
+ disclaimer: string;
19
+ }
20
+
21
+ export interface PairRate {
22
+ bank: 'sunat';
23
+ name: string;
24
+ rate_date: string;
25
+ source: string;
26
+ target: string;
27
+ rate: number;
28
+ rate_type: string;
29
+ /** true when the pair was computed (inverse/cross) rather than published directly */
30
+ derived: boolean;
31
+ method: 'published' | 'inverse' | 'cross';
32
+ disclaimer: string;
33
+ }
34
+
35
+ export interface DatedRates extends Omit<LatestRates, 'rate_date'> {
36
+ requested_date: string;
37
+ rate_date: string;
38
+ published_on_requested_date: boolean;
39
+ }
40
+
41
+ export interface HistoryQuery {
42
+ symbol?: string;
43
+ source?: string;
44
+ target?: string;
45
+ from?: string;
46
+ to?: string;
47
+ }
48
+
49
+ export declare function getLatestRates(options: RequestOptions): Promise<LatestRates>;
50
+ export declare function getRate(source: string, target: string, options: RequestOptions): Promise<PairRate>;
51
+ export declare function getRatesForDate(date: string, options: RequestOptions & { source?: string; target?: string }): Promise<DatedRates>;
52
+ export declare function getHistory(query: HistoryQuery, options: RequestOptions): Promise<unknown>;
package/index.mjs ADDED
@@ -0,0 +1,44 @@
1
+ // SUNAT exchange rates via the AllRatesToday API.
2
+ const BASE = 'https://allratestoday.com/api/v1/central-bank/sunat';
3
+ const KEY_HINT = 'An apiKey is required — get a free key at https://allratestoday.com/register (no credit card required).';
4
+
5
+ async function request(path, params, options) {
6
+ const apiKey = options && options.apiKey;
7
+ if (!apiKey) throw new Error(KEY_HINT);
8
+ const url = new URL(BASE + path);
9
+ for (const [k, v] of Object.entries(params || {})) {
10
+ if (v !== undefined && v !== null) url.searchParams.set(k, v);
11
+ }
12
+ const res = await fetch(url, { headers: { Authorization: 'Bearer ' + apiKey } });
13
+ let body;
14
+ try { body = await res.json(); } catch { body = {}; }
15
+ if (!res.ok) {
16
+ const err = new Error(body.error || ('Request failed: HTTP ' + res.status));
17
+ err.status = res.status;
18
+ err.body = body;
19
+ throw err;
20
+ }
21
+ return body;
22
+ }
23
+
24
+ /** Latest published table (all plans, including free). */
25
+ export async function getLatestRates(options) {
26
+ return request('/latest', {}, options);
27
+ }
28
+
29
+ /** Latest rate for one pair, e.g. getRate('USD', 'PEN', { apiKey }). */
30
+ export async function getRate(source, target, options) {
31
+ return request('/latest', { source, target }, options);
32
+ }
33
+
34
+ /** Published table for a date (YYYY-MM-DD). Paid plans. Weekends/holidays return the most recent published date. */
35
+ export async function getRatesForDate(date, options) {
36
+ if (!/^\d{4}-\d{2}-\d{2}$/.test(String(date))) throw new Error('date must be YYYY-MM-DD');
37
+ return request('/' + date, options && options.source ? { source: options.source, target: options.target } : {}, options);
38
+ }
39
+
40
+ /** Daily time series. Pass { symbol } or { source, target }, optional from/to (YYYY-MM-DD). Paid plans. */
41
+ export async function getHistory(query, options) {
42
+ const { symbol, source, target, from, to } = query || {};
43
+ return request('/history', { symbol, source, target, from, to }, options);
44
+ }
package/package.json CHANGED
@@ -1,6 +1,71 @@
1
1
  {
2
2
  "name": "@allratestoday/sunat-exchange-rate",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.3.29",
4
+ "description": "Official SUNAT (Peru) daily exchange rates — 1 currency vs PEN, current table, archive from 2026. Zero-dependency Node.js/TypeScript client.",
5
+ "keywords": [
6
+ "sunat",
7
+ "exchange-rate",
8
+ "exchange-rates",
9
+ "exchangerate",
10
+ "exchange rate",
11
+ "exchange rates",
12
+ "pen-exchange-rate",
13
+ "currency",
14
+ "forex",
15
+ "fx",
16
+ "pen",
17
+ "currency-api",
18
+ "currency-exchange-rates",
19
+ "forex-api",
20
+ "currency-conversion",
21
+ "currency-exchange",
22
+ "currency-rates",
23
+ "tax-authority",
24
+ "official-rates",
25
+ "peru",
26
+ "api",
27
+ "typescript",
28
+ "money",
29
+ "convert",
30
+ "conversion",
31
+ "exchange",
32
+ "rate",
33
+ "accounting",
34
+ "utilities",
35
+ "number",
36
+ "cart",
37
+ "ecommerce"
38
+ ],
39
+ "homepage": "https://allratestoday.com/tax-authority-rates-api/sunat/",
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "git+https://github.com/AllRates-Today/sunat-exchange-rate.git"
43
+ },
44
+ "bugs": {
45
+ "url": "https://github.com/AllRates-Today/sunat-exchange-rate/issues"
46
+ },
47
+ "license": "MIT",
48
+ "author": "AllRatesToday <hello@allratestoday.com> (https://allratestoday.com)",
49
+ "type": "module",
50
+ "main": "./index.cjs",
51
+ "module": "./index.mjs",
52
+ "types": "./index.d.ts",
53
+ "exports": {
54
+ ".": {
55
+ "types": "./index.d.ts",
56
+ "import": "./index.mjs",
57
+ "require": "./index.cjs"
58
+ }
59
+ },
60
+ "files": [
61
+ "index.mjs",
62
+ "index.cjs",
63
+ "index.d.ts",
64
+ "README.md",
65
+ "LICENSE"
66
+ ],
67
+ "engines": {
68
+ "node": ">=18"
69
+ },
70
+ "sideEffects": false
71
+ }