grok_api
A Rust client library for the Grok AI API (xAI). Simple, robust, and production-ready.
Features
- 🚀 Easy to use - Simple async API with builder pattern
- 🔄 Automatic retries - Built-in retry logic for transient failures
- 🛡️ Robust error handling - Comprehensive error types with detailed messages
- 🌐 Network resilient - Optimized for challenging conditions (Starlink, satellite connections)
- 🔧 Flexible configuration - Customize timeouts, retries, and more
- 📊 Rate limiting - Client-side rate limit tracking (optional)
- 🛠️ Tool/Function calling - Full support for function calling and tools
Quick Start
Add this to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["full"] }
Simple Example
use ;
async
Conversation with History
use ;
async
Advanced Configuration
use ;
async
API Key
Get your API key from x.ai.
Set it as an environment variable:
Or pass it directly to the client:
let client = new?;
Available Models
grok-3- Latest flagship model (default)grok-3-mini- Efficient smaller modelgrok-2-latest- Previous generationgrok-2- Stable previous generationgrok-beta- Experimental featuresgrok-vision-beta- Vision capabilities
// Use a specific model
let response = client.chat.await?;
// Or with the builder
let response = client
.chat_with_history
.model
.send
.await?;
Error Handling
The library provides comprehensive error types:
use ;
match client.chat.await
Retry Logic
Network errors are automatically retried with exponential backoff:
let client = builder
.api_key
.max_retries // Retry up to 5 times
.build?;
Retryable errors include:
- Network timeouts
- Connection failures
- Server errors (5xx)
- Starlink/satellite network drops
Function Calling / Tools
The library supports Grok's function calling feature:
use ;
use json;
let tools = vec!;
let messages = vec!;
let response = client
.chat_with_history
.tools
.send
.await?;
if response.has_tool_calls
Starlink Optimization
The library includes special handling for Starlink and other satellite connections:
- Automatic detection of connection drops
- Exponential backoff with jitter
- Extended timeout handling
- Connection pooling and keep-alive
use ;
let client = builder
.api_key
.timeout_secs // Longer timeout for satellite
.max_retries // More retries for drops
.build?;
match client.chat.await
Examples
Run the examples:
# Set your API key
# Simple chat
# Multi-turn conversation
# Streaming (placeholder for future feature)
Features
Default Features
retry- Automatic retry logic (enabled by default)
Optional Features
starlink- Additional optimizations for Starlink connections
[]
= { = "0.1", = ["starlink"] }
Testing
# Run tests
# Run tests with logging
RUST_LOG=debug
# Run integration tests (requires API key)
GROK_API_KEY="your-key"
Minimum Supported Rust Version (MSRV)
This crate requires Rust 1.70 or later.
Documentation
Full API documentation is available at docs.rs/grok_api.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Disclaimer
This is an unofficial library and is not affiliated with, endorsed by, or sponsored by xAI or X Corp.
Links
Changelog
See CHANGELOG.md for release notes and version history.
Support
For bugs and feature requests, please open an issue.
Made with ❤️ and Rust
