polyte_clob/ws/
auth.rs

1//! API credentials for WebSocket authentication.
2
3use std::fmt;
4
5use serde::{Deserialize, Serialize};
6
7/// API credentials for WebSocket user channel authentication.
8///
9/// These credentials can be obtained from your Polymarket account settings
10/// or derived using the CLOB API.
11///
12/// # Example
13///
14/// ```
15/// use polyte_clob::ws::ApiCredentials;
16///
17/// let creds = ApiCredentials::new("api_key", "api_secret", "passphrase");
18/// ```
19#[derive(Clone, Serialize, Deserialize)]
20pub struct ApiCredentials {
21    /// API key
22    #[serde(rename = "apiKey")]
23    pub api_key: String,
24    /// API secret
25    pub secret: String,
26    /// API passphrase
27    pub passphrase: String,
28}
29
30impl ApiCredentials {
31    /// Create new API credentials.
32    pub fn new(
33        api_key: impl Into<String>,
34        secret: impl Into<String>,
35        passphrase: impl Into<String>,
36    ) -> Self {
37        Self {
38            api_key: api_key.into(),
39            secret: secret.into(),
40            passphrase: passphrase.into(),
41        }
42    }
43
44    /// Load credentials from environment variables.
45    ///
46    /// Reads:
47    /// - `POLYMARKET_API_KEY`
48    /// - `POLYMARKET_API_SECRET`
49    /// - `POLYMARKET_API_PASSPHRASE`
50    pub fn from_env() -> Result<Self, std::env::VarError> {
51        Ok(Self {
52            api_key: std::env::var("POLYMARKET_API_KEY")?,
53            secret: std::env::var("POLYMARKET_API_SECRET")?,
54            passphrase: std::env::var("POLYMARKET_API_PASSPHRASE")?,
55        })
56    }
57}
58
59impl fmt::Debug for ApiCredentials {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        f.debug_struct("ApiCredentials")
62            .field("api_key", &"<redacted>")
63            .field("secret", &"<redacted>")
64            .field("passphrase", &"<redacted>")
65            .finish()
66    }
67}