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, api::market::InstIdRequest};
18//!
19//! // Public, unauthenticated client.
20//! let client = OkxClient::builder().build();
21//! let ticker = client
22//!     .market()
23//!     .get_ticker(&InstIdRequest {
24//!         inst_id: "BTC-USDT",
25//!     })
26//!     .await?;
27//! println!("last price: {}", ticker[0].last.as_str());
28//!
29//! // Authenticated client.
30//! let creds = Credentials::new("key", "secret", "passphrase");
31//! let client = OkxClient::builder().credentials(creds).build();
32//! let balance = client.account().get_balance(rust_okx::api::account::BalanceRequest::default()).await?;
33//! # Ok(())
34//! # }
35//! # #[cfg(not(feature = "reqwest"))]
36//! # fn run() {}
37//! ```
38#![warn(missing_docs)]
39#![warn(clippy::all)]
40
41pub mod api;
42mod client;
43mod credentials;
44mod error;
45pub mod model;
46mod signing;
47#[cfg(test)]
48mod test_util;
49pub mod transport;
50#[cfg(feature = "websocket")]
51pub mod ws;
52
53pub use client::{OkxClient, OkxClientBuilder};
54pub use credentials::Credentials;
55#[cfg(feature = "websocket")]
56pub use error::WsError;
57pub use error::{Error, RestError, Result};
58pub use model::NumberString;
59#[cfg(feature = "reqwest")]
60pub use transport::ReqwestTransport;
61pub use transport::{Transport, TransportError};
62#[cfg(feature = "websocket")]
63pub use ws::{
64    Arg, OkxWs, OkxWsBuilder, Push, WsChannelConnectionCount, WsChannelGroup, WsConn, WsConnector,
65    WsEvent, WsFrame, WsNotice, WsOperation,
66};
67
68/// Global OKX REST API base URL.
69pub const GLOBAL_API_URL: &str = "https://www.okx.com";
70
71/// US and AU OKX REST API base URL.
72pub const US_API_URL: &str = "https://us.okx.com";
73
74/// EEA OKX REST API base URL.
75pub const EEA_API_URL: &str = "https://eea.okx.com";
76
77/// Default global OKX REST API base URL.
78///
79/// This alias is retained for compatibility. Use [`OkxRegion`] with
80/// [`OkxClientBuilder::region`] when building a client for a regional account.
81pub const API_URL: &str = GLOBAL_API_URL;
82
83/// OKX REST API region.
84///
85/// Regional accounts must use the matching API domain. US and AU users
86/// registered on `app.okx.com` should use [`OkxRegion::Us`]. EU users
87/// registered on `my.okx.com` should use [`OkxRegion::Eea`].
88#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
89#[non_exhaustive]
90pub enum OkxRegion {
91    /// Global OKX REST API domain.
92    Global,
93    /// US and AU OKX REST API domain.
94    Us,
95    /// EEA OKX REST API domain.
96    Eea,
97}
98
99impl OkxRegion {
100    /// Return the REST API base URL for this region.
101    pub const fn api_url(self) -> &'static str {
102        match self {
103            Self::Global => GLOBAL_API_URL,
104            Self::Us => US_API_URL,
105            Self::Eea => EEA_API_URL,
106        }
107    }
108}