A modern C++20 SDK for the Alpaca Trading, Market Data, Broker, and Auth APIs.
Contributions are welcome.
- Strongly typed request/response models for every endpoint.
- REST clients for Trading, Market Data, Broker, and Auth APIs.
- Real-time streaming:
- Trade/order updates via WebSocket.
- Market data (trades, quotes, bars) via WebSocket.
- Broker events via Server-Sent Events (SSE).
- Financial utility types:
Decimal— fixed-point precision (8 decimal places) for prices and quantities.DateTime— parsing/formatting for all Alpaca timestamp formats.
- Broker API with both legacy key/secret and OAuth2 client-credentials auth (automatic token refresh).
- 231 unit tests covering model parsing, serialization, and URL/body construction.
| Class | Description |
|---|---|
AlpacaTraderAPI |
Account, orders, positions, watchlists, activities, calendar/clock, options, treasuries, crypto wallets |
AlpacaMarketDataAPI |
Stocks, options, crypto, forex, fixed income, screener, news, logos, corporate actions |
AlpacaBrokerAPI |
Broker account lifecycle, account-scoped trading/funding, journals, reporting, rebalancing |
AlpacaAuthAPI |
OAuth2 token issuance (client credentials) |
AlpacaUpdatesStream |
Real-time trade/order updates (WebSocket) |
AlpacaMarketDataStream |
Real-time market data — trades, quotes, bars (WebSocket) |
BrokerSSEClient |
Broker event streams (SSE with auto-reconnect) |
- C++20 compiler (Clang 14+, GCC 12+, MSVC 2022+)
- CMake 3.16+
- OpenSSL
- Howard Hinnant date
- nlohmann/json (3.7.3+)
- IXWebSocket (fetched automatically if not found)
macOS (Homebrew):
brew install cmake openssl howard-hinnant-date nlohmann-jsonUbuntu / Debian:
sudo apt install cmake libssl-dev nlohmann-json3-devcmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build| CMake option | Default | Description |
|---|---|---|
ALPACA_BUILD_TESTS |
ON |
Build the GoogleTest suite |
ALPACA_BUILD_EXAMPLES |
OFF |
Build example programs |
cp .env.example .env
# Edit .env with your keys, then:
source .envAt minimum for Trading + Market Data:
export ALPACA_API_KEY="your-api-key"
export ALPACA_SECRET_KEY="your-secret-key"For the Broker API (choose one mode):
# Legacy key/secret auth
export ALPACA_BROKER_API_KEY="your-broker-key"
export ALPACA_BROKER_API_SECRET="your-broker-secret"
# Client credentials auth (recommended — automatic token refresh)
export ALPACA_BROKER_CLIENT_ID="your-client-id"
export ALPACA_BROKER_CLIENT_SECRET="your-client-secret"cmake -S . -B build -DALPACA_BUILD_EXAMPLES=ON
cmake --build build
./build/example/simple_example
./build/example/stock_data_example
./build/example/websocket_example
./build/example/broker_get_accounts_exampleSee example/ for the full list.
#include <cstdlib>
#include <iostream>
#include "alpaca/AlpacaTraderAPI.h"
int main() {
const char* key = std::getenv("ALPACA_API_KEY");
const char* secret = std::getenv("ALPACA_SECRET_KEY");
if (!key || !secret) return 1;
alpaca::AlpacaTraderAPI api(key, secret, alpaca::TraderAPIEndpoint::PAPER);
auto account = api.get_account();
std::cout << "Account: " << account.id << "\n";
}#include "alpaca/AlpacaMarketDataAPI.h"
int main() {
alpaca::AlpacaMarketDataAPI md(
std::getenv("ALPACA_API_KEY"),
std::getenv("ALPACA_SECRET_KEY"),
alpaca::MarketDataEndpoint::LIVE,
alpaca::DataFeed::IEX
);
auto latest = md.get_stock_latest_quotes({"AAPL", "MSFT"});
}Legacy auth:
#include "alpaca/AlpacaBrokerAPI.h"
alpaca::AlpacaBrokerAPI broker(
std::getenv("ALPACA_BROKER_API_KEY"),
std::getenv("ALPACA_BROKER_API_SECRET"),
alpaca::BrokerAPIEndpoint::SANDBOX
);
auto accounts = broker.get_accounts();Client-credentials auth (recommended):
#include "alpaca/AlpacaBrokerAPI.h"
alpaca::AlpacaBrokerAPI broker(
std::getenv("ALPACA_BROKER_CLIENT_ID"),
std::getenv("ALPACA_BROKER_CLIENT_SECRET"),
alpaca::BrokerAPIEndpoint::SANDBOX,
alpaca::BrokerAuthMode::CLIENT_CREDENTIALS
);
auto accounts = broker.get_accounts();In client-credentials mode, Bearer token retrieval and refresh are handled automatically inside the HTTP client. Use
AlpacaAuthAPIif you need explicit access to the token payload.
| Stream | Transport | Example |
|---|---|---|
| Trade/order updates | Binary WebSocket | websocket_example.cpp |
| Market data | JSON WebSocket | websocket_example.cpp |
| Broker events | SSE (auto-reconnect) | broker_sse_example.cpp |
Install to a prefix:
cmake --build build --target installConsume from another CMake project:
find_package(alpaca-cpp REQUIRED)
target_link_libraries(your_target PRIVATE alpaca::alpaca-cpp)cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
cmake --build build
cd build && ctest --output-on-failureRun a subset:
./build/test/alpaca-tests --gtest_filter="TraderAPIURLTest.*"include/alpaca/
AlpacaTraderAPI.h # Trading API client
AlpacaMarketDataAPI.h # Market Data API client
AlpacaBrokerAPI.h # Broker API client
AlpacaAuthAPI.h # Auth API client
AlpacaUpdatesStream.h # Trade updates WebSocket stream
AlpacaMarketDataStream.h # Market data WebSocket stream
BrokerSSEClient.h # Broker SSE event stream
api/rest/ # HTTP transport
api/websocket/ # WebSocket transport
core/ # Decimal, DateTime, enums, errors, helpers
model/ # All typed request/response models
trader/ # Trading models
data/ # Market data models
stream/ # Stream event models
broker/ # Broker models
src/ # Implementation files
example/ # Runnable examples
test/ # GoogleTest suites
cmake/ # CMake package config templates
401 Unauthorized — verify you are using the correct endpoint (paper / live / sandbox / production) for your credentials and that env vars are exported in the current shell.
find_package(alpaca-cpp) not found — install the library first (cmake --build build --target install) or point CMAKE_PREFIX_PATH to your install prefix.
Stream examples fail to connect — confirm network access and TLS trust store availability. For testing outside market hours, use the TEST feed in websocket_example.cpp.