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:
[]
= "0.1.0"
# or
# onise = { git = "https://github.com/yourorg/onise-rs.git", branch = "main" }
(Adjust the version or Git URL to match your repository or crates.io version.)
Then run:
to download and compile.
Usage Modes (REST vs. WebSocket)
Our main.rs supports two modes: REST and WebSocket, selected by a command-line argument:
cargo run -- rest— Runs the REST client logiccargo run -- ws— Runs the WebSocket client logic- Omit or use another argument to default to REST
Example: Running the REST client
- Reads
KRAKEN_API_KEYandKRAKEN_API_SECRETfrom your environment if you want private endpoint calls - Calls
get_server_time(), then callsget_balance()
Example: Running the WebSocket client
- Reads
WS_URLfrom environment (defaults towss://ws.kraken.com/v2) - Optionally reads
KRAKEN_WS_TOKENfor private data - Connects, sends a ping, subscribes to a Ticker channel, and loops indefinitely to process incoming messages
You can customize or extend this logic in main.rs to handle more endpoints, advanced trading flows, or reconnection strategies.
REST Usage (API Details)
REST endpoints are in a KrakenClient struct 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 (how the code might look if you ran it solely in REST mode):
use KrakenClient;
use env;
async
WebSocket Usage (API Details)
Spot WebSocket API v2 is handled by a KrakenWsClient:
- Connect with
KrakenWsClient::connect("wss://ws.kraken.com/v2").await? - Send typed requests (e.g.,
ping,authorize,subscribe,add_order) - Automatically spawns a read loop to process messages like Ticker updates or ExecutionReports
Example (if you ran it in WebSocket mode):
use KrakenWsClient;
use WsSubscriptionPayload;
use env;
async
Testing & Integration
REST Testing
- Unit tests: Each REST endpoint can have a unit test with mock responses (via [wiremock] or similar)
- Live integration: Provide real credentials:
WebSocket Testing
-
Local Integration Test:
- A file like
tests/ws_integration_test.rscan spin up a local WebSocket server usingtokio_tungstenite - The
KrakenWsClientconnects tows://127.0.0.1:some_port, sends a ping, you confirm on the server side
- A file like
-
Live:
- Set
WS_URL="wss://ws.kraken.com/v2", optionallyKRAKEN_WS_TOKENif you want private streams - Run
cargo test -- --nocaptureor a dedicated test verifying ping, subscribe, user trading, etc.
- Set
Production Considerations
- Secrets: Do not commit your API key/secret to version control. Use environment variables or a secure vault
- Rate-Limiting: Adjust token-bucket quotas for REST usage; handle
subscribe/unsubscribecarefully in WebSocket usage - Reconnection: For WebSocket, handle reconnection if the socket closes unexpectedly. The example does not show automatic reconnection logic
- Logging: Convert simple
eprintln!calls into structured logs (e.g. with [tracing] or [log]/[env_logger]) if you need advanced debugging
Final Notes
By combining both REST and WebSocket modes in a single binary, you can select between them at runtime with:
# REST mode
# WebSocket mode
Either way, Onise offers a robust, typed interface to Kraken's Spot REST and Spot WebSocket API v2—no stubs, with typed requests and responses for all major endpoints. Enjoy building your Kraken-based applications in Rust!