rust-okx
Async Rust client for the OKX v5 REST API with typed models, pluggable transport, and demo trading support.
Status: early / in development.
rust-okxis currently a0.1.xcrate. The core REST client and high-frequency REST endpoints are implemented and tested, but full OKX API coverage is still expanding. Expect occasional breaking changes before1.0.
Why rust-okx?
- Typed REST API. Endpoints are grouped as
market,public_data,account, andtrade, with typed request builders and response models. - Pluggable transport.
OkxClient<T>is generic over a smallTransporttrait. You can use the defaultreqwesttransport or provide your own mock, retrying client, recorder, or test transport. - No async trait boxing. The public transport trait uses return-position
impl Future, so there is noasync_traitdependency and no requiredBox<dyn Trait>dispatch. - Lossless numeric values. OKX sends prices, sizes, and balances as JSON
strings.
NumberStringpreserves the exact wire value and lets callers decide whether to parse intof64,Decimal, or a domain type. - Match-friendly errors. Errors are exposed as a flat enum with transport, encoding, decoding, OKX API, HTTP status, and configuration cases.
- Demo trading support.
OkxClientBuilder::demo_trading(true)sends thex-simulated-trading: 1header required by OKX demo trading.
Installation
Add the crate and an async runtime to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["rt-multi-thread", "macros"] }
By default, rust-okx enables the built-in reqwest transport. Disable default
features when you want to provide a custom transport:
[]
= { = "0.1", = false }
Quick start
Public market data
Public market data does not require credentials:
use OkxClient;
async
Authenticated requests
Authenticated endpoints require an API key, secret, and passphrase:
use ;
async
Demo trading
OKX uses separate API keys for live and demo trading. A live key cannot be used against the demo environment, and a demo key cannot be used against live trading.
use ;
let credentials = new;
let client = builder
.credentials
.demo_trading
.build;
Regional accounts
The default client uses the global OKX API domain. Regional accounts must use
the matching domain. US and AU users should select OkxRegion::Us; EU users
should select OkxRegion::Eea.
If you are not sure which domain applies to your account, check the OKX account site where you registered and the matching official regional API documentation.
use ;
let client = builder
.region
.build;
API coverage
Endpoints are reached through accessors on OkxClient.
| Accessor | Status | Coverage |
|---|---|---|
client.market() |
Implemented | Tickers, order books, candlesticks, trades, index data, exchange rate, platform volume. |
client.public_data() |
Implemented | Instruments, system time, open interest, funding rate, price limit, mark price, delivery history, position tiers, insurance fund. |
client.account() |
Implemented | Balance, positions, config, bills, leverage, fees, risk state, simulated margin, position builder, borrowing and account settings. |
client.trade() |
Implemented | Place, cancel, amend, close positions, open orders, order history, fills, batch order flows. |
| WebSocket | Not implemented | Planned as a separate public/private streaming API. |
| Funding / Asset | Not implemented | Planned for balances, transfers, deposits, withdrawals, bills, and asset valuation. |
| SubAccount | Not implemented | Planned for sub-account queries, transfers, API keys, and VIP loan allocation. |
See TODO.md for the detailed roadmap.
Design notes
Transport
The transport layer sends a fully built http::Request<bytes::Bytes> and
returns a raw http::Response<bytes::Bytes>. Authentication, signing, endpoint
paths, query serialization, JSON encoding, and OKX response envelopes stay in
the client.
use Bytes;
use ;
;
let client = with_transport.build;
Numeric precision
OKX encodes many numeric values as strings. NumberString keeps the original
string and provides as_str(), parse::<T>(), and, with the rust-decimal
feature, to_decimal().
Error handling
The crate exposes a matchable Error enum:
TransportEncodeDecodeApi { code, message }HttpStatus { status, body }Configuration
Feature flags
All feature flags are additive.
| Feature | Default | Effect |
|---|---|---|
reqwest |
Yes | Enables the built-in ReqwestTransport. |
rust-decimal |
No | Adds NumberString::to_decimal(). |
Testing
The test suite is designed to be useful without network access or credentials.
- Unit tests under
src/**use an offline mock transport. They assert request method, path, query, body, signing headers, response parsing, enum compatibility, and error mapping. - Public integration tests can query public OKX market data.
- Authenticated integration tests load credentials from environment variables and skip automatically when the variables are missing.
- Live account tests are read-only.
- Order placement and lifecycle checks run only against demo trading.
Environment variables:
# Live account, read-only tests
OKX_API_KEY=...
OKX_API_SECRET=...
OKX_PASSPHRASE=...
# Demo trading tests
OKX_DEMO_API_KEY=...
OKX_DEMO_API_SECRET=...
OKX_DEMO_PASSPHRASE=...
You may place these values in a .env file at the repository root. The tests
load it automatically.
Useful commands:
MSRV and features
- Minimum supported Rust version:
1.85 - Edition:
2024 - Default feature:
reqwest - Optional feature:
rust-decimal no_std: not supported
Roadmap
The next large areas are WebSocket, Funding / Asset, SubAccount, advanced trade APIs, and finance modules. See TODO.md for the current backlog.
Disclaimer
This is an unofficial OKX client and is not affiliated with OKX. Trading involves financial risk. Test your flows against the demo environment before using live credentials.
License
Licensed under MIT OR Apache-2.0.