exchange-rateapi
Official Rust SDK for Exchange Rate API -- real-time mid-market exchange rates for 160+ currencies.
Installation
Add to your Cargo.toml:
[]
= "1.0"
Or install via cargo:
Quick Start
use ExchangeRateAPI;
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.unwrap;
// Specific currencies only
let resp = client.latest.unwrap;
println!;
println!;
for in &resp.rates
convert(from, to, amount) -- Convert Currency
Convert an amount between two currencies.
let resp = client.convert.unwrap;
println!;
println!;
for_date(date, base, symbols) -- Historical Rates
Get exchange rates for a specific historical date.
let resp = client.for_date.unwrap;
println!;
for in &resp.rates
time_series(start, end, base, symbols) -- Time Series
Fetch rates over a date range.
let resp = client.time_series.unwrap;
for in &resp.rates
symbols() -- List All Currencies
Get all supported currency codes and their names.
let resp = client.symbols.unwrap;
for in &resp.symbols
get_rate(from, to) -- Single Pair Rate
Convenience method to get a single exchange rate as an f64.
let rate = client.get_rate.unwrap;
println!;
get_historical_rates(source, target, period) -- Preset Period Rates
Fetch rates for a preset time period relative to today.
use Period;
// Available periods: OneDay, SevenDays, ThirtyDays, OneYear
let resp = client.get_historical_rates.unwrap;
for in &resp.rates
// You can also parse period strings
let period = from_str.unwrap; // "1d", "7d", "30d", "1y"
Error Handling
All methods return Result<T, ExchangeRateAPIError>. The error enum has three variants:
use ;
let client = new;
match client.latest
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
- Rust 2021 edition or later
- An API key from exchange-rateapi.com
Links
License
MIT License. See LICENSE for details.