BinanceAPI is a comprehensive Objective-C library for iOS and macOS. It can be used to interact with the various endpoints of the Binance API. Both synchronous and asynchronous requests are supported. A rudimentary WebSocket implementation for event streaming is also provided. BinanceAPI is available as a pod for easy installation via CocoaPods.
The Binance REST API is made up of four distinct endpoint types
This structure is reflected throughout the BinanceAPI library.
BinanceAPI's pod dependencies are included for convenience however, it is recommended that you set up your own CocoaPods environment by running:
$ (sudo) gem install cocoapodsand:
$ pod installto keep up to date with the latest supported pod versions.
For convenience, the examples provided are contained within an Xcode macOS unit test target. Simply run the test(s) to interact with the Binance API.
In order to be able to interact with certain Binance API endpoints, you will need to be in possession of both a valid API and secret key. These can be generated in the API section of your Binance account. Replace the YOUR-API-KEY and YOUR-SECRET-KEY placeholders where appropriate to successfully interact with the API. Ensure that the respective checkmarks for trading and/or withdrawal are enabled when interacting with certain Account endpoints.
The Binance API library contains three client classes:
BNBSynchronousRESTClient(blocking/not recommended)BNBAsynchronousRESTClient(non-blocking)BNBWebSocketClient(event streaming)
Instantiate the desired client with your API and secret key. Depending on the specific endpoint requirements specified at Binance API, it may be necessary to add further endpoint paths to the respective BNBHTTPSessionManager class's signing or API collections. Only when contained within one of these collections will endpoint security be handled automatically.
BNBAsynchronousRESTClient *client = [[BNBAsynchronousRESTClient alloc] initWithAPIKey:@"YOUR-API-KEY" secretKey:@"YOUR-SECRET-KEY"];
Alternatively, when dealing with an open endpoint you can simply instantiate the desired client as follows:
BNBAsynchronousRESTClient *client = [BNBAsynchronousRESTClient new];
With the client instantiated, you are ready to fire requests at the API.
[client testConnectivity:^(id _Nullable responseObject, NSError * _Nullable error){}];
[client checkServerTime:^(id _Nullable responseObject, NSError * _Nullable error){}];
[client orderBookForSymbol:@"LTCBTC"
limit:10
result:^(id _Nullable responseObject, NSError * _Nullable error){}];
[client aggregateTradesListForSymbol:@"LTCBTC"
fromId:NSNotFound
startTime:-1.0
endTime:-1.0
limit:10
result:^(id _Nullable responseObject, NSError * _Nullable error){}];
[client klineDataForSymbol:@"LTCBTC"
interval:FifteenMinutes
startTime:-1.0
endTime:-1.0
limit:10
result:^(id _Nullable responseObject, NSError * _Nullable error){}];
[client priceChangeStatisticsTickerForSymbol:@"LTCBTC"
interval:TwentyFourHours
result:^(id _Nullable responseObject, NSError * _Nullable error){}];
[client priceTickersResult:^(id _Nullable responseObject, NSError * _Nullable error){}];
[client orderBookTickersResult:^(id _Nullable responseObject, NSError * _Nullable error){}];
[client createOrderWithSymbol:@"LTCBTC"
side:Buy
type:Limit
timeInForce:GTC
quantity:1.0
icebergQuantity:0.0
price:0.005
stopPrice:0.0
newClientOrderId:nil
timestamp:[NSDate millisecondTimeIntervalSince1970]
timeToLive:5000
result:^(id _Nullable responseObject, NSError * _Nullable error){}];
[client queryOrderWithSymbol:@"LTCBTC"
orderId:0
originalClientOrderId:nil
timestamp:[NSDate millisecondTimeIntervalSince1970]
timeToLive:5000
result:^(id _Nullable responseObject, NSError * _Nullable error){}];
[client deleteOrderWithSymbol:@"LTCBTC"
orderId:0
originalClientOrderId:nil
newClientOrderId:nil
timestamp:[NSDate millisecondTimeIntervalSince1970]
timeToLive:5000
result:^(id _Nullable responseObject, NSError * _Nullable error){}];
[client openOrdersWithSymbol:@"LTCBTC"
timestamp:[NSDate millisecondTimeIntervalSince1970]
timeToLive:5000
result:^(id _Nullable responseObject, NSError * _Nullable error){}];
[client allOrdersWithSymbol:@"LTCBTC"
orderId:0
limit:10
timestamp:[NSDate millisecondTimeIntervalSince1970]
timeToLive:5000
result:^(id _Nullable responseObject, NSError * _Nullable error){}];
[client accountInformationWithTimestamp:[NSDate millisecondTimeIntervalSince1970]
timeToLive:5000
result:^(id _Nullable responseObject, NSError * _Nullable error){}];
[client tradesWithSymbol:@"LTCBTC"
fromId:NSNotFound
limit:10
timestamp:[NSDate millisecondTimeIntervalSince1970]
timeToLive:5000
result:^(id _Nullable responseObject, NSError * _Nullable error){}];
[client withdrawAsset:@"BTC"
address:@""
amount:0.0
name:nil
timestamp:[NSDate millisecondTimeIntervalSince1970]
timeToLive:5000
result:^(id _Nullable responseObject, NSError * _Nullable error){}];
[client depositHistoryForAsset:@"BTC"
depositStatus:NSNotFound
startTime:-1.0
endTime:-1.0
timestamp:[NSDate millisecondTimeIntervalSince1970]
timeToLive:5000
result:^(id _Nullable responseObject, NSError * _Nullable error){}];
[client withdrawHistoryForAsset:@"BTC"
withdrawStatus:NSNotFound
startTime:-1.0
endTime:-1.0
timestamp:[NSDate millisecondTimeIntervalSince1970]
timeToLive:5000
result:^(id _Nullable responseObject, NSError * _Nullable error){}];
[client createUserStream:^(id _Nullable responseObject, NSError * _Nullable error){}];
[client updateUserStreamForListenKey:@""
result:^(id _Nullable responseObject, NSError * _Nullable error){}];
[client deleteUserStreamForListenKey:@""
result:^(id _Nullable responseObject, NSError * _Nullable error){}];
BNBWebSocketClient *client = [BNBWebSocketClient webSocketClientWithURLString:@"wss://stream.binance.com:9443/ws/ltcbtc@depth"];
BNBWebSocketClient *client = [BNBWebSocketClient webSocketClientWithURLString:@"wss://stream.binance.com:9443/ws/ltcbtc@kline_15m"];
BNBWebSocketClient *client = [BNBWebSocketClient webSocketClientWithURLString:@"wss://stream.binance.com:9443/ws/ltcbtc@aggTrades"];
BNBWebSocketClient *client = [BNBWebSocketClient webSocketClientWithURLString:@"wss://stream.binance.com:9443/ws/listenKey"];
Please refer to the respective tests for a more in depth treatment of the Binance API endpoints.
- Refactoring
- Documenting/Commenting
- Misc improvements