Official Rust SDK for Exchange Rate API -- real-time mid-market exchange rates for 160+ currencies.
Add to your Cargo.toml:
[dependencies]
exchange-rateapi = "1.0"Or install via cargo:
cargo add exchange-rateapiuse 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);
}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.
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 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);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);
}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"]);
}Get all supported currency codes and their names.
let resp = client.symbols().unwrap();
for (code, name) in &resp.symbols {
println!("{}: {}", code, name);
}Convenience method to get a single exchange rate as an f64.
let rate = client.get_rate("USD", "EUR").unwrap();
println!("1 USD = {} EUR", rate);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"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);
}
}| 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.
- Rust 2021 edition or later
- An API key from exchange-rateapi.com
MIT License. See LICENSE for details.