Binance.Net
Binance.Net is a strongly typed client library for accessing the Binance REST and Websocket API. All data is mapped to readable models and enum values. Additional features include an implementation for maintaining a client side order book, easy integration with other exchange client libraries and more.
Supported Frameworks
The library is targeting both .NET Standard 2.0
and .NET Standard 2.1
for optimal compatibility
.NET implementation | Version Support |
---|---|
.NET Core | 2.0 and higher |
.NET Framework | 4.6.1 and higher |
Mono | 5.4 and higher |
Xamarin.iOS | 10.14 and higher |
Xamarin.Android | 8.0 and higher |
UWP | 10.0.16299 and higher |
Unity | 2018.1 and higher |
Discord
A Discord server is available here. Feel free to join for discussion and/or questions around the CryptoExchange.Net and implementation libraries.
Support the project
DonateMake a one time donation in a crypto currency of your choice. If you prefer to donate a currency not listed here please contact me.
Btc: bc1q277a5n54s2l2mzlu778ef7lpkwhjhyvghuv8qf
Eth: 0xcb1b63aCF9fef2755eBf4a0506250074496Ad5b7
USDT (TRX): TKigKeJPXZYyMVDgMyXxMf17MWYia92Rjd
Alternatively, sponsor me on Github using Github Sponsors.
Getting Started
The packge is available on Nuget. After installing the package the Binance API is available by using the BinanceRestClient
and BinanceSocketClient
.
Installation
Add the package via dotnet, or add it via the package manager.
dotnet add package Binance.Net
API Access
Binance.Net can be configured using Dotnet dependency injection, after which the clients can be injected into your services. It also correctly configures logging and HttpClient usage.
builder.Services.AddBinance(options => {
// Options can be configured here, for example:
options.ApiCredentials = new ApiCredentials("APIKEY", "APISECRET");
});
The IBinanceRestClient
and IBinanceSocketClient
can then be injected.
Alternatively the rest and socket client can be constructed directly
var restClient = new BinanceRestClient(options => {
// Options can be configured here, for example:
options.ApiCredentials = new ApiCredentials("APIKEY", "APISECRET");
});
var socketClient = new BinanceSocketClient();
Rate limiting
By default a rate limiter is enabled which makes sure the server rate limits aren't exceeded. For more info on what the rate limits for the Binance API are see the API docs. Note that rate limiting is only implemented to prevent going over the request rate limit, it does not apply for order limits.
Whether client ratelimiting is enabled in the library and what the behaviour should be when the limit is reached can be controlled with the RatelimiterEnabled
and RateLimitingBehaviour
client options
services.AddBinance(x =>
x.RatelimiterEnabled = true;
x.RateLimitingBehaviour = RateLimitingBehaviour.Wait;
}, x =>
{
x.RatelimiterEnabled = true;
x.RateLimitingBehaviour = RateLimitingBehaviour.Wait;
});
To be notified of when a rate limit is hit the static BinanceExchange.RateLimiter
exposes an event which triggers when a rate limit is reached
BinanceExchange.RateLimiter.RateLimitTriggered += (rateLimitEvent) => Console.WriteLine("Limit triggered: " + rateLimitEvent);
// Output: Limit triggered: RateLimitEvent { ApiLimit = Spot Socket, LimitDescription = Limit of 6000 per 00:01:00, RequestDefinition = GET 1, Host = wss://ws-api.binance.com, Current = 5752, RequestWeight = 250, Limit = 6000, TimePeriod = 00:01:00, DelayTime = 00:00:38.7784145, Behaviour = Wait }
Examples
Spot API
Get SymbolsGet a list of supported spot symbols
var binanceClient = new BinanceRestClient();
var result = await binanceClient.SpotApi.ExchangeData.GetExchangeInfoAsync();
Get Symbol TickerGet the 24h price ticker of a spot symbol
var binanceClient = new BinanceRestClient();
var result = await binanceClient.SpotApi.ExchangeData.GetTickerAsync("ETHUSDT");
Get Recent TradesGet the most recent trades for a spot symbol
var binanceClient = new BinanceRestClient();
var result = await binanceClient.SpotApi.ExchangeData.GetRecentTradesAsync("ETHUSDT");
Get BalancesGet asset balances of the spot account
var binanceClient = new BinanceRestClient(options => {
options.ApiCredentials = new ApiCredentials("KEY", "SECRET");
});
var result = await binanceClient.SpotApi.Account.GetBalancesAsync();
Place Limit OrderPlace a new limit order for buying 0.1 ETH at a price of 2000 USDT
var binanceClient = new BinanceRestClient(options => {
options.ApiCredentials = new ApiCredentials("KEY", "SECRET");
});
var result = await binanceClient.SpotApi.Trading.PlaceOrderAsync(
"ETHUSDT",
Binance.Net.Enums.OrderSide.Buy,
Binance.Net.Enums.SpotOrderType.Limit,
0.1m,
2000,
timeInForce: Binance.Net.Enums.TimeInForce.GoodTillCanceled);
Place Market OrderPlace a new market order for buying 50 USDT worth of ETH at the best available price
var binanceClient = new BinanceRestClient(options => {
options.ApiCredentials = new ApiCredentials("KEY", "SECRET");
});
var result = await binanceClient.SpotApi.Trading.PlaceOrderAsync(
"ETHUSDT",
Binance.Net.Enums.OrderSide.Buy,
Binance.Net.Enums.SpotOrderType.Market,
quoteQuantity: 50);
Get Order InfoRetrieve order information for a specific order
var binanceClient = new BinanceRestClient(options => {
options.ApiCredentials = new ApiCredentials("KEY", "SECRET");
});
var result = await binanceClient.SpotApi.Trading.GetOrderAsync("ETHUSDT", 123);
Cancel an orderCancel a currently active order
var binanceClient = new BinanceRestClient(options => {
options.ApiCredentials = new ApiCredentials("KEY", "SECRET");
});
var result = await binanceClient.SpotApi.Trading.CancelOrderAsync("ETHUSDT", 123);
Usd Futures API
Get SymbolsGet a list of supported usd futures symbols
var binanceClient = new BinanceRestClient();
var result = await binanceClient.UsdFuturesApi.ExchangeData.GetExchangeInfoAsync();
Get Symbol TickerGet the 24h price ticker of a spot symbol
var binanceClient = new BinanceRestClient();
var result = await binanceClient.UsdFuturesApi.ExchangeData.GetTickerAsync("ETHUSDT");
Get Recent TradesGet the most recent trades for a spot symbol
var binanceClient = new BinanceRestClient();
var result = await binanceClient.UsdFuturesApi.ExchangeData.GetRecentTradesAsync("ETHUSDT");
Get BalancesGet balance of the perpetual futures account
var binanceClient = new BinanceRestClient(options => {
options.ApiCredentials = new ApiCredentials("KEY", "SECRET");
});
var result = await binanceClient.UsdFuturesApi.Account.GetBalancesAsync();
Get PositionsGet open positions
var binanceClient = new BinanceRestClient(options => {
options.ApiCredentials = new ApiCredentials("KEY", "SECRET");
});
var result = await binanceClient.UsdFuturesApi.Account.GetPositionInformationAsync();
Place Limit OrderPlace a new limit order for going long on 0.1 ETH at a price of 2000 USDT
var binanceClient = new BinanceRestClient(options => {
options.ApiCredentials = new ApiCredentials("KEY", "SECRET");
});
var result = await binanceClient.UsdFuturesApi.Trading.PlaceOrderAsync(
"ETHUSDT",
Binance.Net.Enums.OrderSide.Buy,
Binance.Net.Enums.FuturesOrderType.Limit,
0.1m,
2000,
positionSide: Binance.Net.Enums.PositionSide.Long);