OpenRouter Rust SDK
A type-safe, async Rust SDK for the OpenRouter API - Access 200+ AI models with ease
π Documentation | π― Examples | π¦ Crates.io
π What makes this special?
- π Type Safety: Leverages Rust's type system for compile-time error prevention
- β‘ Async/Await: Built on
tokio
for high-performance concurrent operations - π§ Reasoning Tokens: Industry-leading chain-of-thought reasoning support
- π‘ Streaming: Real-time response streaming with
futures
- ποΈ Builder Pattern: Ergonomic client and request construction
- βοΈ Smart Presets: Curated model groups for programming, reasoning, and free tiers
- π― Complete Coverage: All OpenRouter API endpoints supported
π Quick Start
Installation
Add to your Cargo.toml
:
[]
= "0.4.5"
= { = "1", = ["full"] }
30-Second Example
use ;
async
β¨ Key Features
π§ Advanced Reasoning Support
Leverage chain-of-thought processing with reasoning tokens:
use Effort;
let request = builder
.model
.messages
.reasoning_effort // Enable deep reasoning
.reasoning_max_tokens // Control reasoning depth
.build?;
let response = client.send_chat_completion.await?;
// Access both reasoning and final answer
println!;
println!;
π‘ Real-time Streaming
Process responses as they arrive:
use StreamExt;
let stream = client.stream_chat_completion.await?;
stream
.filter_map
.for_each
.await;
βοΈ Smart Model Presets
Use curated model collections:
use OpenRouterConfig;
let config = default;
// Three built-in presets:
// β’ programming: Code generation and development
// β’ reasoning: Advanced problem-solving models
// β’ free: Free-tier models for experimentation
println!;
π‘οΈ Comprehensive Error Handling
use OpenRouterError;
match client.send_chat_completion.await
π API Coverage
Feature | Status | Module |
---|---|---|
Chat Completions | β | api::chat |
Text Completions | β | api::completion |
Reasoning Tokens | β | api::chat |
Streaming Responses | β | api::chat |
Model Information | β | api::models |
API Key Management | β | api::api_keys |
Credit Management | β | api::credits |
Authentication | β | api::auth |
π― More Examples
Filter Models by Category
use ModelCategory;
let models = client
.list_models_by_category
.await?;
println!;
Advanced Client Configuration
let client = builder
.api_key
.http_referer
.x_title
.base_url // Custom endpoint
.build?;
Streaming with Reasoning
let stream = client.stream_chat_completion.await?;
let mut reasoning_buffer = String new;
let mut content_buffer = String new;
stream.filter_map
.for_each.await;
println!;
println!;
π Documentation & Resources
- π API Documentation - Complete API reference
- π― Examples Repository - Comprehensive usage examples
- π§ Configuration Guide - Model presets and configuration
- β‘ OpenRouter API Docs - Official OpenRouter documentation
Run Examples Locally
# Set your API key
# Basic chat completion
# Reasoning tokens demo
# Streaming responses
# Run with reasoning
π€ Community & Support
π Found a Bug?
Please open an issue with:
- Your Rust version (
rustc --version
) - SDK version you're using
- Minimal code example
- Expected vs actual behavior
π‘ Feature Requests
We love hearing your ideas! Start a discussion to:
- Suggest new features
- Share use cases
- Get help with implementation
π οΈ Contributing
Contributions are welcome! Please see our contributing guidelines:
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Follow the existing code style
- Submit a pull request
β Show Your Support
If this SDK helps your project, consider:
- β Starring the repository
- π¦ Sharing on social media
- π Writing about your experience
- π€ Contributing improvements
π Requirements
- Rust: 1.85+ (2024 edition)
- Tokio: 1.0+ (for async runtime)
- OpenRouter API Key: Get yours here
πΊοΈ Roadmap
- WebSocket Support - Real-time bidirectional communication
- Retry Strategies - Automatic retry with exponential backoff
- Caching Layer - Response caching for improved performance
- CLI Tool - Command-line interface for quick testing
- Middleware System - Request/response interceptors
π License
This project is licensed under the MIT License - see the LICENSE file for details.
β οΈ Disclaimer
This is a third-party SDK not officially affiliated with OpenRouter. Use at your own discretion.
π Release History
Version 0.4.5 (Latest)
- π§ New: Complete reasoning tokens implementation with chain-of-thought support
- βοΈ Updated: Model presets restructured to
programming
/reasoning
/free
categories - π Enhanced: Professional-grade documentation with comprehensive examples
- ποΈ Improved: Configuration system with better model management
Version 0.4.4
- Added: Support for listing models by supported parameters
- Note: OpenRouter API limitations on simultaneous category and parameter filtering
Version 0.4.3
- Added: Support for listing models by category
- Thanks to OpenRouter team for the API enhancement!
Made with β€οΈ for the Rust community
β Star us on GitHub | π¦ Find us on Crates.io | π Read the Docs