Free C# Exchange Rate API

Welcome to the UnirateAPI C# documentation! Our free exchange rate API provides real-time currency conversion capabilities that you can easily integrate into your .NET applications. Whether you're building a web application, desktop software, mobile app, or any other .NET system that needs currency conversion, our API has you covered.

UnirateAPI is designed to be developer-friendly and reliable, with a focus on simplicity and performance. This guide will show you how to make API requests using C# and the .NET framework.

With UnirateAPI, you get:

  • Real-time exchange rates from reliable sources
  • Simple RESTful API endpoints
  • Support for 590+ currencies
  • Completely free with no credit card required
  • 99.9% uptime guarantee
  • Fast response times (avg. 50ms)
  • Automatic currency updates every hour

Using our API with C# is:

  • Perfectly integrated with .NET (Core, Framework, and Xamarin)
  • Simple to implement with HttpClient or RestSharp
  • Compatible with modern JSON serialization (System.Text.Json or Newtonsoft.Json)
  • Fully async-compatible with Task-based operations
  • Works with both Dependency Injection and traditional patterns

For more detailed information about our API endpoints, rate limits, and advanced features, please check our main API documentation.

Below you'll find examples showing how to use our API with C#.

Getting Started with csharp

You can use our API in C# by making HTTP requests using the built-in HttpClient or a third-party library like RestSharp. We'll show you both approaches below.


// Option 1: Using the built-in HttpClient (.NET Core, .NET 5+, or .NET Framework with NuGet package)

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using System.Web;

namespace UnirateApiExample
{
    public class UnirateApiClient
    {
        private readonly HttpClient _httpClient;
        private readonly string _apiKey;
        private readonly string _baseUrl = "https://api.unirateapi.com/api";

        public UnirateApiClient(string apiKey)
        {
            _apiKey = apiKey ?? throw new ArgumentNullException(nameof(apiKey));
            _httpClient = new HttpClient();
        }

