Binance .NET Margin Trading Connector
This is a client library for the Binance Margin Trading API. Access account information, borrow and repay assets, and trade with Binance Margin.
It provides typed REST API and WebSocket Streams clients:
Table of Contents
- Supported Features
- Installation
- Documentation
- REST APIs
- WebSocket Streams
- Logging
- Testing
- Migration Guide
- Contributing
- Licence
Supported Features
- REST API Endpoints:
/sapi/*
- WebSocket Endpoints: Real-time data streaming.
- Inclusion of test cases and examples for quick onboarding.
Installation
To use this library, ensure your environment targets .NET 8 or later (net8.0 and net10.0 are supported; trimming and Native AOT work). Supported platforms: win-x64, win-x86, win-arm64, linux-x64, linux-arm64, linux-arm, linux-musl-x64, linux-musl-arm64, osx-x64 and osx-arm64.
Then install the library using dotnet:
dotnet add package Binance.MarginTrading
The platform library (libbinance_c) ships inside the package. On a platform it does not support, the first client you create throws a BinanceLibraryException. The examples in this repository target net10.0 and need the .NET 10 SDK; the library itself runs on net8.0 and later.
Documentation
For detailed information, refer to the Binance API Documentation.
REST APIs
All REST API endpoints are available through the MarginTradingRestApi class. Some endpoints require authentication with your Binance API credentials.
using Binance.Common;
using Binance.MarginTrading.RestApi;
var config = new RestApiConfig
{
ApiKey = "your-api-key",
ApiSecret = "your-api-secret",
};
using var client = MarginTradingRestApi.Production(config);
var response = await client.CrossMarginCollateralRatioAsync();
Console.WriteLine(MarginTradingRestApiJson.ToJson(response.Data()));
Every endpoint is a method named after it (<Operation>Async) that takes an <Operation>Request when it has parameters. The response (RestApiResponse<T>) has Data(), which decodes the body, Status, RateLimits and Headers. Every client has a factory for each of its environments (Production, and Testnet / Demo where they exist) and FromConfig(config) for the endpoint of the configuration.
More examples can be found in the examples/Binance.MarginTrading.Examples folder.
Configuration Options
The REST API supports the following advanced configuration options through RestApiConfig:
Timeout: How long a request attempt may take as a whole (connecting, writing the request and reading the response), aTimeSpan(default: 1 s).Proxy: Proxy configuration (ProxyConfig):Host: Proxy server hostname.Port: Proxy server port.Protocol: Proxy protocol, aProxyProtocol(Http,Https,Socks5orSocks5h; the SOCKS5 protocols run over plain TCP,Socks5hlets the proxy resolve the host name).Username/Password: Proxy authentication credentials (Basic).
ProxyFromEnv: ReadHTTPS_PROXY,HTTP_PROXY,ALL_PROXYandNO_PROXYfrom the environment (default: true).Proxywins over the environment.KeepAlive: Enable HTTP keep-alive (default: true).Compression: Enable response compression (default: true).Retries: Total attempts for failed requests (default: 3).Backoff: Delay between retries, aTimeSpan; attemptnwaitsn * Backoff(default: 1 s).CustomHeaders: Headers added to every request.MaxResponseSize: Largest response body accepted, in bytes (default: 128 MiB).ApiKey/ApiSecret: API key and HMAC secret.PrivateKeyPem/PrivateKeyFile: RSA or Ed25519 private key for authentication, as PEM text or as the path of a PEM file.PrivateKeyPassphrase: Passphrase for the private key, if encrypted.CaFile/CaPem: PEM bundle (path or text) whose CAs are trusted in addition to the system store, for example a private CA.CaOnly: Trust onlyCaFile/CaPem, not the system store.ServerCertificateValidator: Called with each certificate of the chain, to pin a certificate or to take over the decision.ClientCertificatePem/ClientCertificateFilewithClientKeyPem/ClientKeyFileandClientKeyPassphrase: A client certificate for mutual TLS.TimeOffset/TimestampProvider: A fixed offset added to the host clock, or a function returning the time, for thetimestampof signed requests.
Timeout
You can configure a timeout for requests (a TimeSpan). If an attempt takes longer than the timeout (connecting, writing the request and reading the whole response), it is aborted with a BinanceTimeoutException.
See the Timeout example for a complete program.
Proxy
The REST API supports HTTP/HTTPS proxy configurations, with optional Basic authentication. Without Proxy, the proxy variables of the environment are used.
See the Proxy example for a complete program.
Custom CA Certificates and Certificate Pinning
To trust a private CA, set CaFile or CaPem (added to the system store; CaOnly = true trusts only them). To enhance security, you can pin a certificate with ServerCertificateValidator: it is called for every certificate of the chain with its depth (0 is the server certificate) and whether the built-in checks passed (preverified), and it returns true to accept the certificate. This ensures the client only communicates with servers using specific certificates. An exception thrown by the validator rejects the certificate.
See the Custom CA Certificates and Certificate Pinning example for a complete program.
Client Certificates and Timestamps
A client certificate (mutual TLS) is set with the certificate and its private key, as PEM text or files. TimestampProvider returns the time for the timestamp of signed requests instead of the host clock (or set TimeOffset, a fixed offset added to it).
See the Client Certificates and Timestamps example for a complete program.
Keep-Alive
Enable HTTP keep-alive for persistent connections (enabled by default).
See the Keep-Alive example for a complete program.
Compression
Enable or disable response compression (enabled by default).
See the Compression example for a complete program.
Retries
Configure the number of attempts and the delay between them (Backoff, a TimeSpan). GET and DELETE requests are retried after a network error or an HTTP 500, 502, 503 or 504. Every retry of a signed request is signed again.
See the Retries example for a complete program.
Custom Headers
Add headers to every request.
See the Custom Headers example for a complete program.
Key Pair Based Authentication
The REST API supports key pair-based authentication for secure communication. You can use RSA or Ed25519 keys for signing requests, given as PEM text (PrivateKeyPem) or as the path of a PEM file (PrivateKeyFile). Without a private key, ApiSecret signs with HMAC-SHA256.
See the Key Pair Based Authentication example for a complete program.
config.ToString() never prints the key, the secret, the private key or the passphrase.
Rate Limits
Every response carries the rate limit counters Binance reports, and the response headers.
using var client = MarginTradingRestApi.Production();
var response = await client.CrossMarginCollateralRatioAsync();
foreach (var limit in response.RateLimits)
Console.WriteLine(limit);
Endpoints Without a Method
SendRequestAsync and SendSignedRequestAsync call any endpoint, for example one added to the API after this release. The parameters are encoded, and the request signed, as for the other methods; the body comes back as raw JSON.
var config = new RestApiConfig
{
ApiKey = "your-api-key",
ApiSecret = "your-api-secret",
};
using var client = MarginTradingRestApi.Production(config);
var response = await client.SendSignedRequestAsync("/path/of/the/endpoint", HttpMethod.Get,
new Dictionary<string, object?> { ["name"] = "value", ["limit"] = 10 });
Console.WriteLine(response.Data());
Error Handling
The REST API provides detailed error types to help you handle issues effectively. Every error derives from BinanceException:
BinanceBadRequestException: Invalid request (HTTP 400).BinanceUnauthorizedException: Missing or invalid authentication credentials (HTTP 401).BinanceForbiddenException: Access to the requested resource is forbidden (HTTP 403).BinanceNotFoundException: Resource not found (HTTP 404).BinanceConflictException: The request conflicts with the current state of the resource (HTTP 409).BinanceRateLimitBanException: IP address banned for exceeding rate limits (HTTP 418).BinanceTooManyRequestsException: Rate limit exceeded (HTTP 429).BinanceServerException: Internal server error (HTTP 5xx).BinanceNetworkException: Issues with network connectivity;BinanceTimeoutExceptionis an attempt that took longer thanTimeout.BinanceClientException: General client error (unexpected status, malformed response).BinanceConfigException: Invalid configuration or request.BinanceCancelledException: The operation was cancelled (for example the client was disposed).
See the Error Handling example for a complete program.
BinanceException carries Kind, HttpStatus, Code (the Binance error code), RetryAfter (REST 418 and 429), RetryAt (WebSocket API), TransportCode, TransportCategory and RequestSent, for a REST error the response that caused it (Body, Headers and RateLimits), and for a WebSocket API error its WebSocketRateLimits and ErrorData (the error.data of the answer). Cancelling a CancellationToken cancels the request in flight and throws OperationCanceledException.
If you need another endpoint, create the client from a configuration: new MarginTradingRestApi(new RestApiConfig { BasePath = "https://..." }). The default is MarginTradingRestApi.ProductionUrl.
WebSocket Streams
WebSocket Streams provide real-time data feeds. Use the MarginTradingWebSocketStreams client: its ConnectAsync opens a connection and returns it once it is open, a MarginTradingWebSocketStreamsConnection with one method per stream. ConnectAsync(streams) subscribes to streams by name on open, and ConnectAsync(poolSize: n) opens a pool of n. Dispose the connection when done.
using Binance.Common;
using Binance.MarginTrading.RestApi;
using Binance.MarginTrading.WebSocketStreams;
using var rest = MarginTradingRestApi.Production(new RestApiConfig
{
ApiKey = "your-api-key",
ApiSecret = "your-api-secret",
});
// Keep the listen key open with KeepaliveUserDataStreamAsync at least every 60 minutes.
var listenKey = (await rest.StartUserDataStreamAsync()).Data()!.ListenKey!;
var client = MarginTradingWebSocketStreams.Production();
await using var connection = await client.ConnectAsync();
using var riskData = connection.RiskData(listenKey);
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
try
{
await foreach (var e in riskData.Events.ReadAllAsync(cts.Token))
Console.WriteLine(MarginTradingWebSocketStreamsJson.ToJson(e));
}
catch (OperationCanceledException)
{
}
Events of a subscription is a ChannelReader<T>. It completes when the subscription ends (the client is disconnected or disposed, or the subscription is disposed) and faults with a BinanceException when the server rejects the SUBSCRIBE, so await foreach throws it; a connection the client re-establishes does not end it. subscription.Completion and IsActive report the same. subscription.OnMessage(callback) delivers the events to callbacks instead (every callback gets every event, in order), and subscription.RunAsync(handler, ct) runs one handler over the reader.
A message that does not decode as the event type is skipped, not fatal: it is logged as a warning and counted in subscription.DecodeFailureCount; the stream goes on.
More examples are available in the examples/Binance.MarginTrading.Examples folder.
Configuration Options
The WebSocket Streams client supports the following advanced configuration options through WebSocketStreamsConfig:
ReconnectDelay: The delay between reconnection attempts, aTimeSpan(default: 5 s); after a failed attempt, a backoff of 1 s doubling up to 60 s, with random jitter, is added to it.ConnectTimeout: How long connecting may take, aTimeSpan(default: 10 s).Compression: Enable or disable compression for WebSocket messages (default: true).Mode:WebSocketMode.Single(the default) orWebSocketMode.Pool(n), a pool ofnconnections.MaxMessageSize: Largest message accepted, in bytes (default: 64 MiB).EventBufferCapacity: Capacity of the buffer of each subscription; when it is full the oldest event is dropped. Unbounded by default.Proxy/ProxyFromEnv: As for the REST API, butProxyFromEnvis false by default, as in the other Binance connectors.CaFile/CaPem/CaOnly/ServerCertificateValidator, the client certificate andTimeOffset/TimestampProvider: As for the REST API.
Reconnect Delay
Specify the delay between WebSocket reconnection attempts for streams (a TimeSpan); after a failed attempt, a backoff of 1 second doubling up to 60 seconds, with random jitter, is added to it. After a reconnect the client subscribes again to every stream.
See the Reconnect Delay example for a complete program.
Compression
Enable or disable compression for WebSocket Streams messages.
See the Compression example for a complete program.
Connection Mode
Choose between a single connection (WebSocketMode.Single, the default) and a pool of connections, in the configuration or for
one connection.
See the Connection Mode example for a complete program.
Custom CA Certificates and Certificate Pinning
To trust a private CA, set CaFile or CaPem (added to the system store; CaOnly = true trusts only them). To enhance security, you can pin a certificate with ServerCertificateValidator: it is called for every certificate of the chain with its depth (0 is the server certificate) and whether the built-in checks passed (preverified), and it returns true to accept the certificate. This ensures the client only communicates with servers using specific certificates. An exception thrown by the validator rejects the certificate.
See the Custom CA Certificates and Certificate Pinning example for a complete program.
Subscribe to User Data Streams
The account events arrive on user data streams, opened with a listen key. The listen key expires after 60 minutes without a keepalive; the subscriptions are restored after every reconnect.
using Binance.Common;
using Binance.MarginTrading.RestApi;
using Binance.MarginTrading.WebSocketStreams;
using var rest = MarginTradingRestApi.Production(new RestApiConfig
{
ApiKey = "your-api-key",
ApiSecret = "your-api-secret",
});
// Keep the listen key open with KeepaliveUserDataStreamAsync at least every 60 minutes.
var listenKey = (await rest.StartUserDataStreamAsync()).Data()!.ListenKey!;
var client = MarginTradingWebSocketStreams.Production();
await using var connection = await client.ConnectAsync();
using var riskData = connection.RiskData(listenKey);
using var tradeData = connection.TradeData(listenKey);
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
try
{
await Task.WhenAll(
riskData.RunAsync(e => { Console.WriteLine(MarginTradingWebSocketStreamsJson.ToJson(e)); return ValueTask.CompletedTask; }, cts.Token),
tradeData.RunAsync(e => { Console.WriteLine(MarginTradingWebSocketStreamsJson.ToJson(e)); return ValueTask.CompletedTask; }, cts.Token));
}
catch (OperationCanceledException)
{
}
Unsubscribing from Streams
You can unsubscribe from a stream with Unsubscribe() (or by disposing its subscription). This is useful for managing active subscriptions without closing the connection.
using Binance.Common;
using Binance.MarginTrading.RestApi;
using Binance.MarginTrading.WebSocketStreams;
using var rest = MarginTradingRestApi.Production(new RestApiConfig
{
ApiKey = "your-api-key",
ApiSecret = "your-api-secret",
});
// Keep the listen key open with KeepaliveUserDataStreamAsync at least every 60 minutes.
var listenKey = (await rest.StartUserDataStreamAsync()).Data()!.ListenKey!;
var client = MarginTradingWebSocketStreams.Production();
await using var connection = await client.ConnectAsync();
var subscription = connection.RiskData(listenKey);
await Task.Delay(TimeSpan.FromSeconds(10));
subscription.Unsubscribe();
Console.WriteLine("Unsubscribed");
PingServer() sends a WebSocket ping frame on every open connection, and connection.Events (or connection.OnEvent(callback)) carries the lifecycle events (open, close, error, ping, pong).
A subscription delivers its events either to its Events reader or to callbacks, like on('message', ...) of the other connectors: every callback added with OnMessage gets every event, in order. WaitSubscribedAsync() completes once the server has acknowledged the subscription, and fails with BinanceWebSocketResponseException if the server rejects it.
using var callback = subscription.OnMessage(e => Console.WriteLine(e));
await subscription.WaitSubscribedAsync();
Streams by Name
Subscribe("name-of-the-stream") takes any stream name and returns a subscription with its events as raw JSON. Subscribe([...]) and Unsubscribe([...]) subscribe and unsubscribe stream names without a subscription object, like subscribe and unsubscribe of the other connectors; their messages reach connection.EnableMessages() (a reader of every message as the server sent it) or connection.OnMessage(callback). IsSubscribed tells whether a stream is subscribed, and ConnectAsync(streams) subscribes streams through the connection URL.
var client = MarginTradingWebSocketStreams.Production();
await using var connection = await client.ConnectAsync();
using var subscription = connection.Subscribe("name-of-the-stream");
await subscription.WaitSubscribedAsync();
Console.WriteLine(connection.IsSubscribed("name-of-the-stream"));
await foreach (var e in subscription.Events.ReadAllAsync())
Console.WriteLine(e);
If you need another endpoint, set Url of the configuration: new MarginTradingWebSocketStreams(new WebSocketStreamsConfig { Url = "wss://..." }). The default is MarginTradingWebSocketStreams.ProductionUrl.
Automatic Connection Renewal
The WebSocket connection is automatically renewed for both WebSocket API and WebSocket Streams connections, before the 24 hours expiration of the connection, and when the server announces its shutdown. The subscriptions and the session logon are restored. This ensures continuous connectivity.
Logging
BinanceLogging.Configure(loggerFactory) sends the log of the connectors, which is process-wide, to Microsoft.Extensions.Logging, and BinanceLogging.SetLevel(BinanceLogLevel.Debug) sets the level. The optional Binance.Common.DependencyInjection package does it for you.
using Microsoft.Extensions.Logging;
using var loggerFactory = LoggerFactory.Create(b => b.AddConsole().SetMinimumLevel(LogLevel.Debug));
BinanceLogging.Configure(loggerFactory);
Testing
To run the tests of this connector:
dotnet test tests/Binance.MarginTrading.Tests
The tests run the connector against a loopback server with the example responses documented for the API. They cover every operation, WebSocket API request and stream, and the decoding of the models. They need the platform support described in Binance.Common/README.md.
Migration Guide
If you are moving from the 4.x connector (the single Binance.Spot 4.x package), refer to the Migration Guide for detailed steps.
Contributing
Contributions are welcome. Please open a GitHub issue to discuss your idea or report a bug before sending a pull request. The code of this connector (and its examples and tests) is maintained from the Binance API definitions, so changes to it are made there; edits made by hand in this folder would be overwritten. Make sure all tests pass if you contribute a change.
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.
Licence
This project is licensed under the MIT License. See the LICENCE file for details.