Blockcc-java-api is a lightweight Java library for interacting with the Blockcc API, providing complete API coverage, and supporting synchronous requests, as well as event streaming using WebSockets.
JDK 1.8+
简体中文 |English
-
EXTENSIVE DATA
Our wide range of market data includes cryptocurrency trade data, order book data, blockchain data and historical data. We ensure the integrity and accuracy of our data through rigorous processes.
-
HIGH-PERFORMANCE INFRASTRUCTURE
Our infrastructure is running on multiple servers across several data centres to ensure the fastest data delivery and the lowest latency possible. We have redundant hosting and load-balanced environments for maximum reliability.
-
SECURE ACCESS
Our API endpoints are encrypted through SSL so user traffic is secured when requesting data. To ensure maximum security, we also sign the API requests and require registration and the generation of an API Key.
-
Install library into your Maven's local repository by running
mvn install
-
Add the following Maven dependency to your project's
pom.xml
:<dependency> <groupId>cc.block.data</groupId> <artifactId>blockcc-api-client</artifactId> <version>1.3.2</version> </dependency>
Alternatively, you can clone this repository and run the examples.
There main client classes that can be used to interact with the API:
BlockccApiRestClient
, a synchronous/blocking Blockcc API client;
These can be instantiated through the corresponding factory method
of BlockccApiClientFactory
, by passing the API-KEY
, which can be created
at https://data.block.cc/account/dashboard. Edit TestConstants.java and
replace your api_key in order to test.
public static final String API_KEY="REPLACE_BY_YOUR_API_KEY";
Once the client is instantiated, it is possible to start making requests to the API.
boolean hasNextPage=true;
TickerParam tickerParams=TickerParam.builder().slug("bitcoin").size(100).build();
while(hasNextPage){
BlockccResponse<List<Ticker>>tickerResponse = client.getTickers(tickerParams);
hasNextPage = tickerResponse.hasNextPage();
for(Ticker ticker:tickerResponse.getContent()){
System.out.println(ticker);
}
tickerParams.nextPage();
}
MarketParam marketParams = MarketParam.builder().build();
for (Market market : client.getMarkets(marketParams).getContent()) {
System.out.println(market);
}
// need MarketPair desc
OrderBookParam orderBookParams = OrderBookParam.builder()
.desc("gateio_BTC_USDT")
.build();
System.out.println(client.getOrderBook(orderBookParams));
PriceParam priceParams = PriceParam.builder().build();
// Get Price
for (Price price : client.getPrices(priceParams).getContent()) {
System.out.println(price);
}
// Get History Price
HistoricalPriceParam historicalPriceParams = HistoricalPriceParam.builder()
.slug("ethereum")
.build();
BlockccResponse<List<HistoricalPrice>> historicalPrices = client.getHistoricalPrice(historicalPriceParams);
for (HistoricalPrice historicalPrice : historicalPrices.getContent()) {
System.out.println(historicalPrice);
}
SymbolParam symbolParams = SymbolParam.builder()
.details(false)
.build();
for (Symbol symbol : client.getSymbols(symbolParams).getContent()) {
System.out.println(symbol);
}
// MarketPair desc is required
KlineParam klineParams = KlineParam.builder()
.interval(Interval.ONE_DAY)
.desc("gate-io_BTC_USDT")
.build();
for (Kline kline : client.getKline(klineParams).getContent()) {
System.out.println(kline);
}
BlockccApiWebSocketClient webSocketClient = BlockccApiClientFactory
.newInstance("YOU_API_KEY").newWebSocketClient();
// 1.build topic list
List<String> list = new ArrayList<>();
list.add(Topic.builder()
.type(TopicType.ticker)
.desc("uniswap_BTC_ETH")
.build()
.toTopicString());
// 2.connect
webSocketClient.getTickers(System.out::println,
InputMessage.builder()
.operation(Operation.subscribe)
.args(list)
.build().toMessageString());
}
List<String> priceArgs = new ArrayList<>();
// add Topic Message
priceArgs.add(Topic.builder()
.type(TopicType.price)
.desc("bitcoin")
.build()
.toTopicString());
priceArgs.add(Topic.builder()
.type(TopicType.price)
.desc("uniswap")
.build().toTopicString());
// get Price connetion
webSocketClient.getPrices(System.out::println,
InputMessage.builder()
.operation(Operation.subscribe)
.args(priceArgs)
.build()
.toMessageString());
List<String> orderBookArgs = new ArrayList<>();
// add OrderBook subcribe message list
orderBookArgs.add(Topic.builder()
.type(TopicType.orderbook)
.desc("gate-io_BTC_USDT").build()
.toTopicString());
orderBookArgs.add(Topic.builder()
.type(TopicType.orderbook)
.desc("binance_BNB_USDT")
.build()
.toTopicString());
orderBookArgs.add(Topic.builder()
.type(TopicType.orderbook)
.desc("huobipro_HT_USDT")
.build()
.toTopicString());
// get connection
client.getOrderBooks(System.out::println, InputMessage.builder()
.operation(Operation.subscribe)
.args(orderBookArgs)
.build()
.toMessageString());
Each of the methods on BlockccApiWebSocketClient, which opens a new web socket, also returns a Closeable
.
This Closeable
can be used to close the underlying web socket and free any associated resources, e.g.
Closable ws = client.getConnection(someCallback,"msg");
// some time later...
ws.close();
An extensive set of examples, covering most aspects of the API, can be found at https://github.com/blockcc/blockcc-api-client-java/tree/master/src/test/java/cc/block/data/api/examples.