|
| 1 | +# streamlit-unirate |
| 2 | + |
| 3 | +Streamlit integration for the [UniRate API](https://unirateapi.com) — live exchange rates, currency conversion, and VAT rates with automatic `st.cache_data` / `st.cache_resource` caching built in. |
| 4 | + |
| 5 | +[](https://pypi.org/project/streamlit-unirate/) |
| 6 | +[](https://pypi.org/project/streamlit-unirate/) |
| 7 | +[](LICENSE) |
| 8 | +[](https://github.com/UniRate-API/streamlit-unirate/actions) |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +```bash |
| 13 | +pip install streamlit-unirate |
| 14 | +``` |
| 15 | + |
| 16 | +## Quick start |
| 17 | + |
| 18 | +```python |
| 19 | +import streamlit as st |
| 20 | +import streamlit_unirate as ur |
| 21 | + |
| 22 | +# List currencies (cached 1 h) |
| 23 | +currencies = ur.list_currencies() |
| 24 | + |
| 25 | +from_cur = st.selectbox("From", currencies) |
| 26 | +to_cur = st.selectbox("To", currencies) |
| 27 | +amount = st.number_input("Amount", value=100.0) |
| 28 | + |
| 29 | +if st.button("Convert"): |
| 30 | + result = ur.convert(to_cur, amount, from_cur) |
| 31 | + st.metric(f"{amount} {from_cur} →", f"{result:.4f} {to_cur}") |
| 32 | +``` |
| 33 | + |
| 34 | +## API key |
| 35 | + |
| 36 | +Set `UNIRATE_API_KEY` in `.streamlit/secrets.toml` (preferred) or as an environment variable: |
| 37 | + |
| 38 | +```toml |
| 39 | +# .streamlit/secrets.toml |
| 40 | +UNIRATE_API_KEY = "your_api_key_here" |
| 41 | +``` |
| 42 | + |
| 43 | +```bash |
| 44 | +UNIRATE_API_KEY=your_key streamlit run app.py |
| 45 | +``` |
| 46 | + |
| 47 | +Get a free key at [unirateapi.com](https://unirateapi.com). |
| 48 | + |
| 49 | +## API reference |
| 50 | + |
| 51 | +All functions use Streamlit's caching automatically — no extra decorators needed. |
| 52 | + |
| 53 | +### `get_client(api_key=None, base_url=None)` |
| 54 | + |
| 55 | +Returns the underlying `unirate.UnirateClient` singleton (cached via `@st.cache_resource`). Use this when you need direct access to Pro endpoints such as historical rates or time series. |
| 56 | + |
| 57 | +```python |
| 58 | +client = ur.get_client() |
| 59 | +data = client.get_time_series("2025-01-01", "2025-01-31", base_currency="USD") |
| 60 | +``` |
| 61 | + |
| 62 | +### `get_rate(from_currency="USD", to_currency=None)` → `float | dict` |
| 63 | + |
| 64 | +Current exchange rate. Cached for **5 minutes**. |
| 65 | + |
| 66 | +```python |
| 67 | +rate = ur.get_rate("USD", "EUR") # → 0.9214 (float) |
| 68 | +all_rates = ur.get_rate("USD") # → {"EUR": 0.92, "GBP": 0.79, ...} |
| 69 | +``` |
| 70 | + |
| 71 | +### `convert(to_currency, amount=1.0, from_currency="USD")` → `float` |
| 72 | + |
| 73 | +Convert an amount. Cached for **5 minutes**. |
| 74 | + |
| 75 | +```python |
| 76 | +eur = ur.convert("EUR", 250.0, "USD") # → 230.35 |
| 77 | +``` |
| 78 | + |
| 79 | +### `list_currencies()` → `list[str]` |
| 80 | + |
| 81 | +All supported currency codes. Cached for **1 hour**. |
| 82 | + |
| 83 | +```python |
| 84 | +codes = ur.list_currencies() # → ["AED", "AFN", ..., "ZWL"] |
| 85 | +``` |
| 86 | + |
| 87 | +### `get_vat_rates(country=None)` → `dict` |
| 88 | + |
| 89 | +VAT rates for all countries or a single ISO-3166 country code. Cached for **24 hours**. |
| 90 | + |
| 91 | +```python |
| 92 | +all_vat = ur.get_vat_rates() |
| 93 | +de_vat = ur.get_vat_rates("DE") |
| 94 | +``` |
| 95 | + |
| 96 | +## Error handling |
| 97 | + |
| 98 | +All functions raise exceptions from `unirate.exceptions`: |
| 99 | + |
| 100 | +| Exception | Cause | |
| 101 | +|---|---| |
| 102 | +| `AuthenticationError` | Missing or invalid API key | |
| 103 | +| `RateLimitError` | Free-tier rate limit exceeded | |
| 104 | +| `InvalidCurrencyError` | Unknown currency code | |
| 105 | +| `APIError` | Other API error (check `.status_code`) | |
| 106 | + |
| 107 | +```python |
| 108 | +from unirate.exceptions import RateLimitError, AuthenticationError |
| 109 | +import streamlit_unirate as ur |
| 110 | + |
| 111 | +try: |
| 112 | + rate = ur.get_rate("USD", "EUR") |
| 113 | +except AuthenticationError: |
| 114 | + st.error("Check your UNIRATE_API_KEY.") |
| 115 | +except RateLimitError: |
| 116 | + st.warning("Rate limit hit — try again in a moment.") |
| 117 | +``` |
| 118 | + |
| 119 | +## Related UniRate clients |
| 120 | + |
| 121 | +<!-- unirate-ecosystem-footer:start --> |
| 122 | +**Other UniRate integrations:** |
| 123 | +[Python](https://github.com/UniRate-API/unirate-api-python) · [Node.js](https://github.com/UniRate-API/unirate-api-nodejs) · [Go](https://github.com/UniRate-API/unirate-api-go) · [Rust](https://github.com/UniRate-API/unirate-api-rust) · [Ruby](https://github.com/UniRate-API/unirate-api-ruby) · [PHP](https://github.com/UniRate-API/unirate-api-php) · [Java](https://github.com/UniRate-API/unirate-api-java) · [Swift](https://github.com/UniRate-API/unirate-api-swift) · [.NET](https://github.com/UniRate-API/unirate-api-dotnet) · [FastAPI](https://github.com/UniRate-API/fastapi-unirate) · [Flask](https://github.com/UniRate-API/flask-unirate) · [Django REST](https://github.com/UniRate-API/djangorestframework-unirate) · [Wagtail](https://github.com/UniRate-API/wagtail-unirate) · [Next.js](https://github.com/UniRate-API/next-unirate) · [Nuxt](https://github.com/UniRate-API/nuxt-unirate) · [SvelteKit](https://github.com/UniRate-API/sveltekit-unirate) · [React](https://github.com/UniRate-API/react-unirate) · [Vue](https://github.com/UniRate-API/vue-unirate) · [Angular](https://github.com/UniRate-API/angular-unirate) · [Remix](https://github.com/UniRate-API/remix-unirate) · [NestJS](https://github.com/UniRate-API/nestjs-unirate) · [LangChain (Python)](https://github.com/UniRate-API/langchain-unirate) · [LangChain.js](https://github.com/UniRate-API/langchain-js-unirate) · [Astro](https://github.com/UniRate-API/astro-unirate) · [Eleventy](https://github.com/UniRate-API/eleventy-unirate) · [Hugo](https://github.com/UniRate-API/hugo-unirate) · [Jekyll](https://github.com/UniRate-API/jekyll-unirate) · [Strapi](https://github.com/UniRate-API/strapi-plugin-unirate) · [Directus](https://github.com/UniRate-API/directus-extension-unirate) · [Medusa](https://github.com/UniRate-API/medusa-plugin-unirate) · [MCP Server](https://github.com/UniRate-API/unirate-mcp) · [CLI](https://github.com/UniRate-API/unirate-cli) |
| 124 | +<!-- unirate-ecosystem-footer:end --> |
| 125 | + |
| 126 | +## License |
| 127 | + |
| 128 | +MIT — see [LICENSE](LICENSE). |
0 commit comments