Skip to main content

Crate unirate_axum

Crate unirate_axum 

Source
Expand description

§unirate-axum

Axum integration for the UniRate API — free, real-time currency exchange rates, conversion, and VAT rates.

This crate exposes a ready-made router that mounts four GET routes on top of the official unirate_api::Client:

RouteQuery paramsBacking client call
/ratefrom, toClient::get_rate / Client::get_all_rates
/convertfrom, to, amountClient::convert
/currencies—Client::get_supported_currencies
/vatcountry (optional)Client::get_vat_rate / Client::get_vat_rates

UniRate errors are mapped to the matching HTTP status with a JSON error body via ApiError.

§Client sourcing

The UniRate HTTP client itself is not re-implemented here — this crate depends on the published unirate-api crate (unirate_api::Client) and only adds the Axum wiring. That keeps the client parity in one place and this crate small.

§Quick start

use unirate_axum::router;
use unirate_api::Client;

#[tokio::main]
async fn main() {
    let client = Client::new(std::env::var("UNIRATE_API_KEY").unwrap());
    let app = router(client);

    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

§Composing into a larger app

router returns a plain axum::Router, so you can nest it under a prefix or merge it into an existing router:

use axum::Router;
use unirate_axum::router;
use unirate_api::Client;

let client = Client::new("your-api-key");
let app: Router = Router::new().nest("/currency", router(client));

Structs§

ApiError
Wraps a UniRateError and turns it into an HTTP response.
ConvertParams
Query params for GET /convert.
ConvertResponse
Body of a successful GET /convert.
CurrenciesResponse
Body of a successful GET /currencies.
RateParams
Query params for GET /rate.
VatAll
Body of a successful GET /vat (all countries).
VatCountry
Body of a successful GET /vat?country=XX.
VatParams
Query params for GET /vat.
VatRateBody
VAT rate for a single country, as returned in a JSON response.

Functions§

router
Build an axum::Router wired to the given Client.