Getting Started
BinanceAPI.NET
is a High Performance Rest API/Websocket Wrapper for Binance.com
with an emphasis on creating requests quickly and caching data when possible so requests are more responsive,
This library the best option if you want to trade Spot
or Margin
.
Authentication
Create an Authentication Provider
that can be used by multiple Rest Clients
AuthenticationProvider authenticationProvider = new AuthenticationProvider(api_key, api_secret);
Create Rest Client
Change the Rest Client Options
and create a new Rest Client
using the Authentication Provider
above
RestClientOptions restClientOptions = new()
{
SyncUpdateTime = 5,
GetCacheSize = 2048,
PutCacheSize = 2048,
PostCacheSize = 2048,
DeleteCacheSize = 2048,
ReceiveWindow = TimeSpan.FromMilliseconds(1000),
DefaultApiController = BinanceApiController.DEFAULT
};
RestClient restClient = new RestClient(authenticationProvider, restClientOptions, CancellationToken.None);
Create Socket Client
Create a Socket Client
that can be used to Connect/Subscribe
to Websocket Streams
Returns a SocketClientHost
that can be used to manage the connection.
SocketClient socketClient = new SocketClient(50);
SocketClientHost example = socketClient.SymbolMiniTickerUpdates(...);
example.ConnectionStatusChanged += BinanceSocket_StatusChanged;
example.ReconnectSocket();
example.DestroySocket();
50
is the default number of Reconnect Attempts
before the socket will Fail
Simple Ticker Example
Subscribe to Real Time LastPrice Updates
for a particular symbol
using BinanceAPI;
using BinanceAPI.Hosts;
namespace FullSocketExample
{
public class LastPriceTicker
{
private readonly SocketClientHost TickerHost;
public decimal LastPrice = decimal.Zero;
public LastPriceTicker(string symbol, ref RestClient restClient, ref SocketClient socketClient)
{
LastPrice = restClient.Spot.Market.GetPrice(symbol).Data?.Price ?? decimal.Zero;
TickerHost = socketClient.RealTimeLastPriceUpdates(symbol, (update) =>
{
if (update != null)
{
LastPrice = update.LastPrice;
}
});
}
public void Dispose()
{
TickerHost.DestroySocket();
TickerHost.Dispose();
}
}
}