| Crates.io | openrouter-sdk |
| lib.rs | openrouter-sdk |
| version | 0.1.4 |
| created_at | 2025-07-08 21:53:58.922806+00 |
| updated_at | 2025-07-08 22:45:22.76481+00 |
| description | Rust SDK for OpenRouter API |
| homepage | |
| repository | https://github.com/zaoinc/openrouter-sdk |
| max_upload_size | |
| id | 1743644 |
| size | 48,372 |
# OpenRouter Rust SDK 🦀
[](https://crates.io/crates/openrouter-sdk)
[](https://docs.rs/openrouter-sdk)
[](LICENSE)
Type-safe Rust client for [OpenRouter.ai](https://openrouter.ai) with full LLM API support.
```bash
cargo add openrouter-sdk
use openrouter_sdk::{OpenRouterClient, ChatRequest, Message, Content};
use std::io::{self, Write};
use tokio::runtime::Runtime;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let rt = Runtime::new()?;
let client = OpenRouterClient::new(
std::env::var("OPENROUTER_API_KEY")?
);
println!("Chatbot ready! Type 'quit' to exit\n");
let mut messages = vec![Message {
role: "system".to_string(),
content: Content::Text("You're a helpful Rust assistant".to_string()),
}];
loop {
print!("You: ");
io::stdout().flush()?;
let mut input = String::new();
io::stdin().read_line(&mut input)?;
let input = input.trim();
if input.eq_ignore_ascii_case("quit") {
break;
}
messages.push(Message {
role: "user".to_string(),
content: Content::Text(input.to_string()),
});
let response = rt.block_on(client.chat(ChatRequest {
model: "mistralai/mistral-7b-instruct".to_string(),
messages: messages.clone(),
temperature: Some(0.7),
..Default::default()
}))?;
if let Some(choice) = response.choices.first() {
println!("Assistant: {}\n", choice.message.content);
messages.push(choice.message.clone());
}
}
Ok(())
}
export OPENROUTER_API_KEY="your_api_key"
cargo run
let mut stream = client.chat_stream(request).await?;
while let Some(chunk) = stream.next().await {
print!("{}", chunk?.text());
}
Content::Multi(vec![
ContentPart::text("What's in this image?"),
ContentPart::image("https://example.com/cat.jpg")
])
Add to your Cargo.toml:
[dependencies]
openrouter-sdk = "0.1.3"
tokio = { version = "1.0", features = ["full"] }
anywhow="1.0"
PRs welcome! See CONTRIBUTING.md for guidelines.
💬 Pro Tip: The example above works as-is! Try modifying:
model to switch LLMstemperature for creativity control