A .NET API library (a.k.a. wrapper) for the official Binance web API.
Compatible with .NET Standard 2.0 and .NET Framework 4.7.1
Built using TAP (Task-based Asynchronous Pattern).
Symbol.BTC_USDT
) with exchange info (price/quantity: min, max, etc.).
HttpClient
for performance (implemented as singleton).BinanceApi
are available for application-level deserialization of JSON data.IWebSocketClients
for using WebSocketSharp or WebSocket4Net (for Windows 7 compatibility).BinanceWebSocketStream
).To use the private (authenticated) API methods you must have an account with Binance and create an API-Key. Please use my Referral ID: 10899093 when you Register (it's an easy way to give back at no cost to you).
NOTE: An account is not required to access the public market data.
Using Nuget Package Manager:
PM> Install-Package Binance
Test connectivity.
using Binance;
// Initialize REST API client.
var api = new BinanceApi();
// Check connectivity.
if (await api.PingAsync())
{
Console.WriteLine("Successful!");
}
Place a TEST market order.
using Binance;
var api = new BinanceApi();
// Create user with API-Key and API-Secret.
using (var user = new BinanceApiUser("<API-Key>", "<API-Secret>"))
{
// Create a client (MARKET) order.
var clientOrder = new MarketOrder(user)
{
Symbol = Symbol.BTC_USDT,
Side = OrderSide.Buy,
Quantity = 0.01m
};
try
{
// Send the TEST order.
await api.TestPlaceAsync(clientOrder);
Console.WriteLine("TEST Order Successful!");
}
catch (Exception e)
{
Console.WriteLine($"TEST Order Failed: \"{e.Message}\"");
}
}
Get real-time aggregate trades (with automatic web socket re-connect).
using Binance;
using Binance.WebSocket;
// Initialize web socket client (with automatic streaming).
var webSocketClient = new AggregateTradeWebSocketClient();
// Handle error events.
webSocketClient.Error += (s, e) => { Console.WriteLine(e.Exception.Message); };
// Subscribe callback to BTC/USDT (automatically begin streaming).
webSocketClient.Subscribe(Symbol.BTC_USDT, evt =>
{
var side = evt.Trade.IsBuyerMaker ? "SELL" : "BUY ";
// Handle aggregate trade events.
Console.WriteLine($"{evt.Trade.Symbol} {side} {evt.Trade.Quantity} @ {evt.Trade.Price}");
});
// ...
// Unsubscribe (automatically end streaming).
webSocketClient.Unsubscribe();
Maintain real-time order book (market depth) cache.
using Binance;
using Binance.Cache;
using Binance.WebSocket;
// Initialize web socket cache (with automatic streaming).
var webSocketCache = new DepthWebSocketCache();
// Handle error events.
webSocketCache.Error += (s, e) => { Console.WriteLine(e.Exception.Message); };
// Subscribe callback to BTC/USDT (automatically begin streaming).
webSocketCache.Subscribe(Symbol.BTC_USDT, evt =>
{
// Get symbol from cache (update cache if a symbol is missing).
var symbol = Symbol.Cache.Get(evt.OrderBook.Symbol);
var minBidPrice = evt.OrderBook.Bids.Last().Price;
var maxAskPrice = evt.OrderBook.Asks.Last().Price;
// Handle order book update events.
Console.WriteLine($"Bid Quantity: {evt.OrderBook.Depth(minBidPrice)} {symbol.BaseAsset} - " +
$"Ask Quantity: {evt.OrderBook.Depth(maxAskPrice)} {symbol.BaseAsset}");
});
// ...
// Unsubscribe (automatically end streaming).
webSocketCache.Unsubscribe();
See: Wiki
NOTE: The samples demonstrate up-to-date usage of this library.
REST/WebSocket details: Binance Official Documentation\ REST/WebSocket questions: Binance Official API Telegram (not for questions about this library)
The master branch is currently used for development and may differ from the latest release.\ To get the source code for a particular release, first select the corresponding Tag.
Microsoft Visual Studio Community 2017
Decred (DCR): Dsd7amDBfC7n6G93NJzbaJjUdiSeN5D3tmR
Thank you.
Follow @sonvister