@openexchangeapi/sdk
Version:
Minimal JavaScript client for OpenExchangeAPI using native fetch.
104 lines (74 loc) • 2.3 kB
Markdown
# OpenExchangeAPI JavaScript SDK
A lightweight, modern JavaScript/TypeScript client for [OpenExchangeAPI](https://openexchangeapi.com) — built for browsers and Node.js.
- ✅ Full API support
- 🔑 Optional API key
- 🌐 Works in browsers and Node.js
- 🧠 TypeScript types included
## 📦 Installation
```bash
npm install @openexchangeapi/sdk
````
Or load directly in the browser via CDN:
```html
<script type="module">
import { OpenExchangeApiClient } from 'https://unpkg.com/@openexchangeapi/sdk@latest/src/oex.js';
</script>
```
Also available via jsDelivr:
```html
<script type="module">
import { OpenExchangeApiClient } from 'https://cdn.jsdelivr.net/npm/@openexchangeapi/sdk@latest/src/oex.js';
</script>
```
## 🚀 Usage
```js
import { OpenExchangeApiClient } from '@openexchangeapi/sdk';
const client = new OpenExchangeApiClient(); // optional
const latest = await client.getLatest();
const converted = await client.convert({
from: 'USD',
to: 'EUR',
amount: 100,
});
const currencies = await client.listCurrencies();
const eur = await client.getCurrency({ code: 'EUR' });
console.log(latest);
console.log(converted);
console.log(currencies);
console.log(eur);
```
## 🧩 API Reference
```ts
client.getLatest(base?: string)
client.getLatestPrecise(base?: string)
client.getHistorical(date: string, base?: string)
client.getHistoricalPrecise(date: string, base?: string)
client.convert(from: string, to: string, amount: number)
client.convertPrecise(from: string, to: string, amount: number)
client.listCurrencies(type?: 'fiat' | 'crypto' | 'virtual')
client.getCurrency(code: string)
```
All methods return `Promise<T>`. Errors throw `OpenExchangeApiError`.
## ❗ Error Handling
```js
try {
const result = await client.getLatest();
} catch (err) {
console.error('OpenExchangeAPI error:', err.message);
}
```
## 🔗 CDN Access
* **unpkg:**
`https://unpkg.com/@openexchangeapi/sdk@latest/src/oex.js`
* **jsDelivr:**
`https://cdn.jsdelivr.net/npm/@openexchangeapi/sdk@latest/src/oex.js`
* **GitHub CDN (via jsDelivr):**
`https://cdn.jsdelivr.net/gh/OpenExchangeAPI/sdk-javascript@vlatest/src/oex.js`
## 📄 License
See: [https://openexchangeapi.com/api-license](https://openexchangeapi.com/api-license)