exchange-rateapi 1.0.0

Official Rust SDK for Exchange Rate API -- real-time mid-market exchange rates for 160+ currencies
Documentation
  • Coverage
  • 100%
    51 out of 51 items documented9 out of 18 items with examples
  • Size
  • Source code size: 67.75 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.58 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 15s Average build duration of successful builds.
  • all releases: 15s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • cahthuranag

exchange-rateapi

Crates.io Docs.rs License: MIT

Official Rust SDK for Exchange Rate API -- real-time mid-market exchange rates for 160+ currencies.

Installation

Add to your Cargo.toml:

[dependencies]
exchange-rateapi = "1.0"

Or install via cargo:

cargo add exchange-rateapi

Quick Start

use exchange_rateapi::ExchangeRateAPI;

fn main() {
    let client = ExchangeRateAPI::new("era_live_your_api_key");

    // Get latest USD rates
    let resp = client.latest("USD", None).unwrap();
    println!("USD/EUR: {}", resp.rates["EUR"]);

    // Convert 100 USD to GBP
    let result = client.convert("USD", "GBP", 100.0).unwrap();
    println!("100 USD = {} GBP", result.result);
}

API Key

Sign up at exchange-rateapi.com to get your API key. Keys use the format era_live_....

Authentication is handled automatically via Bearer token in the Authorization header.

Methods

latest(base, symbols) -- Get Latest Rates

Fetch the most recent exchange rates for a base currency.

// All currencies
let resp = client.latest("USD", None).unwrap();

// Specific currencies only
let resp = client.latest("USD", Some("EUR,GBP,JPY")).unwrap();

println!("Base: {}", resp.base);
println!("Date: {}", resp.date);
for (code, rate) in &resp.rates {
    println!("{}: {}", code, rate);
}

convert(from, to, amount) -- Convert Currency

Convert an amount between two currencies.

let resp = client.convert("USD", "EUR", 250.0).unwrap();

println!("{} {} = {} {}", resp.amount, resp.from, resp.result, resp.to);
println!("Rate: {}", resp.rate);

for_date(date, base, symbols) -- Historical Rates

Get exchange rates for a specific historical date.

let resp = client.for_date("2025-01-15", "EUR", Some("USD,GBP")).unwrap();

println!("Date: {}", resp.date);
for (code, rate) in &resp.rates {
    println!("{}: {}", code, rate);
}

time_series(start, end, base, symbols) -- Time Series

Fetch rates over a date range.

let resp = client.time_series(
    "2025-01-01",
    "2025-01-31",
    "USD",
    Some("EUR,GBP"),
).unwrap();

for (date, rates) in &resp.rates {
    println!("{}: EUR={}, GBP={}", date, rates["EUR"], rates["GBP"]);
}

symbols() -- List All Currencies

Get all supported currency codes and their names.

let resp = client.symbols().unwrap();

for (code, name) in &resp.symbols {
    println!("{}: {}", code, name);
}

get_rate(from, to) -- Single Pair Rate

Convenience method to get a single exchange rate as an f64.

let rate = client.get_rate("USD", "EUR").unwrap();
println!("1 USD = {} EUR", rate);

get_historical_rates(source, target, period) -- Preset Period Rates

Fetch rates for a preset time period relative to today.

use exchange_rateapi::Period;

// Available periods: OneDay, SevenDays, ThirtyDays, OneYear
let resp = client.get_historical_rates("USD", "EUR", Period::ThirtyDays).unwrap();

for (date, rates) in &resp.rates {
    println!("{}: {}", date, rates["EUR"]);
}

// You can also parse period strings
let period = Period::from_str("7d").unwrap(); // "1d", "7d", "30d", "1y"

Error Handling

All methods return Result<T, ExchangeRateAPIError>. The error enum has three variants:

use exchange_rateapi::{ExchangeRateAPI, ExchangeRateAPIError};

let client = ExchangeRateAPI::new("era_live_your_key");

match client.latest("USD", None) {
    Ok(resp) => println!("Got {} rates", resp.rates.len()),
    Err(ExchangeRateAPIError::HttpError(e)) => {
        eprintln!("Network error: {}", e);
    }
    Err(ExchangeRateAPIError::ApiError { status, message }) => {
        eprintln!("API returned {}: {}", status, message);
    }
    Err(ExchangeRateAPIError::ParseError(msg)) => {
        eprintln!("Could not parse response: {}", msg);
    }
}

Response Types

Method Return Type
latest LatestResponse
convert ConvertResponse
for_date HistoricalResponse
time_series TimeSeriesResponse
symbols SymbolsResponse
get_rate f64
get_historical_rates TimeSeriesResponse

All response structs implement Debug, Clone, Serialize, and Deserialize.

Requirements

Links

License

MIT License. See LICENSE for details.