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