A comprehensive .NET client library for the Coinbase Advanced Trade API. This package provides a complete, production-ready solution with JWT authentication, built-in resilience patterns, strongly-typed models, and dependency injection support.
- JWT Authentication: Secure ES256 JWT token generation with automatic header management
- Resilience Patterns: Built-in retry policies and circuit breaker patterns using Polly
- Strongly Typed: Comprehensive models for all API requests and responses
- Dependency Injection: Easy integration with .NET DI container
- Configuration Support: Supports both appsettings.json and programmatic configuration
- Sandbox Support: Easy switching between production and sandbox environments
- Async/Await: Full async/await support with cancellation tokens
dotnet add package Coinbase.AdvancedTrade.Client --prereleaseAdd your Coinbase credentials to appsettings.json:
{
"Coinbase": {
"ApiKey": "your-api-key",
"ApiSecret": "your-private-key",
"UseSandbox": true
}
}Register the client in your DI container:
using Coinbase.AdvancedTrade.Client.Configuration;
// From configuration
services.AddCoinbaseAdvancedTradeClient(configuration);
// Or programmatically
services.AddCoinbaseAdvancedTradeClient(settings =>
{
settings.ApiKey = "your-api-key";
settings.ApiSecret = "your-private-key";
settings.UseSandbox = true;
});Inject and use the client:
using Coinbase.AdvancedTrade.Client;
using Coinbase.AdvancedTrade.Client.Models;
public class TradingService
{
private readonly ICoinbaseAdvancedTradeClient _client;
public TradingService(ICoinbaseAdvancedTradeClient client)
{
_client = client;
}
public async Task<decimal> GetBitcoinPriceAsync()
{
var response = await _client.ListProductsAsync();
if (!response.IsSuccess) return 0;
var btc = response.Data!.Products?.FirstOrDefault(p => p.ProductId == "BTC-USD");
return decimal.Parse(btc?.Price ?? "0");
}
public async Task PlaceOrderAsync()
{
var order = new OrderRequest
{
ClientOrderId = Guid.NewGuid().ToString(),
ProductId = "BTC-USD",
Side = "BUY",
OrderConfiguration = new OrderConfiguration
{
MarketMarketIoc = new MarketMarketIoc
{
QuoteSize = "10.00" // $10 worth of BTC
}
}
};
var result = await _client.PlaceOrderAsync(order);
if (result.IsSuccess && result.Data!.Success)
{
Console.WriteLine($"Order placed: {result.Data.SuccessResponse?.OrderId}");
}
else
{
Console.WriteLine($"Order failed: {result.ErrorMessage ?? result.Data?.ErrorResponse?.Message}");
}
}
}ListAccountsAsync()- List all accountsGetAccountAsync(Guid id)- Get specific account details
PlaceOrderAsync(OrderRequest request)- Place a new orderCancelOrdersAsync(List<string> orderIds)- Cancel one or more ordersGetOrdersAsync(OrderSearchRequest? request)- Search historical orders with filtersGetOrderAsync(string orderId)- Get a specific order by IDClosePositionAsync(ClosePositionRequest request)- Close a futures/perp position
ListProductsAsync()- List all available trading pairsGetProductAsync(string productId)- Get details for a specific productGetBestBidAskAsync(List<string>? productIds)- Get best bid/ask pricesGetProductCandlesAsync(string productId, long start, long end, string granularity)- Get OHLCV candlestick data
GetPortfoliosAsync()- List all portfoliosGetPortfolioBreakdownAsync(string portfolioUuid)- Get detailed portfolio breakdown
All methods return ApiResponse<T> with IsSuccess, Data, ErrorMessage, and Exception properties.
| Property | Description | Default |
|---|---|---|
ApiKey |
Your Coinbase API key | Required |
ApiSecret |
Your private key (PEM or base64) | Required |
UseSandbox |
Use sandbox environment | false |
BaseUrl |
Production API URL | https://api.coinbase.com/api/v3/brokerage |
SandboxBaseUrl |
Sandbox API URL | https://api-sandbox.coinbase.com/api/v3/brokerage |
- Market Orders:
MarketMarketIoc - Limit Orders:
LimitLimitGtcV3,LimitLimitGtdV3,LimitLimitFokV3 - Stop Orders:
StopLimitStopLimitGtcV3,StopLimitStopLimitGtdV3 - Bracket Orders:
TriggerBracketGtcV3,TriggerBracketGtdV3 - Smart Order Routing:
SorLimitIoc
All methods return ApiResponse<T> instead of throwing exceptions:
var response = await client.ListAccountsAsync();
if (response.IsSuccess)
{
var accounts = response.Data!;
// Use accounts...
}
else
{
Console.WriteLine($"Error: {response.ErrorMessage}");
// response.Exception contains the underlying exception if needed
}- Automatic retries for transient failures (429, 502, 503, 504, 408)
- Exponential backoff strategy (2^n seconds)
- 3 retry attempts
- Opens after 5 consecutive failures
- 2-minute break duration
- Automatic recovery
- Log in to Coinbase Advanced Trade
- Go to Settings > API Keys
- Create a new API key with appropriate permissions
- Download your private key and store it securely
Contributions are welcome! Please read CONTRIBUTING.md for development setup, testing, and PR guidelines. AI coding agents should also read AGENTS.md. For security vulnerabilities, see SECURITY.md.
This project is licensed under the MIT License.
This library is not officially affiliated with Coinbase. Use at your own risk. Always test thoroughly in sandbox environment before using in production.