|
kalshi-cpp 0.6.2
C++23 client for Kalshi's Predictions API
|
An unofficial C++23 client for Kalshi's Predictions API. It covers every REST operation and WebSocket channel. A generator builds it from Kalshi's OpenAPI and AsyncAPI documents in spec/, so type and field names match Kalshi's API docs. Kalshi's separate Margin and Perpetuals API is out of scope. The API reference lists every type and method.
You need a C++23 compiler, CMake 3.31+, OpenSSL 3, libcurl, and libwebsockets. CI tests GCC 13 on Ubuntu 24.04, Apple Clang on Apple silicon, and MSVC on Windows, where vcpkg.json supplies the dependencies. On macOS, the deployment target must be 13.3 or later.
Add the library to your CMake project:
After cmake --install, find_package(kalshi CONFIG REQUIRED) provides the same target. Until 1.0, a minor release may break the API. CHANGELOG.md has migration notes.
Public endpoints need no key.
kalshi::collect_pages follows cursors when you want every page.
Calls that can fail return kalshi::Result<T>, which is std::expected<T, kalshi::Error>, instead of throwing. Error::code classifies the failure, such as RateLimited or NetworkError. When Kalshi rejects a request, Error::http_status and Error::api_code hold its HTTP status and error code.
Create an API key at https://kalshi.com/account/profile. Save the private key file and note the key ID. Signer loads unencrypted RSA and Ed25519 PEM keys.
Kalshi's demo exchange uses play money and its own keys. To use it, pass kalshi::ClientConfig::for_environment(kalshi::Environment::Demo) to HttpClient after the signer. kalshi::WsConfig::for_environment does the same for WebSocketClient.
This places a real order unless client points at the demo exchange.
Counts and prices are fixed-point strings, not doubles. The client checks them and the required fields before sending, so order.price = "56c" fails locally with InvalidRequest.
connect() waits until the connection opens or fails. Callbacks then run on the client's network thread until you call disconnect() or destroy ws, so keep your program running. After a dropped connection, the client reconnects, resubscribes, and keeps your ws::Subscription handles valid. When it sees a skipped order book sequence number, it reports the gap to on_error and requests fresh snapshots. docs/channels.md lists each channel's message types.
Both are transports you stack under the client:
By default, RetryingTransport repeats a POST, PUT, or DELETE only after a 429, so it never places an order twice. RateLimitConfig{} matches Kalshi's Basic tier. To match your account's limits, pass the results of get_account_api_limits() and get_account_endpoint_costs() to kalshi::rate_limit_config().
examples/ has four programs: public market data, your portfolio, an order placed and canceled on the demo exchange, and a live order book. From a clone, make run-market_data runs the first one without a key. For the others, make run-<name> loads your key from .env, as the examples README shows.
CONTRIBUTING.md covers building from source, tests, code generation, and releases. Report security issues as SECURITY.md describes.