pub struct XaiClient { /* private fields */ }Expand description
The main xAI API client.
Use this client to interact with all xAI API endpoints.
§Example
use xai_rust::XaiClient;
// Create from API key
let client = XaiClient::new("your-api-key")?;
// Or use the builder
let client = XaiClient::builder()
.api_key("your-api-key")
.timeout_secs(300)
.build()?;
// Or load from XAI_API_KEY environment variable
let client = XaiClient::from_env()?;Implementations§
Source§impl XaiClient
impl XaiClient
Sourcepub fn new(api_key: impl Into<String>) -> Result<Self>
pub fn new(api_key: impl Into<String>) -> Result<Self>
Create a new client with the given API key.
Uses default configuration (global endpoint, 120s timeout).
§Errors
Returns an error if the API key contains invalid header characters or the HTTP client cannot be created.
Sourcepub fn from_env() -> Result<Self>
pub fn from_env() -> Result<Self>
Create a new client from the XAI_API_KEY environment variable.
Sourcepub fn builder() -> XaiClientBuilder
pub fn builder() -> XaiClientBuilder
Create a new client builder.
Sourcepub fn sync(&self) -> Result<SyncXaiClient>
pub fn sync(&self) -> Result<SyncXaiClient>
Create a blocking/sync facade from this async client.
The sync facade is intended for non-async applications and should not be used from inside an active async runtime.
Sourcepub fn into_sync(self) -> Result<SyncXaiClient>
pub fn into_sync(self) -> Result<SyncXaiClient>
Convert this async client into a blocking/sync facade.
The sync facade is intended for non-async applications and should not be used from inside an active async runtime.
Sourcepub fn with_config(config: ClientConfig) -> Result<Self>
pub fn with_config(config: ClientConfig) -> Result<Self>
Create a client with the given configuration.
Sourcepub fn responses(&self) -> ResponsesApi
pub fn responses(&self) -> ResponsesApi
Access the Responses API.
The Responses API is the primary endpoint for chat interactions.
§Example
use xai_rust::{XaiClient, Role};
let client = XaiClient::from_env()?;
let response = client.responses()
.create("grok-4")
.message(Role::User, "Hello!")
.send()
.await?;Sourcepub fn auth(&self) -> AuthApi
pub fn auth(&self) -> AuthApi
Access the Auth API.
§Example
use xai_rust::XaiClient;
let client = XaiClient::from_env()?;
let key = client.auth().api_key().await?;
println!("Authenticated as: {}", key.api_key);Sourcepub fn chat(&self) -> ChatApi
pub fn chat(&self) -> ChatApi
Access the Chat Completions API (legacy).
Note: The Chat Completions API is deprecated. Use responses() instead.
Sourcepub fn images(&self) -> ImagesApi
pub fn images(&self) -> ImagesApi
Access the Images API.
§Example
use xai_rust::XaiClient;
let client = XaiClient::from_env()?;
let response = client.images()
.generate("grok-2-image", "A cat in a tree")
.send()
.await?;Sourcepub fn videos(&self) -> VideosApi
pub fn videos(&self) -> VideosApi
Access the Videos API.
§Example
use xai_rust::XaiClient;
let client = XaiClient::from_env()?;
let video = client.videos().get("video-123").await?;
println!("Video: {:?}", video.id);Sourcepub fn files(&self) -> FilesApi
pub fn files(&self) -> FilesApi
Access the Files API.
§Example
use xai_rust::XaiClient;
let client = XaiClient::from_env()?;
let files = client.files().list().await?;Sourcepub fn models(&self) -> ModelsApi
pub fn models(&self) -> ModelsApi
Access the Models API.
§Example
use xai_rust::XaiClient;
let client = XaiClient::from_env()?;
let models = client.models().list().await?;Sourcepub fn realtime(&self) -> RealtimeApi
pub fn realtime(&self) -> RealtimeApi
Access the Realtime API for voice interactions.
§Example
use xai_rust::{XaiClient, Voice, AudioFormat};
let client = XaiClient::from_env()?;
let session = client.realtime()
.connect("grok-4")
.voice(Voice::Ara)
.audio_format(AudioFormat::Pcm16)
.start()
.await?;Sourcepub fn batch(&self) -> BatchApi
pub fn batch(&self) -> BatchApi
Access the Batch API for processing multiple requests.
§Example
use xai_rust::XaiClient;
let client = XaiClient::from_env()?;
let batch = client.batch().create("my-batch").await?;
println!("Created batch: {}", batch.id);Sourcepub fn collections(&self) -> CollectionsApi
pub fn collections(&self) -> CollectionsApi
Access the Collections API for document storage and retrieval.
§Example
use xai_rust::XaiClient;
let client = XaiClient::from_env()?;
let collection = client.collections().create_named("my-docs").await?;
println!("Created collection: {}", collection.id);Sourcepub fn documents(&self) -> DocumentsApi
pub fn documents(&self) -> DocumentsApi
Access the Documents API.
§Example
use xai_rust::api::DocumentsSearchRequest;
use xai_rust::XaiClient;
let client = XaiClient::from_env()?;
let response = client.documents().search(DocumentsSearchRequest::new("query")).await?;
println!("Found {} results", response.results.len());Sourcepub fn embeddings(&self) -> EmbeddingsApi
pub fn embeddings(&self) -> EmbeddingsApi
Access the Embeddings API.
§Example
use serde_json::json;
use xai_rust::api::EmbeddingsRequest;
use xai_rust::XaiClient;
let client = XaiClient::from_env()?;
let response = client
.embeddings()
.create(EmbeddingsRequest::new("text-embedding", json!("Hello")))
.await?;
println!("embedding result {}",
response.data.len());Sourcepub fn tokenizer(&self) -> TokenizerApi
pub fn tokenizer(&self) -> TokenizerApi
Access the Tokenizer API for counting tokens.
§Example
use xai_rust::XaiClient;
let client = XaiClient::from_env()?;
let count = client.tokenizer()
.count_tokens("grok-4", "Hello, world!")
.await?;
println!("Token count: {}", count);