|
| 1 | +# dlt-unirate |
| 2 | + |
| 3 | +[](https://pypi.org/project/dlt-unirate/) |
| 4 | +[](https://opensource.org/licenses/MIT) |
| 5 | + |
| 6 | +A [dlt](https://dlthub.com) source for the [UniRate API](https://unirateapi.com) |
| 7 | +— load live currency exchange rates, the supported-currency list, and |
| 8 | +per-country VAT rates into any dlt destination (DuckDB, BigQuery, Snowflake, |
| 9 | +Postgres, filesystem, …). |
| 10 | + |
| 11 | +UniRate provides 593+ fiat, crypto, and commodity exchange rates. Latest rates, |
| 12 | +currencies, and VAT rates are available on the free tier; historical rates |
| 13 | +require a Pro plan. |
| 14 | + |
| 15 | +## Why this package |
| 16 | + |
| 17 | +dlt has no built-in FX source, so people hand-roll a `requests` loop or a |
| 18 | +`rest_api` config every time they need exchange rates in a warehouse. This |
| 19 | +package gives you a typed, tested `@dlt.source` with three resources you can |
| 20 | +drop straight into a pipeline. |
| 21 | + |
| 22 | +## Install |
| 23 | + |
| 24 | +```bash |
| 25 | +pip install dlt-unirate |
| 26 | +``` |
| 27 | + |
| 28 | +This pulls in `dlt` and `requests`. Add a destination extra for your target, |
| 29 | +e.g. `pip install "dlt[duckdb]"`. |
| 30 | + |
| 31 | +## Quick start |
| 32 | + |
| 33 | +```python |
| 34 | +import dlt |
| 35 | +from dlt_unirate import unirate_source |
| 36 | + |
| 37 | +pipeline = dlt.pipeline( |
| 38 | + pipeline_name="unirate", |
| 39 | + destination="duckdb", |
| 40 | + dataset_name="unirate_data", |
| 41 | +) |
| 42 | + |
| 43 | +# api_key resolves from dlt secrets / the UNIRATE_API_KEY env var, |
| 44 | +# or pass it explicitly: unirate_source(api_key="..."). |
| 45 | +load_info = pipeline.run(unirate_source(base_currency="USD")) |
| 46 | +print(load_info) |
| 47 | +``` |
| 48 | + |
| 49 | +That loads three tables: `exchange_rates`, `currencies`, and `vat_rates`. |
| 50 | + |
| 51 | +## Configuring the API key |
| 52 | + |
| 53 | +`unirate_source` follows dlt's config/secrets convention. Any of these work: |
| 54 | + |
| 55 | +- **Env var:** `export UNIRATE_API_KEY="..."` |
| 56 | +- **dlt secrets** — `.dlt/secrets.toml`: |
| 57 | + |
| 58 | + ```toml |
| 59 | + [sources.unirate] |
| 60 | + api_key = "..." |
| 61 | + ``` |
| 62 | +- **Explicit argument:** `unirate_source(api_key="...")` |
| 63 | + |
| 64 | +Get a free key at [unirateapi.com](https://unirateapi.com). |
| 65 | + |
| 66 | +## Resources |
| 67 | + |
| 68 | +| Resource | Table columns | Notes | |
| 69 | +|---|---|---| |
| 70 | +| `exchange_rates` | `base_currency`, `target_currency`, `rate` | Every current rate for `base_currency`. `write_disposition="replace"`. | |
| 71 | +| `currencies` | `currency_code` | All supported currency codes. `replace`. | |
| 72 | +| `vat_rates` | `country_code`, `country_name`, `vat_rate` | Per-country VAT rates. `replace`. | |
| 73 | +| `historical_exchange_rates` | `date`, `base_currency`, `target_currency`, `rate` | **Pro-gated** — off by default. `merge` on `(date, base, target)`. | |
| 74 | + |
| 75 | +## Source parameters |
| 76 | + |
| 77 | +| Parameter | Default | Description | |
| 78 | +|---|---|---| |
| 79 | +| `api_key` | dlt secret / `UNIRATE_API_KEY` | UniRate API key. | |
| 80 | +| `base_currency` | `"USD"` | Base for `exchange_rates` (and historical). | |
| 81 | +| `base_url` | `https://api.unirateapi.com` | API base URL. | |
| 82 | +| `request_timeout` | `30` | Per-request timeout, seconds. | |
| 83 | +| `include_historical` | `False` | Also yield the Pro-gated historical resource. | |
| 84 | +| `historical_dates` | `None` | `YYYY-MM-DD` dates to pull historical rates for. | |
| 85 | + |
| 86 | +### Historical rates (Pro) |
| 87 | + |
| 88 | +```python |
| 89 | +source = unirate_source( |
| 90 | + base_currency="EUR", |
| 91 | + include_historical=True, |
| 92 | + historical_dates=["2024-01-01", "2024-02-01"], |
| 93 | +) |
| 94 | +pipeline.run(source) |
| 95 | +``` |
| 96 | + |
| 97 | +Historical endpoints return HTTP 403 on the free tier (surfaced as |
| 98 | +`ProSubscriptionError`); a UniRate Pro plan is required. |
| 99 | + |
| 100 | +## Selecting a subset of resources |
| 101 | + |
| 102 | +```python |
| 103 | +# Only load exchange rates and currencies. |
| 104 | +pipeline.run(unirate_source().with_resources("exchange_rates", "currencies")) |
| 105 | +``` |
| 106 | + |
| 107 | +## Error handling |
| 108 | + |
| 109 | +Errors raised by the underlying client all inherit from `UniRateError`: |
| 110 | + |
| 111 | +| HTTP | Exception | |
| 112 | +|---|---| |
| 113 | +| 401 | `AuthenticationError` | |
| 114 | +| 403 | `ProSubscriptionError` | |
| 115 | +| 404 | `InvalidCurrencyError` | |
| 116 | +| 429 | `RateLimitError` | |
| 117 | +| other non-2xx | `APIError` (carries `status_code`) | |
| 118 | +| network / transport | `UniRateError` | |
| 119 | + |
| 120 | +## Related UniRate clients |
| 121 | + |
| 122 | +If you want to call the API directly, there are official clients in |
| 123 | +[Python](https://github.com/UniRate-API/unirate-api-python), |
| 124 | +[Node.js](https://github.com/UniRate-API/unirate-api-nodejs), |
| 125 | +[Go](https://github.com/UniRate-API/unirate-api-go), |
| 126 | +[Rust](https://github.com/UniRate-API/unirate-api-rust), |
| 127 | +[Java](https://github.com/UniRate-API/unirate-api-java), |
| 128 | +[Ruby](https://github.com/UniRate-API/unirate-api-ruby), |
| 129 | +[PHP](https://github.com/UniRate-API/unirate-api-php), |
| 130 | +[.NET](https://github.com/UniRate-API/unirate-api-dotnet), and |
| 131 | +[Swift](https://github.com/UniRate-API/unirate-api-swift), plus data-stack |
| 132 | +integrations for [dbt](https://github.com/UniRate-API/dbt-unirate), |
| 133 | +[Airflow](https://github.com/UniRate-API/airflow-provider-unirate), and |
| 134 | +[LangChain](https://github.com/UniRate-API/langchain-unirate). |
| 135 | + |
| 136 | +## License |
| 137 | + |
| 138 | +MIT — see [LICENSE](LICENSE). |
0 commit comments