        // Convert currency
        public async Task ConvertAsync(string fromCurrency, string toCurrency, decimal amount)
        {
            var queryString = HttpUtility.ParseQueryString(string.Empty);
            queryString["api_key"] = _apiKey;
            queryString["from"] = fromCurrency;
            queryString["to"] = toCurrency;
            queryString["amount"] = amount.ToString();

            var requestUrl = $"{_baseUrl}/convert?{queryString}";
            var response = await _httpClient.GetAsync(requestUrl);
            
            response.EnsureSuccessStatusCode();
            
            var content = await response.Content.ReadAsStringAsync();
            return JsonSerializer.Deserialize(content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
        }

        // Get exchange rate
        public async Task GetRateAsync(string fromCurrency, string toCurrency)
        {
            var queryString = HttpUtility.ParseQueryString(string.Empty);
            queryString["api_key"] = _apiKey;
            queryString["base"] = fromCurrency;
            queryString["symbols"] = toCurrency;

            var requestUrl = $"{_baseUrl}/rates?{queryString}";
            var response = await _httpClient.GetAsync(requestUrl);
            
            response.EnsureSuccessStatusCode();
            
            var content = await response.Content.ReadAsStringAsync();
            var ratesResponse = JsonSerializer.Deserialize(content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
            
            return ratesResponse.Rates[toCurrency];
        }

        // Get supported currencies
        public async Task> GetCurrenciesAsync()
        {
            var queryString = HttpUtility.ParseQueryString(string.Empty);
            queryString["api_key"] = _apiKey;

            var requestUrl = $"{_baseUrl}/currencies?{queryString}";
            var response = await _httpClient.GetAsync(requestUrl);
            
            response.EnsureSuccessStatusCode();
            
            var content = await response.Content.ReadAsStringAsync();
            return JsonSerializer.Deserialize>(content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
        }
    }

    public class ConversionResult
    {
        public string From { get; set; }
        public string To { get; set; }
        public decimal Amount { get; set; }
        public decimal Result { get; set; }
    }

    public class RatesResponse
    {
        public string Base { get; set; }
        public Dictionary Rates { get; set; }
    }

    class Program
    {
        static async Task Main(string[] args)
        {
            try
            {
                var apiKey = "YOUR_API_KEY";
                var client = new UnirateApiClient(apiKey);

                // Convert 100 USD to EUR
                var conversionResult = await client.ConvertAsync("USD", "EUR", 100);
                Console.WriteLine($"Converted amount: {conversionResult.Result} {conversionResult.To}");

                // Get exchange rate between USD and EUR
                var rate = await client.GetRateAsync("USD", "EUR");
                Console.WriteLine($"1 USD = {rate} EUR");

                // Get supported currencies
                var currencies = await client.GetCurrenciesAsync();
                Console.WriteLine("Supported currencies:");
                int count = 0;
                foreach (var currency in currencies)
                {
                    Console.Write($"{currency.Key} ");
                    count++;
                    if (count >= 5) break;
                }
                Console.WriteLine("...");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
    }
}


// Option 2: Using RestSharp (popular HTTP client library)
// First, install RestSharp: Install-Package RestSharp

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Newtonsoft.Json;
using RestSharp;

namespace UnirateApiRestSharpExample
{
    public class UnirateApiRestClient
    {
        private readonly RestClient _client;
        private readonly string _apiKey;

        public UnirateApiRestClient(string apiKey)
        {
            _apiKey = apiKey ?? throw new ArgumentNullException(nameof(apiKey));
            _client = new RestClient("https://api.unirateapi.com/api");
        }

        // Convert currency
        public async Task ConvertAsync(string fromCurrency, string toCurrency, decimal amount)
        {
            var request = new RestRequest("/convert", Method.GET);
            request.AddParameter("api_key", _apiKey);
            request.AddParameter("from", fromCurrency);
            request.AddParameter("to", toCurrency);
            request.AddParameter("amount", amount.ToString());

            var response = await _client.ExecuteAsync(request);
            
            if (!response.IsSuccessful)
                throw new Exception($"API request failed: {response.ErrorMessage}");
            
            return JsonConvert.DeserializeObject(response.Content);
        }

        // Get exchange rate
        public async Task GetRateAsync(string fromCurrency, string toCurrency)
        {
            var request = new RestRequest("/rates", Method.GET);
            request.AddParameter("api_key", _apiKey);
            request.AddParameter("base", fromCurrency);
            request.AddParameter("symbols", toCurrency);

            var response = await _client.ExecuteAsync(request);
            
            if (!response.IsSuccessful)
                throw new Exception($"API request failed: {response.ErrorMessage}");
            
            var ratesResponse = JsonConvert.DeserializeObject(response.Content);
            return ratesResponse.Rates[toCurrency];
        }
    }

    public class ConversionResult
    {
        public string From { get; set; }
        public string To { get; set; }
        public decimal Amount { get; set; }
        public decimal Result { get; set; }
    }

    public class RatesResponse
    {
        public string Base { get; set; }
        public Dictionary Rates { get; set; }
    }

    // Example usage
    class RestSharpExample
    {
        static async Task TestRestSharpClient()
        {
            try
            {
                var apiKey = "YOUR_API_KEY";
                var client = new UnirateApiRestClient(apiKey);

                // Convert 100 USD to EUR
                var result = await client.ConvertAsync("USD", "EUR", 100);
                Console.WriteLine($"Converted amount: {result.Result} {result.To}");

                // Get exchange rate
                var rate = await client.GetRateAsync("USD", "EUR");
                Console.WriteLine($"1 USD = {rate} EUR");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
    }
}

Response Example


// API Response:
{
  "from": "USD",
  "to": "EUR",
  "amount": 100,
  "result": 92.45
}

// Console Output:
Converted amount: 92.45 EUR
1 USD = 0.9245 EUR
Supported currencies: USD EUR GBP JPY AUD ...

Important Notes

  • Replace 'YOUR_API_KEY' with your actual API key
  • Use System.Text.Json for modern .NET applications, or Newtonsoft.Json for broader compatibility
  • Consider adding retry logic for network failures with Polly
  • Store your API key in appsettings.json or as an environment variable
  • Implement proper exception handling for production applications
  • Use IHttpClientFactory for proper HttpClient management in ASP.NET Core
  • Consider implementing a caching layer to reduce API calls

Ready to Get Started?

Sign up now to get your API key and start converting currencies in your csharp applications.

Get Your API Key