Rust

Rust Exchange Rate API

Fetch live and historical exchange rates in Rust. Install our SDK with cargo or call the REST API directly with reqwest.

Installation

Install the official Rust SDK from crates.io using cargo.

cargo add exchange-rateapi

Or add it manually to your Cargo.toml:

[dependencies]
exchange-rateapi = "0.1"
Requirements — Rust 1.70 or later. The SDK depends on reqwest and serde which are pulled in automatically.

Quick Start

Create a client and fetch your first exchange rate in just a few lines.

use exchange_rateapi::ExchangeRateAPI;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = ExchangeRateAPI::new("era_live_YOUR_API_KEY");

    // Get the latest USD to EUR rate
    let rate = client.get_rate("USD", "EUR").await?;
    println!("1 USD = {} EUR", rate.rate);
    // Output: 1 USD = 0.9215 EUR

    Ok(())
}
Get your free API keySign up here to get an API key that starts with era_live_. The free plan includes 300 requests per month.

Authentication

The SDK authenticates every request using a Bearer token derived from your API key. Pass the key when constructing the client.

use exchange_rateapi::ExchangeRateAPI;

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

All subsequent method calls on client automatically include the Authorization: Bearer era_live_... header.

All Methods

The exchange-rateapi crate wraps every API endpoint and handles authentication, retries, and response deserialization for you.

latest(base, symbols) — Get Latest Rates

Retrieve the most recent exchange rates for one or more target currencies.

use exchange_rateapi::ExchangeRateAPI;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = ExchangeRateAPI::new("era_live_YOUR_API_KEY");

    // Latest rates for specific symbols
    let rates = client.latest("USD", &["EUR", "GBP", "JPY"]).await?;
    for r in &rates {
        println!("1 USD = {} {}", r.rate, r.target);
    }
    // Output:
    // 1 USD = 0.9215 EUR
    // 1 USD = 0.7891 GBP
    // 1 USD = 151.42 JPY

    Ok(())
}

convert(from, to, amount) — Convert Currencies

Convert a specific amount from one currency to another.

// Convert 1000 USD to EUR
let result = client.convert("USD", "EUR", 1000.0).await?;
println!("$1,000 = {} EUR", result.result);

for_date(date, base, symbols) — Historical Rates

Fetch exchange rates for a specific historical date.

// Get rates for a specific date
let rates = client.for_date("2026-01-15", "USD", &["EUR", "GBP"]).await?;
for r in &rates {
    println!("{}: 1 USD = {} {}", r.time, r.rate, r.target);
}

time_series(start, end, base, symbols) — Date Range

Retrieve rates across a date range.

// Get daily rates over a date range
let series = client.time_series(
    "2026-01-01",
    "2026-01-31",
    "USD",
    &["EUR"]
).await?;

for point in &series {
    println!("{}: {}", point.date, point.rate);
}

symbols() — List Currencies

Get all supported currency codes and names.

// Get all supported currency codes
let symbols = client.symbols().await?;
for currency in &symbols.currencies {
    println!("{}: {}", currency.code, currency.name);
}

get_rate(from, to) — Single Pair

Get the current rate for a single currency pair.

// Single currency pair
let rate = client.get_rate("USD", "EUR").await?;
println!("1 USD = {} EUR", rate.rate);

get_historical_rates(source, target, period) — Preset Periods

Fetch historical rate data using preset period strings such as "7d", "30d", "90d", or "1y".

// Get 30-day historical data
let history = client.get_historical_rates("USD", "EUR", "30d").await?;
for point in &history.data {
    println!("{}: {}", point.date, point.rate);
}

Error Handling

The SDK returns errors via the ExchangeRateAPIError enum, which covers authentication failures, rate limiting, network issues, and invalid responses.

use exchange_rateapi::{ExchangeRateAPI, ExchangeRateAPIError};

#[tokio::main]
async fn main() {
    let client = ExchangeRateAPI::new("era_live_YOUR_API_KEY");

    match client.get_rate("USD", "EUR").await {
        Ok(rate) => {
            println!("1 USD = {} EUR", rate.rate);
        }
        Err(ExchangeRateAPIError::Unauthorized) => {
            eprintln!("Invalid API key. Check your API key.");
        }
        Err(ExchangeRateAPIError::RateLimited) => {
            eprintln!("Rate limit exceeded. Try again later.");
        }
        Err(ExchangeRateAPIError::NotFound) => {
            eprintln!("Currency pair not found.");
        }
        Err(ExchangeRateAPIError::Network(e)) => {
            eprintln!("Network error: {}", e);
        }
        Err(e) => {
            eprintln!("Unexpected error: {}", e);
        }
    }
}

Configuration

Customize the client with a custom base URL or request timeout.

use exchange_rateapi::ExchangeRateAPI;
use std::time::Duration;

let client = ExchangeRateAPI::builder("era_live_YOUR_API_KEY")
    .base_url("https://exchange-rateapi.com")
    .timeout(Duration::from_secs(30))
    .build();

Response Format

Single Target Response

[
  {
    "rate": 0.9215,
    "source": "USD",
    "target": "EUR",
    "time": "2026-05-28T12:00:00Z"
  }
]

Multiple Targets Response

[
  { "rate": 0.9215, "source": "USD", "target": "EUR", "time": "2026-05-28T12:00:00Z" },
  { "rate": 0.7891, "source": "USD", "target": "GBP", "time": "2026-05-28T12:00:00Z" },
  { "rate": 151.42, "source": "USD", "target": "JPY", "time": "2026-05-28T12:00:00Z" }
]
160+ currencies supported — including USD, EUR, GBP, JPY, AUD, CAD, CHF, CNY, INR, BRL, and many more. See the full list via the /api/v1/symbols endpoint.

Links

Explore the Rust SDK resources:

Start building with Rust

Get your free API key and start fetching exchange rates in Rust in under a minute.

Get Free API Key →