Binance Common Types and Utilities for Binance .NET Connectors
Binance.Common is the shared library of the Binance .NET connectors. It implements what every
connector needs to talk to the Binance REST and WebSocket APIs: configuration, request signing,
connection management, error types, logging and utilities. The connectors under src/
are built on top of it, and it comes with every one of them.
Installation
Binance.Common is installed with every connector (see the root README). To add it on its own:
dotnet add package Binance.Common
The optional Binance.Common.DependencyInjection package registers the clients with
Microsoft.Extensions.DependencyInjection.
Features
- REST client with connection reuse, retries with backoff (GET and DELETE), gzip/deflate
decoding and rate-limit headers (
RestApiResponse<T>). - Request signing with HMAC-SHA256, RSA or Ed25519 keys.
- WebSocket API client: request/response matching, connection pools, session logon that is repeated after reconnects, and user-data subscriptions that are restored after reconnects.
- WebSocket Streams client: subscriptions over connection pools and URL paths, restored after reconnects.
- Automatic connection management: reconnects with backoff, renewal every 23 hours and on
serverShutdown, and dead-connection detection. - Typed responses and events decoded on demand, with exact decimals (
decimal), 64-bit integers and extensible enums that keep values the SDK does not know. - Asynchronous calls: every request is a
Taskthat accepts aCancellationToken, and the events of a subscription are aChannelReader<T>. - Proxies (HTTP, HTTPS, SOCKS5 and SOCKS5 with remote name resolution, with authentication, or from the environment).
- TLS: private CAs (
CaFile,CaPem,CaOnly) and certificate pinning (ServerCertificateValidator). - Errors as one exception hierarchy (
BinanceException) carrying Binance'sCode. - Logging through
Microsoft.Extensions.Logging. - Trimming and Native AOT are supported.
Usage
REST
The clients of the connectors take a RestApiConfig. For example, with Binance.Spot:
using Binance.Common;
using Binance.Spot.RestApi;
var config = new RestApiConfig
{
ApiKey = "...",
ApiSecret = "...", // or PrivateKeyFile = "ed25519.pem"
};
using var client = SpotRestApi.Production(config);
var time = await client.TimeAsync();
Console.WriteLine(time.Data()!.ServerTime); // the body is decoded when Data() is called
// Status, rate limits (RestApiRateLimit: RateLimitType, Interval, IntervalNum, Count) and headers come with every response
foreach (var limit in time.RateLimits)
Console.WriteLine(limit);
WebSocket API and Streams
WebSocketApiConfig configures the clients that send WebSocket API requests and WebSocketStreamsConfig the clients that
subscribe to streams. As in the other Binance connectors, a client holds the configuration and ConnectAsync() returns an open
connection, which has the methods; every call opens an independent one. Connections reconnect, renew themselves every 23 hours and
survive serverShutdown on their own.
using Binance.Common;
using Binance.Spot.WebSocketApi;
using WebSocketStreamsNs = Binance.Spot.WebSocketStreams;
var apiConfig = new WebSocketApiConfig
{
ApiKey = "...",
PrivateKeyFile = "ed25519.pem",
};
var api = SpotWebSocketApi.Production(apiConfig);
await using var connection = await api.ConnectAsync(); // or ConnectAsync(WebSocketMode.Pool(3))
// Logs the connection on, and again after reconnects (AutoSessionRelogon)
await connection.SessionLogonAsync();
var time = await connection.TimeAsync();
Console.WriteLine(time.Data()!.ServerTime);
var streams = WebSocketStreamsNs.SpotWebSocketStreams.Production();
await using var streamsConnection = await streams.ConnectAsync();
using var trades = streamsConnection.AggTrade(new WebSocketStreamsNs.AggTradeRequest { Symbol = "bnbusdt" });
await trades.WaitSubscribedAsync(); // optional: until the server has acknowledged the SUBSCRIBE
await foreach (var trade in trades.Events.ReadAllAsync())
Console.WriteLine($"{trade.S} {trade.P}");
// Or callbacks, like stream.on('message', ...) of the other connectors (instead of the reader):
// using var callback = trades.OnMessage(trade => Console.WriteLine(trade.P));
- Requests wait for their response up to
Timeout. A request that was not answered in time fails with aBinanceException. - The WebSocket API repeats
session.logonafter a reconnect (AutoSessionRelogon) and restores user-data subscriptions. - Streams are subscribed again after a reconnect.
WaitSubscribedAsync()of a subscription completes once the server has acknowledged it, and fails withBinanceWebSocketResponseExceptionif the server rejects it.Unsubscribe()(orDispose()) ends it. WebSocketMode.Single(the default) orWebSocketMode.Pool(n), inModeof the configuration or given toConnectAsync, spreads the requests or streams overnconnections;session.logonandsession.logoutgo to every connection.- Reconnects wait
ReconnectDelay; after a failed attempt, a backoff of 1 s doubling up to 60 s, with random jitter, is added to it, and the wait is at least theRetry-Afterof a rejected upgrade. Streams of a connection that closes for good move to another connection of the pool. - A subscription's events go either to its reader (
Events) or to callbacks (OnMessage, which every event reaches in order; disposing the result removes one), not both. The reader completes when the connection is disconnected or disposed and faults with aBinanceExceptionwhen the server rejects the SUBSCRIBE; a reconnect never ends it.CompletionandIsActivereport the same. A WebSocket API user-data subscription completes only on dispose. - Connections and subscriptions hold native resources and registry entries: dispose every one of them.
PingServer()sends a ping frame. The lifecycle events of a connection (open, close, error, ping, pong) go to itsEventsreader or toOnEventcallbacks.EnableMessages()of a connection turns on the delivery of every message of its connections as the server sent it (aConnectionMessage) and returns their reader;OnMessagedelivers them to callbacks instead. The library builds them only from then on. Unbounded unlessEventBufferCapacityis set.- For what has no method yet:
SendRequestAsync(endpoint, method, ...)/SendSignedRequestAsyncof a REST client call any endpoint,SendMessageAsync/SendSignedMessageAsyncof a WebSocket API connection send any method, andSubscribe(stream)of a WebSocket Streams connection takes any stream name. They return raw JSON (JsonElement), or decode it with the type info given to the generic overloads. Subscribe(["a@trade", "b@trade"])andUnsubscribe([...])of a WebSocket Streams connection subscribe and unsubscribe stream names without a subscription object (their messages reachEnableMessages/OnMessage),IsSubscribed(stream)tells whether a stream is subscribed, andConnectAsync(streams)subscribes streams through the connection URL.
Errors
Every error derives from BinanceException, which carries the Binance Code (when there is one) and the
Kind of the failure.
- REST errors derive from
BinanceApiException:BinanceBadRequestException,BinanceUnauthorizedException,BinanceForbiddenException,BinanceNotFoundException,BinanceConflictException(409),BinanceTooManyRequestsException,BinanceRateLimitBanExceptionorBinanceServerException. They carry theHttpStatus, and the response that caused them:Body,HeadersandRateLimits. An error answered by the WebSocket API carries theWebSocketRateLimitsof that answer and itsErrorData(error.data). BinanceTimeoutException(aBinanceNetworkException) means the attempt took longer thanTimeout.BinanceNetworkException.RequestSentsays whether a failed request may have reached Binance. Check it before placing an order again.TransportCodeandTransportCategorygive the system error behind a network failure (for examplesystemand the value ofECONNREFUSED), or0and an empty category when there is none.RetryAfteris theRetry-Afterdelay of a 418 / 429 response;RetryAtis the time from theretryAfterof a WebSocket API error, which Binance sends as an epoch timestamp.- WebSocket failures carry
WebSocketKind(BinanceWebSocketErrorKind), and the exception type follows the cause:- a request without a response in time is a
BinanceTimeoutException, a lost connection or a refused connect aBinanceNetworkException, and an operation cancelled by the connector (the client was disposed) aBinanceCancelledException; - the other failures derive from
BinanceWebSocketException:BinanceWebSocketNotConnectedException,BinanceWebSocketNoResponseException(the connection closed before the response arrived: checkRequestSent),BinanceWebSocketResponseException(an error answered by the WebSocket API, withHttpStatus,CodeandRetryAt),BinanceWebSocketHandshakeException(the upgrade was rejected, withHttpStatus),BinanceWebSocketProtocolExceptionandBinanceWebSocketServerException; any other failure is a plainBinanceWebSocketException.
- a request without a response in time is a
- Invalid configurations throw
BinanceConfigException. - A cancelled token throws
OperationCanceledException; an operation cancelled by the connector (the client was disposed) throwsBinanceCancelledException. Cancelling a request (REST or WebSocket API) aborts it; cancellingConnectAsync(the connection is then disposed),DisconnectAsync,WaitSubscribedAsyncor a user-dataSubscribeAsyncabandons the wait only: a disconnect may still close the connections, and a user-data subscribe may still open the subscription on the server.
using var client = SpotRestApi.Production();
try
{
await client.TimeAsync();
}
catch (BinanceTooManyRequestsException e)
{
await Task.Delay(e.RetryAfter ?? TimeSpan.FromSeconds(1));
}
catch (BinanceNetworkException e) when (!e.RequestSent)
{
// The request never reached the server: it is safe to send it again.
}
Configuration
Timeout: how long one attempt of a request may take as a whole (DNS, connect, proxy, TLS, writing the request and reading the response), as in the other Binance connectors; a retry starts a new attempt.Retries/Backoff(GET and DELETE only) andKeepAlive.CompressionandMaxResponseSize.Proxy: aProxyProtocol(Http,Https,Socks5orSocks5h;Socks5hlets the proxy resolve the host name), with a user name and password. Without it,HTTPS_PROXY/HTTP_PROXY/ALL_PROXY/NO_PROXYfrom the environment are used whenProxyFromEnvis set: by default for REST, and not for WebSockets, as in the other Binance connectors.- TLS:
CaFile/CaPemadd trusted CAs to the system store (a private CA),CaOnlytrusts only them, andServerCertificateValidatoris called with each certificate of the chain (X509Certificate2, depth, whether the built-in checks passed) to pin a certificate or take over the decision. An exception it throws rejects the certificate. CustomHeadersandTimeUnit. TheUser-Agentnames the product and its version; the clients set it.TimeOffset: a fixed offset (TimeSpan, may be negative, at most one day) added to the host clock for thetimestampof signed REST requests, signed WebSocket API requests and signed streams, including retries, re-logons, reconnects and renewals. Set it toserverTime - local timewhen requests fail with-1021.TimestampProvider(aFunc<DateTimeOffset>) replaces the clock altogether, for example with one synchronised with Binance; set one of the two.ClientCertificatePem/ClientCertificateFilewithClientKeyPem/ClientKeyFile(andClientKeyPassphrase) present a client certificate (mutual TLS).- The
Idof a WebSocket API request is used only if it is a string of 32 lower-case hexadecimal characters; any other value is replaced by a generated id. ApiKeywithApiSecret(HMAC-SHA256), orPrivateKeyPem/PrivateKeyFile(RSA or Ed25519) withPrivateKeyPassphrase.- WebSocket:
ReconnectDelay,ConnectTimeout,Mode,MaxMessageSize,AutoSessionRelogon(WebSocket API) andEventBufferCapacity, which bounds the buffer of each subscription (the oldest event is dropped when a slow reader lets it fill up; unbounded by default).
config.ToString() redacts every secret. A client copies its configuration when it is created; changing it afterwards has no
effect.
Pinning a certificate and trusting a private CA:
using Binance.Common;
using System.Security.Cryptography;
var config = new RestApiConfig
{
CaFile = "/path/to/private-ca.pem",
ServerCertificateValidator = (certificate, depth, preverified) =>
depth != 0 ? preverified : certificate.GetCertHashString(HashAlgorithmName.SHA256) == "<SHA-256 of the server certificate>",
};
Models and decoding
- Responses and events are typed models. Amounts the API sends as numbers are
decimal, ids arelong, and a field that the API documents as a closed set of strings is an extensible enum: a struct with a static member for each known value (NewOrderSide.Buy) that also carries a value the SDK does not know, so a new value from the server never fails a call (new NewOrderSide("SOMETHING_NEW"),Value). - A response body that does not decode into the model fails with a
BinanceClientExceptionwhose message names the model and starts with the payload. A stream message that does not decode is skipped, logged as a warning and counted inDecodeFailureCountof the subscription; the stream goes on. - Values with several possible shapes are
OneOftypes:TryAs<T>(out var value)decodes the rawJsonas one of theAlternativeTypes(false when it is not one), andAs<T>()does the same for models and lists, returning null.
Logging
The log is process-wide. BinanceLogging.Configure(loggerFactory) sends it to ILogger (the level is the lowest one the
logger enables), BinanceLogging.SetLevel(BinanceLogLevel.Debug) sets the level, and Configure(null) writes to stderr again.
Without configuration, warnings and errors go to stderr; the LOG_LEVEL environment variable (debug, info, warn,
error or none) changes that. The Binance.Common.DependencyInjection package configures it for
you.
using Binance.Common;
using Microsoft.Extensions.Logging;
using var loggerFactory = LoggerFactory.Create(b => b.AddConsole().SetMinimumLevel(LogLevel.Debug));
BinanceLogging.Configure(loggerFactory);
Supported platforms
The package ships a platform library in runtimes/<rid>/native/ for each of these runtime identifiers:
| Platform | RID | Oldest version |
|---|---|---|
| Windows x64 / x86 / ARM64 | win-x64, win-x86, win-arm64 |
Windows 10, Windows Server 2016 |
| Linux (glibc) x64 / ARM64 | linux-x64, linux-arm64 |
glibc 2.28: RHEL / AlmaLinux / Rocky 8, Debian 10, Ubuntu 20.04, Amazon Linux 2023 |
| Linux (glibc) 32-bit ARM (for example Raspberry Pi OS) | linux-arm |
glibc 2.36: Debian 12, Raspberry Pi OS 12 (Bookworm) |
| Linux (musl, for example Alpine) x64 / ARM64 | linux-musl-x64, linux-musl-arm64 |
built and tested on Alpine 3.20 |
| macOS Intel / Apple silicon | osx-x64, osx-arm64 |
macOS 12 |
The release build checks the oldest glibc and macOS versions that each library needs. On glibc Linux x64 / ARM64 the library
also uses the system's libstdc++.so.6 (any version since GCC 8), which .NET itself needs.
.NET picks the right one for your system; nothing has to be installed besides the package. A BinanceLibraryException at the
first call means that the library for your platform is missing or does not match the package; its message lists the supported
identifiers and the one of the running process.
For diagnostics, the BINANCE_C_LIB environment variable can name the path of a platform library to load instead of the shipped one.
Contributing
Contributions are welcome. See the root README: open a GitHub issue before making changes and
discuss them with the maintainers. This folder (src/Binance.Common/) and its tests (tests/Binance.Common.Tests/) are maintained by hand; follow the existing C# structure.
Disclaimer
This SDK is provided by Binance on an "as is" and "as available" basis for use at your own risk. Binance makes no representations or warranties of any kind, whether express or implied, as to the operation of the SDK, its accuracy, reliability, completeness, or fitness for any particular purpose.
To the fullest extent permitted by law, Binance shall not be liable for any losses, damages, or expenses of any kind arising from or in connection with your use of, or inability to use, this SDK, including but not limited to any financial losses resulting from errors, bugs, interruptions, or inaccuracies in the SDK.
Your use of this SDK to access the Binance Platform is subject to the Binance API Key Terms and the Binance Terms of Use, which shall prevail in the event of any conflict with this disclaimer. You are solely responsible for any orders or transactions executed through the Binance Platform using this SDK.
This SDK is not intended to constitute investment advice or a recommendation to buy, sell, or hold any digital asset. You should independently evaluate and verify all information before acting.
License
This project is licensed under the MIT License - see the LICENCE file for details.