Skip to main content

quantum_sdk/
keys.rs

1use serde::{Deserialize, Serialize};
2
3use crate::client::Client;
4use crate::error::Result;
5
6/// Request body for creating an API key.
7#[derive(Debug, Clone, Serialize, Default)]
8pub struct CreateKeyRequest {
9    /// Human-readable name for the key.
10    pub name: String,
11
12    /// Restrict to specific endpoints (e.g. ["chat", "images"]).
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub endpoints: Option<Vec<String>>,
15
16    /// Maximum spend in USD before the key is disabled.
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub spend_cap_usd: Option<f64>,
19
20    /// Rate limit in requests per minute.
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub rate_limit: Option<i32>,
23}
24
25/// Details about an API key (returned on creation and listing).
26#[derive(Debug, Clone, Deserialize)]
27pub struct KeyDetails {
28    /// Unique key identifier.
29    pub id: String,
30
31    /// Human-readable name.
32    pub name: String,
33
34    /// First characters of the key for identification.
35    pub key_prefix: String,
36
37    /// Scope restrictions.
38    #[serde(default)]
39    pub scope: Option<serde_json::Value>,
40
41    /// Amount spent by this key in ticks.
42    #[serde(default)]
43    pub spent_ticks: i64,
44
45    /// Whether the key has been revoked.
46    #[serde(default)]
47    pub revoked: bool,
48
49    /// Creation timestamp (RFC 3339).
50    #[serde(default)]
51    pub created_at: Option<String>,
52
53    /// Last usage timestamp (RFC 3339). Only present in list responses.
54    #[serde(default)]
55    pub last_used: Option<String>,
56}
57
58/// Response from creating an API key.
59#[derive(Debug, Clone, Deserialize)]
60pub struct CreateKeyResponse {
61    /// The full API key (only shown once on creation).
62    pub key: String,
63
64    /// Key metadata.
65    pub details: KeyDetails,
66}
67
68/// Response from listing API keys.
69#[derive(Debug, Clone, Deserialize)]
70pub struct ListKeysResponse {
71    /// All keys for the account.
72    pub keys: Vec<KeyDetails>,
73}
74
75/// Generic status response for operations that return a simple status.
76#[derive(Debug, Clone, Deserialize)]
77pub struct StatusResponse {
78    /// Status message (e.g. "ok", "deleted", "revoked").
79    pub status: String,
80
81    /// Optional human-readable message.
82    #[serde(default)]
83    pub message: Option<String>,
84}
85
86impl Client {
87    /// Creates a new API key with optional scope and spend restrictions.
88    pub async fn create_key(&self, req: &CreateKeyRequest) -> Result<CreateKeyResponse> {
89        let (resp, _meta) = self
90            .post_json::<CreateKeyRequest, CreateKeyResponse>("/qai/v1/keys", req)
91            .await?;
92        Ok(resp)
93    }
94
95    /// Lists all API keys for the account.
96    pub async fn list_keys(&self) -> Result<ListKeysResponse> {
97        let (resp, _meta) = self.get_json::<ListKeysResponse>("/qai/v1/keys").await?;
98        Ok(resp)
99    }
100
101    /// Revokes an API key by its ID.
102    pub async fn revoke_key(&self, id: &str) -> Result<StatusResponse> {
103        let path = format!("/qai/v1/keys/{id}");
104        let (resp, _meta) = self.delete_json::<StatusResponse>(&path).await?;
105        Ok(resp)
106    }
107}