Skip to main content

Crate grok_api

Crate grok_api 

Source
Expand description

§Grok API Client

A Rust client library for interacting with the Grok AI API (xAI).

This library provides a simple and robust interface to the Grok AI models, with built-in retry logic, error handling, and support for challenging network conditions (like Starlink satellite connections).

§Features

  • Simple API: Easy-to-use async interface
  • Robust Error Handling: Comprehensive error types with detailed messages
  • Automatic Retries: Built-in retry logic for transient network failures
  • Starlink Optimized: Special handling for satellite network drops
  • Rate Limiting: Client-side rate limit tracking (optional)
  • Tool/Function Calling: Support for function calling and tools

§Quick Start

use grok_api::{GrokClient, Result};

#[tokio::main]
async fn main() -> Result<()> {
    // Create a client with your API key
    let client = GrokClient::new("your-api-key")?;

    // Send a simple chat message
    let response = client
        .chat("What is Rust?", None)
        .await?;

    println!("Response: {}", response);
    Ok(())
}

§Advanced Usage

use grok_api::{GrokClient, ChatMessage, Result};
use serde_json::json;

#[tokio::main]
async fn main() -> Result<()> {
    let client = GrokClient::builder()
        .api_key("your-api-key")
        .timeout_secs(60)
        .max_retries(5)
        .build()?;

    // Build a conversation
    let messages = vec![
        ChatMessage::system("You are a helpful Rust programming assistant."),
        ChatMessage::user("How do I create a Vec in Rust?"),
    ];

    let response = client
        .chat_with_history(&messages)
        .temperature(0.7)
        .max_tokens(1000)
        .model("grok-3")
        .send()
        .await?;

    println!("Response: {}", response.content().unwrap_or(""));
    Ok(())
}

Modules§

prelude
Re-export commonly used types

Structs§

ChatMessage
Represents a chat message in a conversation
ChatRequest
A chat completion request
ChatResponse
Response from a chat completion request
Choice
A single completion choice
FunctionCall
Details of a function call
GrokClient
Main client for interacting with the Grok API
GrokClientBuilder
Builder for configuring a GrokClient
ImageUrl
Represents an image URL
Message
A message in a chat completion
RetryConfig
Configuration for retry behavior
ToolCall
A tool/function call made by the assistant
Usage
Token usage information
VideoUrl
Represents a video URL

Enums§

ContentPart
A part of a message content (text, image, or video)
Error
Main error type for the Grok API client
MessageContent
The content of a message, which can be a simple string or a list of parts
Model
Available Grok models
Role
Role of a message in a conversation
StopReason
Reason why a completion finished

Constants§

DEFAULT_API_BASE_URL
The default API base URL for Grok (xAI)
DEFAULT_MAX_RETRIES
Default maximum number of retries
DEFAULT_MODEL
The default model to use if none is specified
DEFAULT_TIMEOUT_SECS
Default timeout in seconds
VERSION
The version of this crate

Type Aliases§

Result
Result type alias for Grok API operations