|
| 1 | +# UniRateAPI.jl |
| 2 | + |
| 3 | +[](https://github.com/UniRate-API/UniRateAPI.jl/actions/workflows/CI.yml) |
| 4 | + |
| 5 | +Official Julia client for the [UniRate API](https://unirateapi.com) — free |
| 6 | +currency exchange rates, historical data, and VAT rates. |
| 7 | + |
| 8 | +- **Zero third-party dependencies.** HTTP goes through the Julia standard |
| 9 | + library (`Downloads`) and JSON is parsed by a small internal parser, so |
| 10 | + installing this package pulls in nothing beyond the standard library. |
| 11 | +- Full parity with the other UniRate clients: current rates, conversion, |
| 12 | + supported currencies, historical rates/timeseries/limits, and VAT rates. |
| 13 | +- A typed error hierarchy and an injectable HTTP transport for offline testing. |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +```julia |
| 18 | +using Pkg |
| 19 | +Pkg.add("UniRateAPI") |
| 20 | +``` |
| 21 | + |
| 22 | +or, in the Pkg REPL (`]`): |
| 23 | + |
| 24 | +``` |
| 25 | +pkg> add UniRateAPI |
| 26 | +``` |
| 27 | + |
| 28 | +## Quick start |
| 29 | + |
| 30 | +Get a free API key at [unirateapi.com](https://unirateapi.com). |
| 31 | + |
| 32 | +```julia |
| 33 | +using UniRateAPI |
| 34 | + |
| 35 | +client = UniRateClient("your-api-key") |
| 36 | + |
| 37 | +# Current rate |
| 38 | +rate = get_rate(client, "USD", "EUR") # 0.92 |
| 39 | + |
| 40 | +# Convert an amount |
| 41 | +eur = convert_amount(client, 100, "USD", "EUR") # 92.5 |
| 42 | + |
| 43 | +# All rates for a base currency |
| 44 | +rates = get_all_rates(client, "USD") # Dict("EUR" => 0.92, ...) |
| 45 | + |
| 46 | +# Supported currencies |
| 47 | +currencies = get_supported_currencies(client) # ["USD", "EUR", ...] |
| 48 | + |
| 49 | +# VAT rates |
| 50 | +vat = get_vat_rate(client, "DE") |
| 51 | +println(vat.vat_data.vat_rate) # 19.0 |
| 52 | +``` |
| 53 | + |
| 54 | +The client reads no environment variables itself — pass the key explicitly. |
| 55 | +The bundled example reads it from `UNIRATE_API_KEY`: |
| 56 | + |
| 57 | +```julia |
| 58 | +UNIRATE_API_KEY=your-key julia --project=. examples/basic.jl |
| 59 | +``` |
| 60 | + |
| 61 | +## API |
| 62 | + |
| 63 | +Construct a client with `UniRateClient(api_key; timeout=30, base_url=..., transport=nothing)`. |
| 64 | + |
| 65 | +| Method | Returns | |
| 66 | +|---|---| |
| 67 | +| `get_rate(client, from, to)` | `Float64` | |
| 68 | +| `get_all_rates(client, from="USD")` | `Dict{String,Float64}` | |
| 69 | +| `convert_amount(client, amount, from, to)` | `Float64` | |
| 70 | +| `get_supported_currencies(client)` | `Vector{String}` | |
| 71 | +| `get_historical_rate(client, date, from, to)` | `Float64` | |
| 72 | +| `get_historical_rates(client, date, base="USD")` | `Dict{String,Float64}` | |
| 73 | +| `convert_historical(client, amount, from, to, date)` | `Float64` | |
| 74 | +| `get_time_series(client, start_date, end_date; amount=1, base="USD", currencies=String[])` | `TimeSeriesData` | |
| 75 | +| `get_historical_limits(client)` | `HistoricalLimits` | |
| 76 | +| `get_vat_rates(client)` | `VatRatesResponse` | |
| 77 | +| `get_vat_rate(client, country)` | `VatCountryResponse` | |
| 78 | + |
| 79 | +Every method also accepts `format` and `callback` keyword arguments, passed |
| 80 | +through as query parameters. Currency and country codes are uppercased |
| 81 | +automatically. |
| 82 | + |
| 83 | +> The conversion method is named `convert_amount` (not `convert`) so it does |
| 84 | +> not shadow `Base.convert`. |
| 85 | +
|
| 86 | +### Historical endpoints require a Pro subscription |
| 87 | + |
| 88 | +`get_historical_rate`, `get_historical_rates`, `convert_historical`, |
| 89 | +`get_time_series`, and `get_historical_limits` are **Pro-gated**. On the free |
| 90 | +tier they raise an `APIError` with `status == 403`. |
| 91 | + |
| 92 | +## Error handling |
| 93 | + |
| 94 | +Every error subtypes `UniRateError`, so you can catch that one type to handle |
| 95 | +all failures, or match a specific type to branch on the HTTP status. |
| 96 | + |
| 97 | +```julia |
| 98 | +using UniRateAPI |
| 99 | + |
| 100 | +try |
| 101 | + rate = get_rate(client, "USD", "EUR") |
| 102 | +catch e |
| 103 | + if e isa AuthenticationError |
| 104 | + # 401 — missing or invalid API key |
| 105 | + elseif e isa RateLimitError |
| 106 | + # 429 — rate limit exceeded |
| 107 | + elseif e isa InvalidCurrencyError |
| 108 | + # 404 — unknown currency or no data |
| 109 | + elseif e isa InvalidDateError |
| 110 | + # 400 — bad parameters |
| 111 | + elseif e isa APIError |
| 112 | + # e.status / e.body — includes 403 (Pro-gated) and 503 |
| 113 | + elseif e isa TransportError |
| 114 | + # network failure or unparseable response |
| 115 | + end |
| 116 | +end |
| 117 | +``` |
| 118 | + |
| 119 | +| HTTP status | Error type | |
| 120 | +|---|---| |
| 121 | +| 400 | `InvalidDateError` | |
| 122 | +| 401 | `AuthenticationError` | |
| 123 | +| 403 | `APIError` (status = 403) | |
| 124 | +| 404 | `InvalidCurrencyError` | |
| 125 | +| 429 | `RateLimitError` | |
| 126 | +| 503 | `APIError` (status = 503) | |
| 127 | +| other | `APIError` | |
| 128 | +| network / parse | `TransportError` | |
| 129 | + |
| 130 | +## Rate limits |
| 131 | + |
| 132 | +The free tier is generous but rate-limited; a `429` raises `RateLimitError`. |
| 133 | +See [unirateapi.com](https://unirateapi.com) for current limits and Pro plans. |
| 134 | + |
| 135 | +## Testing offline |
| 136 | + |
| 137 | +The HTTP transport is injectable. Pass a callable |
| 138 | +`(url, headers) -> (status, body)` as `transport` to run without a network — |
| 139 | +this is exactly how the test suite works: |
| 140 | + |
| 141 | +```julia |
| 142 | +client = UniRateClient("test-key"; |
| 143 | + transport = (url, headers) -> (200, """{"rate": "0.92"}""")) |
| 144 | +get_rate(client, "USD", "EUR") # 0.92, no network call |
| 145 | +``` |
| 146 | + |
| 147 | +Run the tests with `julia --project=. -e 'using Pkg; Pkg.test()'`. Mock tests |
| 148 | +run by default; setting `UNIRATE_API_KEY` additionally runs the free-tier live |
| 149 | +tests. |
| 150 | + |
| 151 | +## Related clients |
| 152 | + |
| 153 | +UniRate ships official clients for many languages and frameworks — Python, |
| 154 | +Node/TypeScript, Go, Rust, Ruby, PHP, Java, Swift, .NET, Dart, and more. See |
| 155 | +[github.com/UniRate-API](https://github.com/UniRate-API). |
| 156 | + |
| 157 | +## License |
| 158 | + |
| 159 | +MIT © 2026 Unirate Team. See [LICENSE](LICENSE). |
0 commit comments