Skip to Content
unii.docs
Sdks

Rust SDK

Fast, safe, and robust Rust client crate with Serde serialization and streaming support.

Overview

The Unii Rust SDK (unii crate) is a 100% safe Rust client crate with Serde serialization, asynchronous tokio support, and Server-Sent Events (SSE) streaming for Unii APIs.

Installation

Add unii to your project using Cargo:

cargo add unii

Or add it directly to your Cargo.toml:

Cargo.toml
[dependencies]
unii = "0.1.0"

Initialize and use the client

Create a UniiClient and send requests:

main.rs
use unii::{ChatMessage, ChatCompletionRequest, UniiClient, UniiError};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let api_key = std::env::var("UNII_API_KEY")?;
    let client = UniiClient::new(api_key)?;

    // 1. Verify API key
    let auth = client.verify_auth().await?;
    println!("Key: {}", auth.key_name);

    // 2. Create chat completion
    let req = ChatCompletionRequest {
        messages: vec![ChatMessage::user("What is the return policy?")],
        stream: Some(false),
        temperature: Some(0.7),
        max_tokens: Some(1024),
        project_id: None,
    };

    match client.create_chat_completion(&req).await {
        Ok(res) => println!("{}", res.choices[0].message.content),
        Err(UniiError::Api(e)) => eprintln!("API Error {}: {}", e.status, e.message),
        Err(e) => eprintln!("Error: {}", e),
    }

    Ok(())
}

On this page