A .NET Standard 2.0+ wrapper for the Binance API.
Built for ease-of-use and extensibility.
BinanceClient
class.PM> Install-Package Binance.NET
Binance.NET can be built on Windows, Mac, and Linux.
git clone https://github.com/pgibler/Binance.NET
cd Binance.NET/src
dotnet build
Binance.NET/src/Binance.NET/bin/Debug/netstandard{version}
.BinanceClient
class.Once you have done these steps, to use Binance.NET in your C# application, create an instance of BinanceClient
and invoke it's functionality from your code.
// Retrieve your API key & secret from your account on binance and use those values here.
string apiKey = "<your binance API key>";
string apiSecret = "<your binance API secret>";
// Instantiate a Binance API client.
var binance = new BinanceClient(apiKey, apiSecret);
// Create a buy order.
binance.Buy("ETH-BTC", 1.0, 0.001);
// Create a sell order.
binance.Sell("ETH-BTC", 1.0, 0.0015)
In some cases, the API request may fail because of any number of reasons. In the event that the Binance API responds with an error message, the success callback is skipped on the API call and an error callback is invoked. You can set up a custom error handler in 2 ways:
// Buy order handling the response.
binance.Buy("ETH-BTC", 1.0, 0.00111213,
response => Console.WriteLine(response.OrderId),
Console.WriteLine($"API error. Message: {exception.Message}. Code: ${exception.Code}"));
DefaultExceptionCallback
property of the BinanceClient
class.// Handle all unmanaged exceptions across all API calls.
binance.DefaultExceptionCallback = exception => {
Console.WriteLine($"API error. Message: {exception.Message}. Code: ${exception.Code}"));
Logger.Log(exception);
}
You can also mix both methods. If you specify a DefaultExceptionCallback
, it will be invoked whenever an API call results in an exception if you did not provide the API call with an exception callback. This way you can handle exceptions in a general use case and then specifically handle ones that require custom behavior.
For testing purposes, you can choose to eschew the usage of these as you try out the API methods. However, it is recommended you use them to see what goes wrong when you are testing.
Included in Binance.NET are the following API calls. All of these functions are members of the BinanceClient
class.
Buy(string symbol, double quantity, double price)
Submits a buy order.
Sell(string symbol, double quantity, double price)
Submits a sell order.
CancelOrder(string symbol, long orderId)
Cancels an order.
OrderStatus(string symbol, long orderId, Action<OrderResponse> successCallback)
Returns the status of an open order.
OpenOrders(string symbol, Action<OpenOrdersResponse> successCallback)
Returns a list of all open orders.
AllOrders(string symbol, Action<AllOrdersResponse> successCallback)
Returns a list of all orders from the account.
Depth(string symbol, Action<DepthCache> callback)
Returns the current depth of a symbol.
Prices(Action<Dictionary<string, double>> callback)
Returns all price data.
BookTickers(Action<Dictionary<string, BookPrice>> callback)
Returns all book tickers.
PreviousDay(string symbol, Action<PreviousDayResponse> successCallback)
Returns the 24hr ticker price change statistics.
Account(Action<AccountResponse> successCallback)
Get the account info associated with the API key & secret.
Balance(Action<Dictionary<string, Balance>> callback)
Get the balance of all symbols from the account.
Trades(string symbol, Action<TradesResponse> successCallback)
Get all trades the account is involved in.
DepthCache(string symbol)
Returns the depth cache of the symbol.
DepthVolume(string symbol)
Returns the depth volume of the symbol.
SortBids(string symbol, double max, bool baseValue)
Sorts all bids then collects them up until the max number of bids has been collected.
SortAsks(string symbol, double max, bool baseValue)
Sorts all asks then collects them up until the max number of asks has been collected.
Binance.NET comes with a set of streams that you can run to listen and communicate with the Binance WebSocket services.
Each of these functions return a CancellationTokenSource
instance so you can halt its operation as needed.
DepthStream(string[] symbols, Action<DepthStreamResponse> successCallback)
Opens a stream that invokes the callback when data is received on any of the specified symbols.
DepthCacheStream(string[] symbols, Action<string, DepthCache> callback)
Opens a depth cache stream that invokes the callback when data is received on any of the specified symbols.
TradesStream(string[] symbols, Action<TradesStreamResponse> successCallback)
Opens a trades stream that invokes the callback when data is received on any of the specified symbols.
ChartStream(string[] symbols, long interval, Action<JToken, long, Dictionary<long, OpenHighLowClose>> successCallback)
Opens a charts stream that invokes the callback when data is received on any of the specified symbols.