A C++ SDK for interacting with the Backpack Exchange WebSocket API. Provides real-time market data streaming and account management.
- Real-time market data streaming
- Ticker updates
- Order book (depth) updates
- Trade updates
- Candlestick data (multiple timeframes)
- Private data streaming (with authentication)
- Order updates
- Position updates
- Balance updates
- User trade updates
- Robust WebSocket connection handling
- Automatic reconnection
- Heartbeat monitoring
- Error handling
- Modern C++ design
- Type-safe enums for channels and order types
- RAII principles
- Exception safety
- Thread safety
- C++17 or later
- CMake 3.12 or later
- OpenSSL
- nlohmann/json
- WebSocket++
- Boost (for WebSocket++)
- Clone the repository:
git clone https://github.com/tribecajack/backpack-cpp-sdk.git
cd backpack-cpp-sdk- Create build directory and build:
mkdir build
cd build
cmake ..
make#include <backpack/backpack_client.hpp>
#include <iostream>
int main() {
// Create WebSocket client
backpack::BackpackWebSocketClient client("wss://ws.backpack.exchange");
// Connect to WebSocket server
if (!client.connect()) {
std::cerr << "Failed to connect" << std::endl;
return 1;
}
// Optional: Set API credentials for authenticated endpoints
const char* api_key = std::getenv("BACKPACK_API_KEY");
const char* api_secret = std::getenv("BACKPACK_API_SECRET");
if (api_key && api_secret) {
client.set_credentials(api_key, api_secret);
}
// Subscribe to channels
const std::string symbol = "SOL-USDC";
client.subscribe(backpack::Channel::TICKER, symbol);
client.subscribe(backpack::Channel::TRADES, symbol);
client.subscribe(backpack::Channel::DEPTH, symbol);
client.subscribe(backpack::Channel::CANDLES_1M, symbol);
// Register callback for market data
client.register_general_callback([](const nlohmann::json& msg) {
std::cout << "Received: " << msg.dump(2) << std::endl;
});
// Keep connection alive
while (client.is_connected()) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
return 0;
}cd build
cmake ..
make
./websocket_exampleTICKER: Real-time price and 24h statisticsTRADES: Real-time trade execution dataDEPTH: Real-time order book updatesCANDLES_1M: 1-minute candlestick dataCANDLES_5M: 5-minute candlestick dataCANDLES_15M: 15-minute candlestick dataCANDLES_1H: 1-hour candlestick dataCANDLES_4H: 4-hour candlestick dataCANDLES_1D: 1-day candlestick data
USER_ORDERS: Personal order updatesUSER_TRADES: Personal trade updatesUSER_POSITIONS: Position updatesUSER_BALANCES: Balance updates
To use authenticated endpoints, set your API credentials:
- As environment variables:
export BACKPACK_API_KEY="your_api_key"
export BACKPACK_API_SECRET="your_api_secret"- Or programmatically:
client.set_credentials("your_api_key", "your_api_secret");{
"stream": "ticker.SOL_USDC",
"data": {
"e": "ticker",
"s": "SOL_USDC",
"c": "124.50",
"h": "130.30",
"l": "124.19",
"v": "260656",
"n": 324595
}
}{
"stream": "depth.SOL_USDC",
"data": {
"e": "depth",
"s": "SOL_USDC",
"U": 2060508621,
"u": 2060508621,
"b": [["124.20", "6.05"]],
"a": []
}
}The SDK uses exceptions for error handling. Main exception types:
BackpackError: Base exception classConnectionError: WebSocket connection issuesAuthenticationError: Invalid credentialsSubscriptionError: Channel subscription failures
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
This is an unofficial SDK and is not affiliated with Backpack Exchange. Use at your own risk.