Skip to content


Switch branches/tags

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

Huobi C++ SDK v2

This is Huobi C++ SDK v2, you can import to your C++ project and use this SDK to query all market data, trading and manage your account. The SDK supports RESTful API invoking, and subscribing the market, account and order update from the WebSocket connection.

If you already use SDK v1, it is strongly suggested migrate to v2 as we refactor the implementation to make it simpler and easy to maintain. We will stop the maintenance of v1 in the near future. Please refer to the instruction on how to migrate v1 to v2 in section Migrate from v1

Table of Contents

Quick Start

After you install this SDK properly, you can follow below steps in your C++ project

  • Create the client instance.
  • Call the method provided by client.
// Create ReferenceClient instance and get the timestamp
ReferenceClient client;
cout << client.getTimestamp() << endl;

// Create MarketClient instance and get btcusdt latest 1-min candlestick
MarketClient client;
char *symbol = "btcusdt";
CandlestickRequest candlestickRequest;
candlestickRequest.symbol = symbol;
candlestickRequest.period = "1min";
vector<Candlestick> klines = client.getCandlestick(candlestickRequest);
for (Candlestick candlestick:klines) {
  cout << "open " << << endl;
  cout << "close " << candlestick.close << endl;

For how to install the SDK, please read the Usage section below.


Folder structure

This is the folder structure of SDK source code and the description

  • demo: The demo has main function, it provides the examples how to use client instance to access API and read response.
  • include: The header file of the implementation
    • client: The client class declaration, it is responsible to access data
    • rapidjson: The rapid json library
    • request: The request struct implementation
    • response: The response struct implementation
  • src: The core of the SDK
    • client: The client class implementation.


The SDK is compiled by C++ 11.

The compiler can be gccor clang.

Currently, The SDK has the compatibility on linux system (centos 7 and ubuntu 18.04) only.

Later, macOS and windows.

Install CMake

Please make sure the CMake is installed on your OS.

If not, you can follow to install it.

The minimum required version of CMake is 2.8, but we suggest to use the lastest CMake version.

Install 3rd party libraries

Please make sure the 3rd party libraries have been installed in your system. If not, please install them.

Build SDK

Refer to below steps to build the SDK library

$ git clone
$ cd huobi_Cpp
$ mkdir build
$ cd build
$ cmake .. -DCMAKE_PREFIX_PATH=/opt/cpprest-2.10.16/lib64/cmake/
 // You need to replace it with your own path
$ make
$ sudo make install

if you see the following error when executing "make" command

ld: library not found for -lssl

It indicates your openssl library was not installed expectedly, you can try below command to fix:

$ brew link openssl --force

Run examples

After installing the SDK, you are able to run the examples under /demo folder. if you want to access private data, you need below additional two steps:

  1. Create an API Key first from Huobi official website
  2. Assign your API access key and secret key in file /include/define.h as below:
#define APIKEY "hrf5gdfghe-e74bebd8-2f4a33bc-e7963"
#define SECRETKEY "fecbaab2-35befe7e-2ea695e8-67e56"

If you don't need to access private data, you can ignore the API key. Regarding the difference between public data and private data you can find details in Client section below.

There are different types of demo, refer below steps to run the 'market' demo

<In huobi_Cpp folder>
$ cd demo
$ cd market
$ mkdir build
$ cd build
$ cmake .. -DCMAKE_PREFIX_PATH=/opt/cpprest-2.10.16/lib64/cmake/
 // You need to replace it with your own path
$ make


In this SDK, the client is the struct to access the Huobi API. In order to isolate the private data with public data, and isolated different kind of data, the client category is designated to match the API category.

All the client is listed in below table. Each client is very small and simple, it is only responsible to operate its related data, you can pick up multiple clients to create your own application based on your business.

Data Category Client Privacy API Protocol
Reference ReferenceClient Public Rest
Market MarketClient Public Rest
Account AccountClient Private Rest
Wallet WalletClient Private Rest
Trade TradeClient Private Rest
IsolatedMargin IsolatedMarginClient Private Rest
CrossMargin CrossMarginClient Private Rest
WebSocketMarket WebSocketMarketClient Public WebSocket
WebSocketAsset WebSocketAssetClient Private WebSocket v2
WebSocketOrders WebSocketOrdersClient Private WebSocket v2

Public and Private

There are two types of privacy that is correspondent with privacy of API:

Public client: It invokes public API to get public data (Reference data and Market data), therefore you can create a new instance without applying an API Key.

// Create a ReferenceClient instance
ReferenceClient client;

// Create a MarketClient instance
MarketClient client;

Private client: It invokes private API to access private data, you need to follow the API document to apply an API Key first, and pass the API Key to the init function

// Create an AccountClient instance with APIKey
AccountClient accountClient{APIKEY, SECRETKEY};

// Create a TradeClient instance with API Key
TradeClient tradeClient{APIKEY, SECRETKEY};

The API key is used for authentication. If the authentication cannot pass, the invoking of private interface will fail.

Rest and WebSocket

There are two protocols of API, Rest and WebSocket

Rest: It invokes Rest API and get once-off response, it has two basic types of method: GET and POST

WebSocket: It establishes WebSocket connection with server and data will be pushed from server actively. There are two types of method for WebSocket client:

  • Request method: The method name starts with "req-", it will receive the once-off data after sending the request.
  • Subscription: The method name starts with "sub-", it will receive update after sending the subscription.

Migrate from v1

Why v2

The major difference between v1 and v2 is that the client category.

In SDK v1, the client is categorized as two protocol, request client and subscription client. For example, for Rest API, you can operate everything in request client. It is simple to choose which client you use, however, when you have a client instance, you will have dozens of method, and it is not easy to choose the proper method.

The thing is different in SDK v2, the client class is categorized as seven data categories, so that the responsibility for each client is clear. For example, if you only need to access market data, you can use MarketClient without applying API Key, and all the market data can be retrieved from MarketClient. If you want to operate your order, then you know you should use TradeClient and all the order related methods are there. Since the category is exactly same as the API document, so it is easy to find the relationship between API and SDK. In SDK v2, each client is smaller and simpler, which means it is easier to maintain and less bugs.

How to migrate

You don't need to change your business logic, what you need is to find the v1 request client and subscription client, and replace with the proper v2 client. The additional cost is that you need to have additional initialization for each v2 client.

Request example

Reference data

Exchange timestamp

ReferenceClient client;
cout << client.getTimestamp() << endl;


ReferenceClient client;
vector<Symbol> symbols = client.getSymbols();


ReferenceClient client;
vector<std::string> currencies = client.getCurrencies();

Currency & Chains

ReferenceClient client;
ReferenceCurrenciesRequest referenceCurrenciesRequest;
vector<ReferenceCurrencies> vec = client.getReferenceCurrencies(referenceCurrenciesRequest);

Market data


MarketClient client;
char *symbol = "btcusdt";
CandlestickRequest candlestickRequest;
candlestickRequest.symbol = symbol;
candlestickRequest.period = "1min";
vector<Candlestick> klines = client.getCandlestick(candlestickRequest);


MarketClient client;
DepthRequest depthRequest;
depthRequest.symbol = symbol;
Depth depth = client.getDepth(depthRequest);
cout << "ask price: " << depth.asks[0].price << endl;
cout << "bid price: " << depth.bids[0].price << endl;

Latest trade

MarketClient client;
TradeRequest tradeRequest{symbol};
vector<Trade> trade = client.getTrade(tradeRequest);

Historical Trade

MarketClient client;
HistoryTradeRequest historyTradeRequest;
historyTradeRequest.symbol = symbol;
vector<Trade> tradeHistory = client.getHistoryTrade(historyTradeRequest);
cout << "trade price: " << tradeHistory[0].price << endl;


Authentication is required.

Account balance

AccountClient accountClient{APIKEY, SECRETKEY};
vector<Balance> balanceVec = accountClient.getBalance(12345);

Get Account History

AccountClient accountClient{APIKEY, SECRETKEY};
AccountHistoryRequest accountHistoryRequest{12345, "usdt"};
accountHistoryRequest.transactTypes = "trade";
accountHistoryRequest.size = 10;
vector<AccountHistory> accountHistory = accountClient.getHistory(accountHistoryRequest);


Authentication is required.


WalletClient walletClient{APIKEY, SECRETKEY};
WithdrawCreateRequest withdrawCreateRequest;
withdrawCreateRequest.amount = "5";
withdrawCreateRequest.currency = "usdt";
withdrawCreateRequest.address = "xxxxx";
withdrawCreateRequest.chain = "trc20usdt";
withdrawCreateRequest.fee = "0.0";
long withdrawId = walletClient.withdrawCreate(withdrawCreateRequest);

Cancel withdraw

WalletClient walletClient{APIKEY, SECRETKEY};
long cancelwithdraw = walletClient.withdrawCancel(withdrawId);

Withdraw and deposit history

WalletClient walletClient{APIKEY, SECRETKEY};
QueryDepositWithdrawRequest queryDepositWithdrawRequest;
queryDepositWithdrawRequest.type = "withdraw";
vector<DepositWithdraw> record = walletClient.queryDepositWithdraw(queryDepositWithdrawRequest);
queryDepositWithdrawRequest.type = "deposit";
vector<DepositWithdraw> record = walletClient.queryDepositWithdraw(queryDepositWithdrawRequest);


Authentication is required.

Create order

TradeClient tradeClient{APIKEY, SECRETKEY};
PlaceOrderRequest placeOrderRequest;
placeOrderRequest.accountId = 12345;
placeOrderRequest.symbol = "htusdt";
placeOrderRequest.type = "buy-market";
placeOrderRequest.amount = "5.0";
placeOrderRequest.clientOrderId = "client_order-id";
long orderId = tradeClient.placeOrder(placeOrderRequest);

Cancel order

TradeClient tradeClient{APIKEY, SECRETKEY};

Cancel open orders

TradeClient tradeClient{APIKEY, SECRETKEY};
BatchCancelOpenOrdersRequest batchCancelOpenOrdersRequest;
batchCancelOpenOrdersRequest.accountId = accountId;
BatchCancelOpenOrders batchCancelOpenOrders = tradeClient.batchCancelOpenOrders(batchCancelOpenOrdersRequest);

Get order info

TradeClient tradeClient{APIKEY, SECRETKEY};
Order order = tradeClient.getOrder(orderId);

Historical orders

TradeClient tradeClient{APIKEY, SECRETKEY};
OrdersHistoryRequest ordersHistoryRequest;
std::vector<Order> historicalOrders = tradeClient.getOrdersHistory(ordersHistoryRequest);

Margin Loan

Authentication is required.

These are examples for cross margin

####Apply loan

CrossMarginClient crossMarginClient{APIKEY, SECRETKEY};
CrossMarginTransferOrApplyRequest crossMarginTransferOrApplyRequest{currency, amount};
long marginId = crossMarginClient.marginOrders(crossMarginTransferOrApplyRequest);

Repay loan

CrossMarginClient crossMarginClient{APIKEY, SECRETKEY};
std::string amount = "100.0";
crossMarginClient.repay(marginId, amount.c_str());

####Loan history

CrossMarginClient crossMarginClient{APIKEY, SECRETKEY};
CrossMarginLoanOrdersRequest crossMarginLoanOrdersRequest;
vector<CrossMarginLoanOrder> crossMarginLoanOrders = crossMarginClient.getLoanOrders(crossMarginLoanOrdersRequest);

Subscription example

Subscribe trade update

websocketMarketClient client;
client.subTrade("htusdt", [](Trade trade) {
  cout << trade.price << endl;
  cout << trade.tradeId << endl;

###Subscribe candlestick update

websocketMarketClient client;
client.subKline("htusdt", "1min", [](Candlestick candlestick) {
  cout << candlestick.amount << endl;

Subscribe order update

Authentication is required.

websocketOrdersClient client{APIKEY, SECRETKEY};
client.subOrders("htusdt", [](OrdersUpdate ordersUpdate) {
  cout << ordersUpdate.symbol << endl;

Subscribe account change

Authentication is required.

websocketAssetClient client{APIKEY, SECRETKEY};
client.subAccounts(1, [](AccountsUpdate accountsUpdate) {
  cout << accountsUpdate.changeType << endl;