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, RestError, Result};
51#[cfg(feature = "websocket")]
52pub use error::WsError;
53pub use model::NumberString;
54#[cfg(feature = "reqwest")]
55pub use transport::ReqwestTransport;
56pub use transport::{Transport, TransportError};
57#[cfg(feature = "websocket")]
58pub use ws::{
59    Arg, OkxWs, OkxWsBuilder, Push, WsChannelConnectionCount, WsChannelGroup, WsConn, WsConnector,
60    WsEvent, WsFrame, WsNotice, WsOperation,
61};
62
63/// Global OKX REST API base URL.
64pub const GLOBAL_API_URL: &str = "https://www.okx.com";
65
66/// US and AU OKX REST API base URL.
67pub const US_API_URL: &str = "https://us.okx.com";
68
69/// EEA OKX REST API base URL.
70pub const EEA_API_URL: &str = "https://eea.okx.com";
71
72/// Default global OKX REST API base URL.
73///
74/// This alias is retained for compatibility. Use [`OkxRegion`] with
75/// [`OkxClientBuilder::region`] when building a client for a regional account.
76pub const API_URL: &str = GLOBAL_API_URL;
77
78/// OKX REST API region.
79///
80/// Regional accounts must use the matching API domain. US and AU users
81/// registered on `app.okx.com` should use [`OkxRegion::Us`]. EU users
82/// registered on `my.okx.com` should use [`OkxRegion::Eea`].
83#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
84#[non_exhaustive]
85pub enum OkxRegion {
86    /// Global OKX REST API domain.
87    Global,
88    /// US and AU OKX REST API domain.
89    Us,
90    /// EEA OKX REST API domain.
91    Eea,
92}
93
94impl OkxRegion {
95    /// Return the REST API base URL for this region.
96    pub const fn api_url(self) -> &'static str {
97        match self {
98            Self::Global => GLOBAL_API_URL,
99            Self::Us => US_API_URL,
100            Self::Eea => EEA_API_URL,
101        }
102    }
103}