Binance SDK
Unofficial Rust SDK for the Binance exchange API.
Disclaimer
Stability & Versioning Policy
No stability or backward-compatibility guarantees are provided.
Every version change must be treated as a breaking change, including minor and patch releases.
Users are strongly advised to pin an exact version, for example:
patisson-binance-sdk = "=0.1.8"
Maintenance Policy
This package is developed only in the author’s free time.
Releases are best-effort and not planned in advance.
The scope of the package is intentionally limited to the most commonly used functionality.
Features
- Spot Trading — public market data + signed trading via
PublicClient/PrivateClient - Margin Trading — cross-margin and isolated-margin trading, borrow/repay, user data stream
- Derivatives — USDⓈ-M and COIN-M Futures
- Wallet — coin/network metadata, deposits, withdrawals, account status
- Shared WebSocket driver (
binance::ws::Stream<C, M>) with protocol-level heartbeat, automatic reconnect with exponential back-off, and a connection-TTL refresh that pre-empts Binance's 24-hour disconnect - Typed error classification —
ErrorCodeis a transparent newtype with named constants and predicates (is_auth,is_rate_limited,is_transient, …) instead of a closed enum that goes stale every time Binance ships a new code - Builder pattern on every
*Request/*Paramstype with private fields, so new optional parameters can be added without breaking callers
Scope: every product module ships a representative slice of endpoints — enough to use the SDK and copy the pattern for the rest of Binance's API. Coverage is not exhaustive; extensions are straightforward to add following the existing layout.
Installation
[]
= "=0.1.6"
The package is
patisson-binance-sdk(Cargo.toml); the library you import isbinance:use PublicClient;
Quick start
Public REST — no API key
use ;
async
Private REST — signed
PrivateClient requires API key + secret. The timestamp and HMAC-SHA256
signature are appended automatically.
use ;
async
WebSocket — market data
The shared Stream<C, M> driver is generic over the outgoing command and
incoming message types — every product module plugs into it with its own
OutgoingMessage / IncomingMessage.
use ;
async
Error handling
API errors round-trip through ErrorCode, a transparent newtype around i64
with named constants and classification predicates — match by intent rather
than by remembering numbers.
use ;
match client.new_order.await
Available predicates: is_auth, is_invalid_timestamp, is_invalid_signature,
is_bad_api_key_format, is_api_key_rejected, is_wrong_permissions,
is_bad_request, is_rate_limited, is_server_error, is_transient,
is_order_rejected, is_no_such_order. Plus ErrorCode::raw() -> i64 as the
escape hatch for product-specific codes.
Modules
| Path | Purpose | HTTP | WS |
|---|---|---|---|
binance::spot |
Spot trading | PublicClient + PrivateClient |
market streams |
binance::margin |
Margin (cross + isolated) | PrivateClient only — use spot's PublicClient for market data |
user data stream (listenKey-based) |
binance::derivatives::usds_margined_futures |
USDⓈ-M Futures | PublicClient + PrivateClient |
market streams |
binance::derivatives::coin_margined_futures |
COIN-M Futures | PublicClient + PrivateClient |
market streams |
binance::wallet |
Deposits / withdrawals / account status | PrivateClient only |
— |
binance::ws |
Shared WebSocket driver (Stream<C, M>) |
— | — |
binance::ErrorCode |
Crate-wide error code newtype + predicates | — | — |
Examples
Runnable examples live in examples/, grouped by product prefix:
| Prefix | Products |
|---|---|
spot-* |
Spot |
usdm-* |
USDⓈ-M Futures |
coinm-* |
COIN-M Futures |
margin-* |
Margin |
wallet-* |
Wallet |
Run any of them with cargo:
Examples that hit private endpoints expect API_KEY and API_SECRET in the
environment. See examples/README.md for the full list.
Development
Quality-check hooks live in .githooks/ — install once per clone:
This wires core.hooksPath to .githooks/, which adds:
| Hook | Runs |
|---|---|
pre-commit |
cargo fmt --check, cargo check --all-targets |
pre-push |
cargo fmt --check, cargo clippy --all-targets -- -D warnings, cargo test --all-targets |
The pre-push hook is strict — any new clippy warning fails the push. Skip a
single run with --no-verify when you have a good reason (WIP, hotfix).
License
This project is licensed under the MIT license.