|
| 1 | +# djangorestframework-unirate |
| 2 | + |
| 3 | +Django REST Framework integration for the [UniRate](https://unirateapi.com) |
| 4 | +currency-exchange API. Drop-in API views for live exchange rates, conversion, |
| 5 | +and supported-currency lookups — plus serializer fields and a cached, |
| 6 | +settings-driven client — all keeping your UniRate API key safely server-side. |
| 7 | + |
| 8 | +- **Drop-in endpoints** — mount three ready-made views (`rates`, `convert`, |
| 9 | + `currencies`) under any URL prefix; clients call *your* API, never UniRate |
| 10 | + directly, so the key never reaches the browser. |
| 11 | +- **Serializer fields** — `CurrencyCodeField` (normalises/validates ISO-4217 |
| 12 | + codes) and `ConvertedAmountField` (live-converts a model's amount into a |
| 13 | + target currency). |
| 14 | +- **Thin & aligned** — wraps the official [`unirate-api`](https://pypi.org/project/unirate-api/) |
| 15 | + Python client, so behaviour and error semantics match every other UniRate |
| 16 | + library. Optional Django-cache integration. Zero extra runtime deps. |
| 17 | + |
| 18 | +Companion to [`UniRateBackend` in django-money](https://github.com/django-money/django-money): |
| 19 | +django-money gives you a UniRate *exchange backend*; this package gives you a |
| 20 | +UniRate-powered *REST surface* on top of DRF. |
| 21 | + |
| 22 | +## Install |
| 23 | + |
| 24 | +```bash |
| 25 | +pip install djangorestframework-unirate |
| 26 | +``` |
| 27 | + |
| 28 | +Add it to `INSTALLED_APPS` and configure your key: |
| 29 | + |
| 30 | +```python |
| 31 | +INSTALLED_APPS = [ |
| 32 | + # ... |
| 33 | + "rest_framework", |
| 34 | + "rest_framework_unirate", |
| 35 | +] |
| 36 | + |
| 37 | +UNIRATE_API_KEY = "your-api-key" # or set the UNIRATE_API_KEY env var |
| 38 | +``` |
| 39 | + |
| 40 | +Get a free API key at [unirateapi.com](https://unirateapi.com). |
| 41 | + |
| 42 | +## Quick start — drop-in endpoints |
| 43 | + |
| 44 | +```python |
| 45 | +# urls.py |
| 46 | +from django.urls import include, path |
| 47 | + |
| 48 | +urlpatterns = [ |
| 49 | + path("api/fx/", include("rest_framework_unirate.urls")), |
| 50 | +] |
| 51 | +``` |
| 52 | + |
| 53 | +That exposes: |
| 54 | + |
| 55 | +| Endpoint | Example | Response | |
| 56 | +|---|---|---| |
| 57 | +| `GET api/fx/rates/?from=USD&to=EUR` | single pair | `{"from_currency": "USD", "to_currency": "EUR", "rate": 0.92}` | |
| 58 | +| `GET api/fx/rates/?from=USD` | all pairs | `{"base": "USD", "rates": {"EUR": 0.92, "GBP": 0.79, ...}}` | |
| 59 | +| `GET api/fx/convert/?from=USD&to=EUR&amount=100` | convert | `{"from_currency": "USD", "to_currency": "EUR", "amount": 100.0, "result": 92.0}` | |
| 60 | +| `GET api/fx/currencies/` | supported list | `{"currencies": ["USD", "EUR", "GBP", ...]}` | |
| 61 | + |
| 62 | +`from` defaults to `UNIRATE_DEFAULT_BASE_CURRENCY` (USD); `amount` defaults to |
| 63 | +`1`. Currency codes are case-insensitive and normalised to upper-case. |
| 64 | + |
| 65 | +## Serializer fields |
| 66 | + |
| 67 | +### `ConvertedAmountField` |
| 68 | + |
| 69 | +Adds a live-converted amount to any serializer, derived from a sibling |
| 70 | +currency field: |
| 71 | + |
| 72 | +```python |
| 73 | +from rest_framework import serializers |
| 74 | +from rest_framework_unirate.fields import ConvertedAmountField |
| 75 | + |
| 76 | +class ProductSerializer(serializers.Serializer): |
| 77 | + name = serializers.CharField() |
| 78 | + price = serializers.FloatField() |
| 79 | + currency = serializers.CharField() |
| 80 | + price_eur = ConvertedAmountField( |
| 81 | + amount_field="price", |
| 82 | + from_currency_field="currency", # or from_currency="USD" |
| 83 | + to_currency="EUR", # or omit + pass context={"target_currency": ...} |
| 84 | + ) |
| 85 | +``` |
| 86 | + |
| 87 | +### `CurrencyCodeField` |
| 88 | + |
| 89 | +A `CharField` that upper-cases input and enforces a 3-letter alphabetic code. |
| 90 | +Pass `validate_supported=True` to additionally check it against the live |
| 91 | +`/api/currencies` list (one cached call): |
| 92 | + |
| 93 | +```python |
| 94 | +from rest_framework_unirate.fields import CurrencyCodeField |
| 95 | + |
| 96 | +class QuoteSerializer(serializers.Serializer): |
| 97 | + base = CurrencyCodeField() |
| 98 | + quote = CurrencyCodeField(validate_supported=True) |
| 99 | +``` |
| 100 | + |
| 101 | +## Using the client directly |
| 102 | + |
| 103 | +```python |
| 104 | +from rest_framework_unirate.client import get_accessor |
| 105 | + |
| 106 | +accessor = get_accessor() |
| 107 | +accessor.get_rate("USD", "EUR") # 0.92 |
| 108 | +accessor.get_rates("USD") # {"EUR": 0.92, "GBP": 0.79, ...} |
| 109 | +accessor.convert("USD", "EUR", 100) # 92.0 |
| 110 | +accessor.get_supported_currencies() # ["USD", "EUR", ...] |
| 111 | +``` |
| 112 | + |
| 113 | +## Configuration |
| 114 | + |
| 115 | +| Setting | Default | Purpose | |
| 116 | +|---|---|---| |
| 117 | +| `UNIRATE_API_KEY` | — (required) | Your UniRate API key | |
| 118 | +| `UNIRATE_TIMEOUT` | `30` | HTTP timeout in seconds | |
| 119 | +| `UNIRATE_BASE_URL` | `https://api.unirateapi.com` | Override the API base URL | |
| 120 | +| `UNIRATE_CACHE_TIMEOUT` | `None` | Seconds to cache rates/currencies in Django's cache (off when unset) | |
| 121 | +| `UNIRATE_CACHE_ALIAS` | `default` | Which `CACHES` alias to use | |
| 122 | +| `UNIRATE_DEFAULT_BASE_CURRENCY` | `USD` | Base currency when a request omits `from` | |
| 123 | + |
| 124 | +## Error handling |
| 125 | + |
| 126 | +The bundled views map `unirate` client errors onto sensible HTTP responses: |
| 127 | + |
| 128 | +| Situation | Status | |
| 129 | +|---|---| |
| 130 | +| Unknown currency (upstream 404) | `404 Not Found` | |
| 131 | +| Bad parameters (upstream 400) | `400 Bad Request` | |
| 132 | +| Upstream rate limit (429) | `429 Too Many Requests` | |
| 133 | +| Bad server-side API key / Pro-gated 403 | `502 Bad Gateway` | |
| 134 | +| UniRate unavailable (503) / network failure | `503` / `502` | |
| 135 | + |
| 136 | +Because the API key is server-side, an upstream `401` is treated as a gateway |
| 137 | +misconfiguration (`502`), not as a client auth failure. To apply the same |
| 138 | +mapping to your *own* DRF views, set the handler globally: |
| 139 | + |
| 140 | +```python |
| 141 | +REST_FRAMEWORK = { |
| 142 | + "EXCEPTION_HANDLER": "rest_framework_unirate.exceptions.unirate_exception_handler", |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +## Rate limits |
| 147 | + |
| 148 | +Latest rates, conversion, and the currency list are free-tier endpoints. |
| 149 | +Historical rates and time-series are Pro-gated and return `403` on the free |
| 150 | +tier. Set `UNIRATE_CACHE_TIMEOUT` to cut your upstream call volume. |
| 151 | + |
| 152 | +## Compatibility |
| 153 | + |
| 154 | +- Python 3.10–3.13 |
| 155 | +- Django 4.2, 5.0, 5.1, 5.2 |
| 156 | +- Django REST Framework 3.15–3.16 |
| 157 | + |
| 158 | +## Related UniRate clients |
| 159 | + |
| 160 | +Official UniRate libraries: Python, Node/TypeScript, Go, Rust, Ruby, PHP, |
| 161 | +Java, Swift, .NET — plus framework integrations for FastAPI, Flask, Wagtail, |
| 162 | +LangChain, dbt, Airflow, and more. See the |
| 163 | +[UniRate-API org](https://github.com/UniRate-API). |
| 164 | + |
| 165 | +## License |
| 166 | + |
| 167 | +MIT — see [LICENSE](LICENSE). |
0 commit comments