The OKX API SDK for Rust provides developers with a powerful toolkit for integrating with OKX's cryptocurrency exchange platform. This open-source solution offers full support for OKX's V5 API, enabling seamless interaction with both REST and WebSocket endpoints.
Key Features
- Dual API Support: Access both REST API and WebSocket API endpoints
- Type-Safe Architecture: Complete Rust type definitions with robust error handling
- Modular Design: Organized codebase following Rust's best practices
Comprehensive Coverage: Support for all major API endpoints including:
- Account management
- Trading operations
- Market data
- Asset tracking
- Security Automation: Built-in authentication and request signing
- Connection Reliability: Automatic reconnection and heartbeat mechanisms for WebSocket
- Flexible Execution: Both synchronous and asynchronous calling patterns
Installation Guide
Add the SDK to your project via Cargo.toml:
[dependencies]
okx = "0.1.3"For latest development version:
cargo add --git https://github.com/fairwic/okx_rs๐ Explore advanced trading strategies with OKX
Getting Started
API Configuration
Set up your credentials through environment variables or .env file:
OKX_API_KEY=your_api_key
OKX_API_SECRET=your_api_secret
OKX_PASSPHRASE=your_passphrase
OKX_SIMULATED_TRADING=0 # Set to 1 for simulated tradingREST API Implementation
use okx::{create_client, Error};
use okx::api::market::MarketApi;
#[tokio::main]
async fn main() -> Result<(), Error> {
env_logger::init();
let credentials = Credentials::from_env().unwrap();
let client: OkxClient = OkxClient::new(credentials).unwrap();
let market = OkxMarket::new(client.clone());
let ticker = market.get_ticker("BTC-USDT-SWAP").await?;
println!("BTC-USDT Market Data: {:?}", ticker);
Ok(())
}WebSocket Integration
#[tokio::main]
async fn main() -> Result<(), Error> {
env_logger::init();
let args = Args::new().with_inst_id("BTC-USDT".to_string());
let mut client = OkxWebsocketClient::new_public();
let mut rx = client.connect().await.unwrap();
client.subscribe(ChannelType::Tickers, args).await.unwrap();
tokio::spawn(async move {
while let Some(msg) = rx.recv().await {
println!("Received public channel update: {:?}", msg);
}
});
sleep(Duration::from_secs(100)).await;
Ok(())
}Architectural Overview
src/
โโโ api/
โ โโโ account/ # Account operations
โ โโโ asset/ # Balance inquiries
โ โโโ market/ # Price data
โ โโโ trade/ # Order management
โโโ models/ # Data structures
โโโ client.rs # HTTP client
โโโ config.rs # Runtime configuration
โโโ error.rs # Error handlingAdvanced Configuration
Customize SDK behavior through programmatic configuration:
use okx::config::{Config, Credentials};
let config = Config::default()
.with_api_url("https://www.okx.com")
.with_simulated_trading(true);
let credentials = Credentials::new(
"your_api_key",
"your_api_secret",
"your_passphrase",
"0" // Trading mode flag
);๐ Discover OKX's API documentation
Development Workflow
# Clone repository
git clone https://github.com/fairwic/okx_rs.git
cd okx_rs
# Run tests
cargo test
# Execute examples
cargo run --example market
cargo run --example websocketFrequently Asked Questions
What's the difference between REST and WebSocket APIs?
The REST API follows request-response pattern for one-time data retrieval, while WebSocket maintains persistent connections for real-time updates.
How do I handle rate limits?
The SDK automatically respects OKX's rate limits with built-in request queuing and retry logic.
Is simulated trading supported?
Yes, set OKX_SIMULATED_TRADING=1 to use paper trading without real funds.
What Rust versions are supported?
The SDK maintains compatibility with Rust 1.60+ stable releases.
How secure are the API credentials?
Credentials are never logged and only used for request signing. We recommend using limited-permission API keys.
Can I contribute to the project?
Absolutely! The project welcomes contributions through GitHub pull requests following MIT license terms.
License
This project is licensed under MIT - see the LICENSE file for details.