Binance SDK
A Kotlin Multiplatform library for interacting with the Binance cryptocurrency exchange API. This SDK provides a clean and type-safe interface for both REST and WebSocket operations, supporting spot and futures trading.
Features
- Multiplatform Support: Works on all platforms supported by Kotlin Multiplatform (JVM, Native, JS)
- Futures Trading: Full support for Binance Futures API, including:
- Position management
- Balance queries
- Real-time market data
- Historical kline data
- WebSocket Integration: Real-time market data streams with:
- Automatic reconnection
- Connection state management
- Efficient subscription handling
- Type Safety: Strongly typed models and responses
- Coroutines Support: Asynchronous operations using Kotlin Coroutines
- Flow API: Reactive streams for real-time data using Kotlin Flow
- Error Handling: Comprehensive error handling with custom exceptions
Installation
Add the following to your build.gradle.kts:
repositories {
mavenCentral()
}
dependencies {
implementation("com.velkonost:binance-sdk:1.0.0")
}
Quick Start
- Initialize the SDK with your API credentials:
BinanceSDK.setup(
apiKey = "your_api_key",
apiSecret = "your_api_secret"
)
- Use the SDK to interact with the Binance API:
BinanceSDK.General.ping()
val balance = BinanceSDK.Futures.getBalance("USDT")
val positions = BinanceSDK.Futures.getOpenPositions()
BinanceSDK.Socket.startListenUpdates("BTCUSDT", KlineInterval.Minute1)
Key Features in Detail
Futures Trading
The SDK provides comprehensive support for futures trading operations:
val balance = BinanceSDK.Futures.getBalance("USDT")
val positions = BinanceSDK.Futures.getOpenPositions()
val symbols = BinanceSDK.Futures.getSymbols()
val klines = BinanceSDK.Futures.getKlines(
symbol = "BTCUSDT",
interval = KlineInterval.Minute1,
start = "4 day ago"
)
WebSocket Operations
Real-time market data is available through WebSocket connections:
BinanceSDK.Socket.startListenConnections()
BinanceSDK.Socket.startListenUpdates(
symbol = "BTCUSDT",
interval = KlineInterval.Minute1
)
BinanceSDK.Socket.startListenAllSymbolsUpdates(
delayBetweenLaunches = 1000L,
interval = KlineInterval.Minute1,
logConnectionsState = true
) { update ->
println("Symbol: ${update.symbol}, Price: ${update.kline.close}")
}
BinanceSDK.Socket.stopListenUpdates()
Error Handling
The SDK provides comprehensive error handling:
try {
val balance = BinanceSDK.Futures.getBalance("USDT")
} catch (e: BinanceSDKException) {
when (e) {
is BinanceSDKNotInitializedException -> {
}
else -> {
}
}
}
Data Models
The SDK provides strongly typed models for all API responses:
Kline: Candlestick data with open, high, low, close prices and volume
FuturesBalance: Account balance information for futures trading
FuturesOpenPosition: Detailed information about open positions
SymbolUpdate: Real-time market data updates
Best Practices
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
For support, please open an issue in the GitHub repository.