A modern, production-ready .NET 9 Class Library and Sample Application for the BTCMarkets V3 API.
This solution (BtcMarkets.sln) is organized into the following components:
src/BtcMarkets.Client: The reusable Class Library containing all API logic, models, exceptions, andServiceCollectionextensions.src/BtcMarkets.ConsoleSample: A console application demonstrating how to consume the library to place, list, and cancel orders.tests/BtcMarkets.Client.Tests: xUnit test project with unit tests for the core library.
- .NET 9: Built for the latest .NET release.
- Dependency Injection: Seamless integration via
services.AddBtcMarkets(). - Robust Error Handling: Typed
BtcMarketsExceptionwith HTTP status codes. - Structured Logging: Full
ILoggersupport. - Thread Safety: Uses
IHttpClientFactoryand per-requestHttpRequestMessagehandling. - Async/Await: Non-blocking I/O with
ConfigureAwait(false)best practices.
- .NET 9 SDK
- A BTCMarkets Account (API Key & Private Key)
Open src/BtcMarkets.ConsoleSample/appsettings.json and add your keys:
{
"BtcMarkets": {
"ApiKey": "YOUR_API_KEY",
"PrivateKey": "YOUR_PRIVATE_KEY"
}
}Run from the root directory:
dotnet builddotnet testdotnet run --project src/BtcMarkets.ConsoleSample- Add Reference: Add
BtcMarkets.Clientto your project. - Register Services: In your
Program.csorStartup.cs:
using BtcMarkets.Client;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddBtcMarkets(options =>
{
options.BaseUrl = "https://api.btcmarkets.net";
options.ApiKey = builder.Configuration["BtcMarkets:ApiKey"];
options.PrivateKey = builder.Configuration["BtcMarkets:PrivateKey"];
});- Inject and Use:
public class MyTradingService
{
private readonly IBtcMarketsClient _client;
private readonly ILogger<MyTradingService> _logger;
public MyTradingService(IBtcMarketsClient client, ILogger<MyTradingService> logger)
{
_client = client;
_logger = logger;
}
public async Task PlaceOrderAsync()
{
try
{
await _client.PlaceNewOrderAsync(new NewOrderModel { ... });
}
catch (BtcMarketsException ex)
{
_logger.LogError("API Error {Code}: {Message}", ex.StatusCode, ex.Message);
}
}
}