OpenRouter Rust SDK
openrouter-rs
is a third-party Rust SDK that helps you interact with the OpenRouter API. It wraps various endpoints of the OpenRouter API, making it easier to use in Rust projects. By taking advantage of Rust's strengths like type safety, memory safety, and concurrency without data races, openrouter-rs
ensures a solid and reliable integration with the OpenRouter API.
Current Status
This SDK is currently in active development and supports both simple and advanced usage patterns. I've implemented basic integration tests covering:
- Get API key information
- Model listing
- Chat completions
- Response validation
If you encounter any issues while using it, please open an issue to help us improve.
Notice: I'm trying to simplify the codebase and remove some unnecessary features in version 0.5.0, including:
- Remove commands and keyring
- Simplify configuration loading
- Remove unnecessary dependencies
TODO
- Testing
- Core integration tests
- Complete API coverage
- Features
- Advanced model management capabilities
- Use cargo feature flags to enable/disable features
- Add cli tools for easy usage
If you have any suggestions or feedback, feel free to open an issue or submit a pull request.
Features
- ✅ Builder pattern for client configuration and complex requests
- ✅ Simple constructors for basic usage
- ✅ Full API coverage including:
- API key management
- Chat and text completions
- Streaming responses
- Credit management
- Model information
Installation
Add to your Cargo.toml
:
[]
= "0.4.2"
Quick Start
Using Client Builder Pattern (Recommended)
use ;
async
Simple Client Constructor
// Simple client creation with just API key
let client = builder.build?;
Key Features Explained
1. Builder Pattern for Client Configuration
The client now uses a builder pattern for more flexible configuration:
let client = builder
.http_referer
.x_title
.build?;
Benefits:
- Immutable client configuration
- Clear, chainable configuration
- Default values for optional parameters
2. Streaming Responses
use StreamExt;
let client = builder.build?;
let request = builder
.model
.messages
.max_tokens
.temperature
.build?;
let mut stream = client.stream_chat_completion.await?;
while let Some = stream.next.await
3. Error Handling
Comprehensive error types:
match client.send_chat_completion.await
Examples
Run examples with:
# Builder pattern example
# Simple usage
# Streaming
# Run integration tests (requires API key)
OPENROUTER_API_KEY=your_key
Best Practices
- Client Configuration: Use
OpenRouterClient::builder()
for flexible setup - Request Building: Use builders (
Request::builder()
) for complex requests - Error Handling: Match on
OpenRouterError
variants - Reuse Clients: Create one
OpenRouterClient
per application
Migration Guide
If upgrading from older versions:
Old Style | New Recommended Style |
---|---|
OpenRouterClient::new(key) |
OpenRouterClient::builder(key).build() |
client.base_url("url") |
OpenRouterClient::builder(key).base_url("url").build() |
ChatCompletionRequest::new() |
ChatCompletionRequest::builder().build() |
Risk Disclaimer
This is a third-party SDK not affiliated with OpenRouter. Use at your own risk.
Contributing
Contributions welcome! Please:
- Follow the builder pattern for new features
- Include documentation examples
- Add tests for new features
License
MIT - See LICENSE