C# / .NET

C# Exchange Rate API

Fetch live and historical exchange rates in C# and .NET. Use HttpClient with System.Text.Json to build a currency converter in minutes.

Quick Start

Fetch your first exchange rate using .NET's built-in HttpClient and System.Text.Json. No additional NuGet packages required.

using var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", "Bearer era_live_YOUR_API_KEY");

var response = await client.GetStringAsync(
    "https://exchange-rateapi.com/api/v1/rates?source=USD&target=EUR");
Console.WriteLine(response);
// Output: [{"rate":0.9215,"source":"USD","target":"EUR","time":"2026-05-15T12:00:00Z"}]
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.

Complete C# Example with System.Text.Json

Here is a full example that fetches exchange rates and deserializes the JSON response into strongly-typed C# objects.

using System.Net.Http.Json;
using System.Text.Json.Serialization;

// Define the rate model
public record ExchangeRate(
    [property: JsonPropertyName("rate")] double Rate,
    [property: JsonPropertyName("source")] string Source,
    [property: JsonPropertyName("target")] string Target,
    [property: JsonPropertyName("time")] string Time
);

// Create a reusable service
public class ExchangeRateService
{
    private readonly HttpClient _client;

    public ExchangeRateService(string apiKey)
    {
        _client = new HttpClient();
        _client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
    }

    public async Task<List<ExchangeRate>> GetRatesAsync(string source, string target)
    {
        var response = await _client.GetAsync(
            $"https://exchange-rateapi.com/api/v1/rates?source={source}&target={target}");

        response.EnsureSuccessStatusCode();

        return await response.Content
            .ReadFromJsonAsync<List<ExchangeRate>>()
            ?? new List<ExchangeRate>();
    }
}

// Usage
var service = new ExchangeRateService("era_live_YOUR_API_KEY");

// Single pair
var rates = await service.GetRatesAsync("USD", "EUR");
Console.WriteLine($"1 USD = {rates[0].Rate} EUR");

// Multiple targets
var multiRates = await service.GetRatesAsync("USD", "EUR,GBP,JPY");
foreach (var rate in multiRates)
{
    Console.WriteLine($"1 USD = {rate.Rate} {rate.Target}");
}

Error Handling

public async Task<List<ExchangeRate>?> GetRatesSafeAsync(string source, string target)
{
    try
    {
        var response = await _client.GetAsync(
            $"https://exchange-rateapi.com/api/v1/rates?source={source}&target={target}");

        if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
        {
            Console.WriteLine("Invalid API key. Check your Authorization header.");
            return null;
        }

        if (response.StatusCode == (System.Net.HttpStatusCode)429)
        {
            Console.WriteLine("Rate limit exceeded. Try again later.");
            return null;
        }

        response.EnsureSuccessStatusCode();
        return await response.Content.ReadFromJsonAsync<List<ExchangeRate>>();
    }
    catch (HttpRequestException ex)
    {
        Console.WriteLine($"Request failed: {ex.Message}");
        return null;
    }
}

Without Typed Models (Using JsonDocument)

For quick scripts or when you do not want to define model classes, use JsonDocument to parse the response dynamically.

using System.Text.Json;

using var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", "Bearer era_live_YOUR_API_KEY");

var json = await client.GetStringAsync(
    "https://exchange-rateapi.com/api/v1/rates?source=USD&target=EUR");

using var doc = JsonDocument.Parse(json);
var rate = doc.RootElement[0].GetProperty("rate").GetDouble();
var target = doc.RootElement[0].GetProperty("target").GetString();
Console.WriteLine($"1 USD = {rate} {target}");

Response Format

Single Target Response

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

Multiple Targets Response

[
  { "rate": 0.9215, "source": "USD", "target": "EUR", "time": "2026-05-15T12:00:00Z" },
  { "rate": 0.7891, "source": "USD", "target": "GBP", "time": "2026-05-15T12:00:00Z" },
  { "rate": 151.42, "source": "USD", "target": "JPY", "time": "2026-05-15T12:00:00Z" }
]
.NET 6+ recommended — The examples above use top-level statements and record types available in .NET 6+. For older .NET versions, wrap the code in a Main method and use regular classes instead of records.

Start building with C#

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

Get Free API Key →