Onise, Kraken Client for Rust
A comprehensive, typed, rate-limited, testable Rust client for:
- Kraken's Spot REST API (all public and private endpoints).
- Kraken's Spot WebSocket API v2 (market data, user data, and user trading).
This library provides:
- Strongly typed request/response models for Kraken's endpoints.
- Advanced error handling that parses Kraken's known error codes.
- Configurable rate limiting (token-bucket or others) to avoid hitting limits.
- Integration tests (mocked and local) for both REST and WebSocket.
- WebSocket support with a split read/write approach, typed subscription/unsubscription, user trading, etc.
Disclaimer
This is not an official Kraken product. Always consult the official docs and WebSocket v2 docs for the latest changes or usage policies.
Features
- Complete REST coverage: All documented endpoints (public & private).
- Spot WebSocket API v2 coverage: Market Data (Ticker, Book, Candles, Trades, Instruments), User Data (Executions, Balances), User Trading (Add/Amend/Edit/Cancel, etc.).
- Fully typed models: no placeholders or stubs for request/response fields.
- Rate limiting: A token-bucket approach (via [governor] or similar) can be configured.
- Integration tests: Local mocking for the WebSocket, real environment tests for REST (if you provide credentials).
Requirements
- Rust (edition 2021 or later).
- Cargo for dependency management.
- An Internet connection (for real calls to Kraken).
- Kraken API Key & Secret if you need private endpoints (REST) or user trading/ private data over WebSocket.
- Optionally, a WebSocket token for private data/trading (obtained via
GetWebSocketsTokenfrom the REST API).
Installation
In your Cargo.toml:
[]
= { = "https://github.com/yourorg/kraken-client-rs.git", = "main" }
(Adjust the Git URL or version to match your fork/repo.)
Then cargo build to download and compile.
REST Usage
REST endpoints are in a KrakenClient struct (example name) with methods for:
- Public:
get_server_time,get_system_status,get_asset_info,get_ticker_information, etc. - Private:
get_balance,get_trade_balance,get_open_orders,add_order, etc.
Example snippet (in main.rs or anywhere):
use KrakenClient;
use env;
async
WebSocket Usage
Spot WebSocket API v2 is covered by a KrakenWsClient:
- Connect with
KrakenWsClient::connect("wss://ws.kraken.com/v2").await?. - Split internally into read (stream) and write (sink), spawning a read loop.
- Send typed requests like
ping,authorize,subscribe,add_order. - Receive typed responses automatically in the background task, printing or logging them.
Example in main.rs:
use KrakenWsClient;
use WsSubscriptionPayload;
use env;
async
Key Points:
- The read loop is automatically spawned, so inbound messages (like ticker updates, user trading confirmations) are continuously processed.
- No
clone()errors—split approach is used internally. - Use the provided typed request methods (
add_order,amend_order,cancel_order, etc.) for user trading flows.
Testing & Integration
REST Testing
- Unit tests: Each REST endpoint can have a unit test with mock responses.
- Live integration: Export your real
KRAKEN_API_KEYandKRAKEN_API_SECRET, then call the endpoints.
WebSocket Testing
-
Local Integration Test:
- A file like
tests/ws_integration_test.rscan spin up a local WebSocket server (usingtokio_tungstenite), accept one client, read a message, and close. - The
KrakenWsClientconnects tows://127.0.0.1:XXXXfor offline test.
- A file like
-
Live:
- Set
WS_URL="wss://ws.kraken.com/v2", optionallyKRAKEN_WS_TOKENif you want private streams. cargo test -- --nocaptureor run a dedicated test that verifies you can ping, subscribe, etc.
- Set
Example local test snippet:
// tests/ws_integration_test.rs
async
Production Considerations
- Secrets: Do not commit your API key/secret to source control. Use environment variables or a secure vault.
- Rate-Limiting: Adjust the token-bucket quotas for REST usage. For the WebSocket, handle subscribe/unsubscribe calls carefully.
- Reconnection: If the WebSocket closes or errors, you may want to automatically reconnect and resubscribe. The example does not show an automatic reconnection logic.
- Logging: Expand upon the
eprintln!calls to a structured logger if you need advanced observability.
Final Notes
With this library, you can fully interact with Kraken's Spot REST and Spot WebSocket API v2 in Rust, using typed models for all endpoints, no stubs. Feel free to open issues or PRs to keep it updated as Kraken evolves. Happy trading!