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.
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#.
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}");
}
}
}
}
// 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 ...
Sign up now to get your API key and start converting currencies in your csharp applications.
Get Your API Key