Package Description
$ dotnet add package Tor.Currencylayer.ClientA C# client library for Currencylayer.com API with dependency injection support.
Install-Package Tor.Currencylayer.Client
You have to register the CurrencylayerClient with the dependencies in the Program.cs file.
For the minimal registration, you have to add your Currencylayer API key to the options builder:
services.AddCurrencylayer(options =>
{
options.WithApiKey("Your API key");
});
Setting the API key:
services.AddCurrencylayer(options =>
{
options.WithApiKey("Your Currencylayer API key");
});
If you want to implement some API key factory logic, f.e.: if you want to change your API key in runtime:
public static class SharedData
{
public static string ApiKey { get; set; } = "Your Currencylayer API key";
}
services.AddCurrencylayer(options =>
{
options.WithApiKeyFactory(() => SharedData.ApiKey);
});
If you use an alias accessing Currencylayer or the Currencylayer base address changes and this package is not updated yet, you can override the base address:
services.AddCurrencylayer(options =>
{
options.WithBaseUrl("Your URL");
});
Based on your design, you can choose a http error handling mode with the following code:
services.AddCurrencylayer(options =>
{
options.WithHttpErrorHandling(HttpErrorHandlingMode.ReturnsError);
});
There are two options (default: ReturnsError):
Of course, you can combine these options for your needs except WithApiKey and WithApiKeyFactory.
You can get the ICurrencylayerClient via dependency injection:
public class MyService
{
public MyService(ICurrencylayerClient client)
{
}
}
NOTE: Please note that depending on your subscription plan, certain API endpoints may or may not be available.
Every method call will return with the following CurrencylayerResponse class:
public class CurrencylayerResponse<TResult>
{
public bool Success { get; set; }
public TResult Result { get; set; }
public CurrencylayerError Error { get; set; }
public string Terms { get; set; }
public string Privacy { get; set; }
}
Method parameters:
| Parameter | Description | Optional / Required |
|---|---|---|
| sourceCurrencyCode | Three letter source currency code | Optional |
| destinationCurrencyCodes | The codes of the expected result destination currencies | Optional |
Response:
| Property | Description |
|---|---|
| SourceCurrencyCode | Three letter source currency code |
| Timestamp | The UNIX timestamp of the data |
| Rates | List of the exchange rates |
| Rates -> CurrencyCode | Three letter currency code |
| Rates -> ExchangeRate | Exchange rate |
Method parameters:
| Parameter | Description | Optional / Required |
|---|---|---|
| date | A date in the past for which historical rates are requested | Required |
| sourceCurrencyCode | Three letter source currency code | Optional |
| destinationCurrencyCodes | The codes of the expected result destination currencies | Optional |
Response:
| Property | Description |
|---|---|
| Historical | true / false |
| SourceCurrencyCode | Three letter source currency code |
| Date | The date of the data |
| Timestamp | The UNIX timestamp of the data |
| Rates | List of the exchange rates |
| Rates -> CurrencyCode | Three letter currency code |
| Rates -> ExchangeRate | Exchange rate |
Method parameters:
| Parameter | Description | Optional / Required |
|---|---|---|
| sourceCurrencyCode | Three letter source currency code | Required |
| destinationCurrencyCode | Three letter destination currency code | Required |
| amount | Amount to exchange | Required |
| date | A date in the past for which exchange is requested | Optional |
Response:
| Property | Description |
|---|---|
| Result | The converted amount |
| Historical | true / false |
| Date | The date of the data (null if not historical) |
| Query | Request query info |
| Query -> SourceCurrencyCode | Three letter source currency code |
| Query -> DestinationCurrencyCode | Three letter destination currency code |
| Query -> Amount | Amount to exchange |
| Info | Exchange info |
| Info -> Timestamp | The UNIX timestamp of the exchange rate |
| Info -> Rate | Exchange rate |
Method parameters:
| Parameter | Description | Optional / Required |
|---|---|---|
| startDate | Start date | Required |
| endDate | End date | Required |
| baseCurrencyCode | Three letter base currency code | Optional |
| destinationCurrencyCodes | The codes of the expected result destination currencies | Optional |
Response:
| Property | Description |
|---|---|
| TimeFrame | true / false |
| StartDate | The start date |
| EndDate | The end date |
| SourceCurrencyCode | Three letter source currency code |
| Items | Exchange rate list |
| Items -> Date | Date of the exchanges |
| Items -> Rates -> CurrencyCode | Three letter currency code |
| Items -> Rates -> ExchangeRate | Exchange rate |
Method parameters:
| Parameter | Description | Optional / Required |
|---|---|---|
| startDate | Start date | Required |
| endDate | End date | Required |
| sourceCurrencyCode | Three letter source currency code | Optional |
| destinationCurrencyCodes | The codes of the expected result destination currencies | Optional |
Response:
| Property | Description |
|---|---|
| Change | true / false |
| StartDate | The start date |
| EndDate | The end date |
| SourceCurrencyCode | Three letter source currency code |
| Rates | Exchange fluctuation list |
| Rates -> CurrencyCode | Three letter currency code |
| Rates -> StartRate | Start date |
| Rates -> EndRate | End date |
| Rates -> Change | The change of the given currency rate between your start and end date |
| Rates -> ChangePercentage | The percentage change of the given currency rate between your start and end date |