|
Binance C++ Connector 2.0.0
Official C++ connectors for the Binance APIs
|
Official collection of auto-generated C++ connectors for Binance APIs.
Every connector offers typed REST API, WebSocket API and WebSocket Streams clients, built on the shared binance::common library (signing, HTTP, WebSocket connection management, errors and utilities).
Before using the connectors, ensure you have:
Each connector is a CMake component and target. Build or install only the ones you need:
| Connector | Product | REST API | WebSocket API | WebSocket Streams |
|---|---|---|---|---|
| algo | Algo Trading | ✅ | ||
| alpha | Alpha | ✅ | ✅ | |
| c2c | C2C | ✅ | ||
| cms | CMS (Announcements) | ✅ | ||
| convert | Convert | ✅ | ||
| copy_trading | Copy Trading | ✅ | ||
| crypto_loan | Crypto Loan | ✅ | ||
| derivatives_trading_coin_futures | Derivatives Trading (COIN-M Futures) | ✅ | ✅ | ✅ |
| derivatives_trading_options | Derivatives Trading (Options) | ✅ | ✅ | |
| derivatives_trading_portfolio_margin | Derivatives Trading (Portfolio Margin) | ✅ | ✅ | |
| derivatives_trading_portfolio_margin_pro | Derivatives Trading (Portfolio Margin Pro) | ✅ | ✅ | |
| derivatives_trading_usds_futures | Derivatives Trading (USDS-M Futures) | ✅ | ✅ | ✅ |
| dual_investment | Dual Investment | ✅ | ||
| fiat | Fiat | ✅ | ||
| gift_card | Gift Card | ✅ | ||
| margin_trading | Margin Trading | ✅ | ✅ | |
| mining | Mining | ✅ | ||
| pay | Pay | ✅ | ||
| rebate | Rebate | ✅ | ||
| simple_earn | Simple Earn | ✅ | ||
| spot | Spot Trading | ✅ | ✅ | ✅ |
| staking | Staking | ✅ | ||
| stock_options | Stock Options | ✅ | ✅ | |
| stocks | Stocks | ✅ | ✅ | |
| sub_account | Sub Account | ✅ | ||
| vip_loan | VIP Loan | ✅ | ||
| w3w_prediction | W3W Prediction | ✅ | ✅ | |
| wallet | Wallet | ✅ |
The repository contains a vcpkg port in ports/binance-connector-cpp. Use it as an overlay port: it builds the checkout it is in, so check out the release you want first (git checkout v2.0.0). Every connector is a feature named like the product, with hyphens instead of underscores (vcpkg does not allow them): copy-trading builds the copy_trading component. Name the connectors you need (there is no default one: without features, only binance::common is built), or all for every connector:
In a vcpkg.json manifest, list the features you need: { "name": "binance-connector-cpp", "features": ["spot", "wallet"] }.
In manifest mode, pass --overlay-ports the same way or set VCPKG_OVERLAY_PORTS.
Build and install the connectors you need, for example spot (list several with semicolons, e.g. -DBINANCE_PRODUCTS="spot;wallet"; by default every connector under clients/ is built):
Then use them from your CMake project, with the same vcpkg toolchain:
Signed order on the Spot testnet, with the credentials read from the environment (order_test validates the order without placing it; use new_order to place it):
Market data stream (public, no credentials):
Instead of installing it, you can also build the SDK as part of your project with FetchContent (or add_subdirectory). Your project then provides the dependencies, e.g. by listing boost-asio, boost-beast, boost-json, openssl and zlib in its own vcpkg.json:
vcpkg is not required: Boost 1.86+ (Asio, Beast, JSON), OpenSSL 3 and zlib from your system package manager work too, e.g. cmake -S . -B build -DBINANCE_PRODUCTS=spot with libboost-all-dev libssl-dev zlib1g-dev on Ubuntu 25.10 (24.04 ships Boost 1.83, which is too old). An installed SDK requires at least the Boost version it was built with, and should be used with that same version, because its public headers include Boost.Asio templates.
<binance/spot/spot.hpp> includes every client of a connector; including only the module you use (rest_api.hpp, websocket_api.hpp or websocket_streams.hpp) keeps compile times down. Each module has factories for its environments: production, testnet and, where available, demo.
| CMake option | Default | Description |
|---|---|---|
| BINANCE_PRODUCTS | all | Connectors to build, e.g. spot;derivatives_trading_usds_futures ("" = common only) |
| BINANCE_BUILD_TESTS | OFF | Build the GoogleTest suites (ON in dev, ci, asan, tsan and release-tests) |
| BINANCE_BUILD_EXAMPLES | OFF | Build the examples (ON in dev, ci, asan and tsan) |
| BINANCE_WARNINGS_AS_ERRORS | OFF | Treat warnings as errors (ON in the dev, ci and release-tests presets) |
| BUILD_SHARED_LIBS | OFF | Build shared libraries |
| BINANCE_LTO | ON | Link-time optimisation of shared Release builds |
| BINANCE_BUILD_C_API | OFF | Build libbinance_c, the C interface for language bindings (see C API) |
| BINANCE_INSTALL | ON when top-level | Generate the install rules and the CMake package |
Release builds put every function in its own section so the linker can drop unused code: link your program with -Wl,-dead_strip (macOS) or -Wl,--gc-sections (Linux) to benefit from it. Shared libraries hide the symbols of the static dependencies they bundle on Linux; on macOS, use a dynamic vcpkg triplet (-DVCPKG_TARGET_TRIPLET=arm64-osx-dynamic) to keep them out of the library. On Windows, a DLL build must be used with the MSVC toolset and runtime (/MD or /MDd) it was built with, and binance::common sets _WIN32_WINNT=0x0A00 (Windows 10) for its consumers, since Asio's types depend on it.
-DBINANCE_BUILD_C_API=ON builds libbinance_c, a shared library with a plain C interface to binance::common, for bindings in other languages. The interface is declared and documented in c_api/include/binance/binance_c.h; the library exports only the bnc_* functions and bundles its dependencies.
Releases follow Semantic Versioning for the API: only a major release breaks code that builds against the SDK, and find_package accepts any release with the requested major version. The C++ ABI is kept only between patch releases: a minor release adds fields to the request and response structs whenever the Binance API adds parameters, which changes their layout. Shared libraries therefore carry <major>.<minor> as their SOVERSION (libbinance_spot.so.1.0), so that a program is never loaded with a library it was not built against: rebuild it for a new minor release. Static libraries, vcpkg and FetchContent build from source and are not affected. The stable binary interface is the C API: libbinance_c is versioned by its C ABI, and its SOVERSION is BNC_ABI_VERSION, which changes with every incompatible change to binance_c.h. Headers under binance/common/detail/ and names in detail namespaces are internal and may change in any release.
Besides the prerequisites, make format, make lint and make docs need clang-format, clang-tidy (with its run-clang-tidy script), Doxygen and jq:
The checks of make lint are listed one by one in .clang-tidy, so that a newer clang-tidy adds none by itself; it lints the library sources (common/src, clients/*/src).
The build is driven by the CMake presets:
| Preset | Build |
|---|---|
| dev | Debug, tests, examples, warnings as errors (pull requests) |
| release-tests | Release (optimised), tests, warnings as errors |
| asan, tsan | dev with the address / undefined or thread sanitizers |
| release | Release without tests, for installing |
| ci | RelWithDebInfo, tests (the full CI, started manually) |
make wraps the same commands, and make help shows its usage:
| Command | What it does |
|---|---|
| make test [PRODUCTS=spot] [PRESET=dev] [JOBS=8] | configure, build and test (JOBS applies to build and tests) |
| make example NAME=spot/websocket_streams/agg_trade | build and run one example |
| make format / make format-check | format the sources with clang-format / check the formatting |
| make lint / make lint-fix | run clang-tidy (fails on a warning) / apply its fixes |
| make asan | the tests under the address and undefined behaviour sanitizers |
| make install / make install-check | install into build/<preset>/install / build a consumer against it |
| make docs | generate the API reference with Doxygen |
| make clean | remove build/ |
Since this repository contains auto-generated code using OpenAPI Generator, we encourage you to:
Manual edits to the generated connectors under clients/ and examples/ will be overwritten.
This SDK is provided by Binance on an "as is" and "as available" basis for use at your own risk. Binance makes no representations or warranties of any kind, whether express or implied, as to the operation of the SDK, its accuracy, reliability, completeness, or fitness for any particular purpose.
To the fullest extent permitted by law, Binance shall not be liable for any losses, damages, or expenses of any kind arising from or in connection with your use of, or inability to use, this SDK, including but not limited to any financial losses resulting from errors, bugs, interruptions, or inaccuracies in the SDK.
Your use of this SDK to access the Binance Platform is subject to the Binance API Key Terms and the Binance Terms of Use, which shall prevail in the event of any conflict with this disclaimer. You are solely responsible for any orders or transactions executed through the Binance Platform using this SDK.
This SDK is not intended to constitute investment advice or a recommendation to buy, sell, or hold any digital asset. You should independently evaluate and verify all information before acting.
This project is licensed under the MIT License - see the LICENCE file for details.