Requires the keys:write scope. Creates an own API key and returns its secret once. Use Create a customer key for customer keys. Only send is available as a product. Product entries must be distinct. The name is trimmed and must contain 1 to 64 characters. Read product rules do not restrict SWQoS.
// src/bin/management_create_key.rs// Builds on the example project (/reference/json-rpc#example-project).use swqos_examples::swqos::{Result, required};use serde::Serialize;#[derive(Serialize)]struct KeyCreate { name: String, products: [&'static str; 1], expires_in_days: u16,}#[tokio::main]async fn main() -> Result<()> { let name = required("KEY_NAME")?.trim().to_owned(); if !(1..=64).contains(&name.chars().count()) { return Err("KEY_NAME must have 1 to 64 characters after trimming".into()); } let payload = KeyCreate { name, products: ["send"], expires_in_days: 30 }; let endpoint = reqwest::Url::parse("https://console.zerocopy.sh/api/v1/keys")?; let request = reqwest::Client::new() .post(endpoint) .bearer_auth(required("MANAGEMENT_API_KEY")?); // Reuse this key and the same body when retrying this creation attempt. let request = request.header("Idempotency-Key", required("KEY_REQUEST_ID")?); let request = request.json(&payload); let response = request.send().await?; let status = response.status(); let request_id = response.headers().get("Request-Id") .map(|value| value.to_str()).transpose()?.unwrap_or("<missing>").to_owned(); if let Some(etag) = response.headers().get("ETag") { eprintln!("ETag: {}", etag.to_str()?); } let body = response.text().await?; if !status.is_success() { return Err(format!("HTTP {status}; Request-Id: {request_id}; {body}").into()); } eprintln!("Request-Id: {request_id}"); println!("{body}"); Ok(())}