| Crates.io | onise |
| lib.rs | onise |
| version | 1.1.0 |
| created_at | 2025-01-21 10:45:21.195699+00 |
| updated_at | 2025-01-22 13:21:07.042958+00 |
| description | An async client for Kraken's APIs in Rust. |
| homepage | |
| repository | https://github.com/myakeen/onise |
| max_upload_size | |
| id | 1524735 |
| size | 166,409 |
A comprehensive, typed, rate-limited, testable Rust client for:
This library provides:
Disclaimer
This is not an official Kraken product. Always consult the official docs and WebSocket v2 docs for the latest changes or usage policies.
GetWebSocketsToken from the REST API)In your Cargo.toml:
[dependencies]
onise = "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:
cargo build
to download and compile.
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 logiccargo run -- rest
KRAKEN_API_KEY and KRAKEN_API_SECRET from your environment if you want private endpoint callsget_server_time(), then calls get_balance()cargo run -- ws
WS_URL from environment (defaults to wss://ws.kraken.com/v2)KRAKEN_WS_TOKEN for private dataYou can customize or extend this logic in main.rs to handle more endpoints, advanced trading flows, or reconnection strategies.
REST endpoints are in a KrakenClient struct with methods for:
get_server_time, get_system_status, get_asset_info, get_ticker_information, etc.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 onise::KrakenClient;
use std::env;
#[tokio::main]
async fn main() {
let api_key = env::var("KRAKEN_API_KEY").ok();
let api_secret = env::var("KRAKEN_API_SECRET").ok();
// Create a rate-limited client
let client = KrakenClient::new(api_key, api_secret, None, 3, 2);
// Public call: get server time
match client.get_server_time().await {
Ok(time_resp) => println!("Server time: {:?}", time_resp),
Err(e) => eprintln!("Error: {}", e),
}
// Private call: get balance
match client.get_balance().await {
Ok(balance) => println!("Balance: {:?}", balance.balances),
Err(e) => eprintln!("Error fetching balance: {}", e),
}
}
Spot WebSocket API v2 is handled by a KrakenWsClient:
KrakenWsClient::connect("wss://ws.kraken.com/v2").await?ping, authorize, subscribe, add_order)Example (if you ran it in WebSocket mode):
use onise::ws_client::KrakenWsClient;
use onise::ws_models::WsSubscriptionPayload;
use std::env;
#[tokio::main]
async fn main() {
let url = env::var("WS_URL").unwrap_or("wss://ws.kraken.com/v2".to_string());
let token = env::var("KRAKEN_WS_TOKEN").ok();
let client = KrakenWsClient::connect(&url).await.expect("Failed to connect");
// Authorize if you have a token for private data/trading
if let Some(t) = token {
client.authorize(&t, Some(1)).await.expect("Auth failed");
}
// Send a ping
client.send_ping(Some(2)).await.expect("Ping failed");
// Subscribe to ticker updates for BTC/USD
client.subscribe(
WsSubscriptionPayload::Ticker { symbol: "XBT/USD".to_string() },
Some(3),
).await.expect("Subscribe failed");
println!("Connected to {url}, listening...");
loop {
// Just wait indefinitely to see inbound messages
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
}
}
export KRAKEN_API_KEY="..."
export KRAKEN_API_SECRET="..."
cargo test -- --nocapture
Local Integration Test:
tests/ws_integration_test.rs can spin up a local WebSocket server using tokio_tungsteniteKrakenWsClient connects to ws://127.0.0.1:some_port, sends a ping, you confirm on the server sideLive:
WS_URL="wss://ws.kraken.com/v2", optionally KRAKEN_WS_TOKEN if you want private streamscargo test -- --nocapture or a dedicated test verifying ping, subscribe, user trading, etc.subscribe/unsubscribe carefully in WebSocket usageeprintln! calls into structured logs (e.g. with [tracing] or [log]/[env_logger]) if you need advanced debuggingBy combining both REST and WebSocket modes in a single binary, you can select between them at runtime with:
# REST mode
cargo run -- rest
# WebSocket mode
cargo run -- ws
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!