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.
- 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
Add the following to your build.gradle.kts
:
repositories {
mavenCentral()
}
dependencies {
implementation("com.velkonost:binance-sdk:1.0.0")
}
- Initialize the SDK with your API credentials:
// Initialize the SDK
BinanceSDK.setup(
apiKey = "your_api_key",
apiSecret = "your_api_secret"
)
- Use the SDK to interact with the Binance API:
// Check API connectivity
BinanceSDK.General.ping()
// Get futures account balance
val balance = BinanceSDK.Futures.getBalance("USDT")
// Get open positions
val positions = BinanceSDK.Futures.getOpenPositions()
// Subscribe to real-time market data
BinanceSDK.Socket.startListenUpdates("BTCUSDT", KlineInterval.Minute1)
The SDK provides comprehensive support for futures trading operations:
// Get account balance for a specific asset
val balance = BinanceSDK.Futures.getBalance("USDT")
// Get all open positions
val positions = BinanceSDK.Futures.getOpenPositions()
// Get available trading symbols
val symbols = BinanceSDK.Futures.getSymbols()
// Get historical kline data
val klines = BinanceSDK.Futures.getKlines(
symbol = "BTCUSDT",
interval = KlineInterval.Minute1,
start = "4 day ago"
)
Real-time market data is available through WebSocket connections:
// Start listening to connection state
BinanceSDK.Socket.startListenConnections()
// Subscribe to updates for a specific symbol
BinanceSDK.Socket.startListenUpdates(
symbol = "BTCUSDT",
interval = KlineInterval.Minute1
)
// Subscribe to updates for all available symbols
BinanceSDK.Socket.startListenAllSymbolsUpdates(
delayBetweenLaunches = 1000L,
interval = KlineInterval.Minute1,
logConnectionsState = true
) { update ->
// Handle symbol update
println("Symbol: ${update.symbol}, Price: ${update.kline.close}")
}
// Stop listening to updates
BinanceSDK.Socket.stopListenUpdates("BTCUSDT")
The SDK provides comprehensive error handling:
try {
val balance = BinanceSDK.Futures.getBalance("USDT")
} catch (e: BinanceSDKException) {
when (e) {
is BinanceSDKNotInitializedException -> {
// SDK not initialized
}
else -> {
// Handle other SDK exceptions
}
}
}
The SDK provides strongly typed models for all API responses:
Kline
: Candlestick data with open, high, low, close prices and volumeFuturesBalance
: Account balance information for futures tradingFuturesOpenPosition
: Detailed information about open positionsSymbolUpdate
: Real-time market data updates
-
Initialization: Always initialize the SDK before use:
BinanceSDK.setup(apiKey, apiSecret)
-
Error Handling: Use try-catch blocks to handle potential exceptions:
try { // SDK operations } catch (e: BinanceSDKException) { // Handle exceptions }
-
WebSocket Management:
- Monitor connection state using
startListenConnections()
- Close unused connections with
stopListenUpdates()
- Use appropriate intervals for your use case
- Monitor connection state using
-
Rate Limiting: Be aware of Binance API rate limits and implement appropriate throttling
Contributions are welcome! Please feel free to submit a Pull Request.
For support, please open an issue in the GitHub repository.