Skip to main content

rust_okx/
lib.rs

1//! Async Rust client for the [OKX v5 REST API](https://www.okx.com/docs-v5/en/).
2//!
3//! The crate is built around three layers that map cleanly onto the OKX request
4//! lifecycle:
5//!
6//! 1. **Request building** — typed request/response models live under [`api`].
7//! 2. **Authentication** — credentials and HMAC-SHA256 request signing.
8//! 3. **Transport** — sending raw HTTP. The [`Transport`] trait abstracts this
9//!    so the default [`ReqwestTransport`] can be swapped for a custom or mock
10//!    implementation without changing any calling code.
11//!
12//! # Example
13//!
14//! ```no_run
15//! # #[cfg(feature = "reqwest")]
16//! # async fn run() -> Result<(), rust_okx::Error> {
17//! use rust_okx::{Credentials, OkxClient};
18//!
19//! // Public, unauthenticated client.
20//! let client = OkxClient::builder().build();
21//! let ticker = client.market().get_ticker("BTC-USDT").await?;
22//! println!("last price: {}", ticker[0].last.as_str());
23//!
24//! // Authenticated client.
25//! let creds = Credentials::new("key", "secret", "passphrase");
26//! let client = OkxClient::builder().credentials(creds).build();
27//! let balance = client.account().get_balance(None).await?;
28//! # Ok(())
29//! # }
30//! # #[cfg(not(feature = "reqwest"))]
31//! # fn run() {}
32//! ```
33#![warn(missing_docs)]
34#![warn(clippy::all)]
35
36pub mod api;
37mod client;
38mod credentials;
39mod error;
40pub mod model;
41mod signing;
42#[cfg(test)]
43mod test_util;
44pub mod transport;
45#[cfg(feature = "websocket")]
46pub mod ws;
47
48pub use client::{OkxClient, OkxClientBuilder};
49pub use credentials::Credentials;
50pub use error::Error;
51pub use model::NumberString;
52#[cfg(feature = "reqwest")]
53pub use transport::ReqwestTransport;
54pub use transport::{Transport, TransportError};
55#[cfg(feature = "websocket")]
56pub use ws::{Arg, OkxWs, OkxWsBuilder, WsChannelGroup, WsConn, WsConnector, WsEvent, WsFrame};
57
58/// Global OKX REST API base URL.
59pub const GLOBAL_API_URL: &str = "https://www.okx.com";
60
61/// US and AU OKX REST API base URL.
62pub const US_API_URL: &str = "https://us.okx.com";
63
64/// EEA OKX REST API base URL.
65pub const EEA_API_URL: &str = "https://eea.okx.com";
66
67/// Default global OKX REST API base URL.
68///
69/// This alias is retained for compatibility. Use [`OkxRegion`] with
70/// [`OkxClientBuilder::region`] when building a client for a regional account.
71pub const API_URL: &str = GLOBAL_API_URL;
72
73/// OKX REST API region.
74///
75/// Regional accounts must use the matching API domain. US and AU users
76/// registered on `app.okx.com` should use [`OkxRegion::Us`]. EU users
77/// registered on `my.okx.com` should use [`OkxRegion::Eea`].
78#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
79#[non_exhaustive]
80pub enum OkxRegion {
81    /// Global OKX REST API domain.
82    Global,
83    /// US and AU OKX REST API domain.
84    Us,
85    /// EEA OKX REST API domain.
86    Eea,
87}
88
89impl OkxRegion {
90    /// Return the REST API base URL for this region.
91    pub const fn api_url(self) -> &'static str {
92        match self {
93            Self::Global => GLOBAL_API_URL,
94            Self::Us => US_API_URL,
95            Self::Eea => EEA_API_URL,
96        }
97    }
98}