Skip to content
This repository was archived by the owner on Aug 17, 2026. It is now read-only.

Repository files navigation

openrouter-rust

Crates.io Docs.rs CI License: Unlicense

An idiomatic, async Rust SDK for the OpenRouter API. A Rust port of openrouter-go; behavior and defaults are kept in sync with the Go SDK on purpose.

Install

cargo add openrouter-client

The crate is published as openrouter-client on crates.io (openrouter, openrouter-rust, and openrouter-rs were all already taken), but it imports as openrouter — your code writes use openrouter::....

MSRV is 1.75. Optional beta feature gates the Responses API:

[dependencies]
openrouter-client = { version = "0.2", features = ["beta"] }

Browser WebAssembly builds use the opt-in browser feature:

[dependencies]
openrouter-client = { version = "0.2", features = ["browser"] }

The feature selects browser randomness, timers, Fetch/ReadableStream transport, and local futures for wasm32-unknown-unknown. Native builds keep their existing Tokio, reqwest streaming, and rustls transport.

Quickstart

use openrouter::{ChatCompletionRequest, Client, Message};

#[tokio::main]
async fn main() -> openrouter::Result<()> {
    let client = Client::builder()
        .api_key(std::env::var("OPENROUTER_API_KEY").unwrap())
        .app_name("my-app")
        .build()?;

    let req = ChatCompletionRequest::new(
        "google/gemini-3.1-flash-lite",
        vec![Message::user("Hello, who are you?")],
    );
    let resp = client.chat_complete(req).await?;
    if let Some(text) = resp.choices.first()
        .and_then(|c| c.message.as_ref())
        .and_then(|m| m.content_text())
    {
        println!("{text}");
    }
    Ok(())
}

Streaming:

use futures::StreamExt;
use openrouter::{ChatCompletionRequest, Client, Message};

# async fn run() -> openrouter::Result<()> {
let client = Client::builder().api_key("sk-…").build()?;
let req = ChatCompletionRequest::new(
    "google/gemini-3.1-flash-lite",
    vec![Message::user("Stream a haiku.")],
);
let mut stream = client.chat_complete_stream(req).await?;
while let Some(chunk) = stream.next().await {
    let chunk = chunk?;
    if let Some(delta) = chunk.choices.first()
        .and_then(|c| c.delta.as_ref())
        .and_then(|d| d.content.as_deref())
    {
        print!("{delta}");
    }
}
# Ok(()) }

Features

Surface Status
Chat completions + legacy completions (blocking + streaming) ✅
Tool / function calling (incl. streaming tool-call deltas) ✅
Structured outputs (JSON Schema + JSON mode) ✅
MCP tool conversion ✅
Message transforms (middle-out) ✅
Web-search plugin ✅
Provider routing (order, sort, only/ignore, quantization, max price, ZDR) ✅
Reasoning tokens (effort + max-tokens) ✅
Multimodal (images, PDFs with parsing engines, audio, text files) ✅
Discovery (/models, /models/{author}/{slug}/endpoints, /providers) ✅
Account (credits, activity, current key, ZDR endpoint listing) ✅
Provisioning key CRUD ✅
Workspaces + organization members ✅
Guardrails (spend caps, allowlists, key/member assignments) ✅
Rerank (/rerank) ✅
Text-to-speech (/audio/speech) ✅
Async video generation (submit / poll / download) ✅
Broadcast webhook parser (OTLP JSON) ✅
OAuth PKCE helpers ✅
Browser WebAssembly (browser feature) ✅
[beta] Responses API (gated behind the beta cargo feature) ✅

Recipes

In-tree recipes are embedded in rustdoc and viewable on docs.rs:

End-to-end smoke tests

The e2e example is a single binary that mirrors the Go SDK's cmd/openrouter-test/ layout. Every subcommand hits the live OpenRouter API using google/gemini-3.1-flash-lite by default.

OPENROUTER_API_KEY=sk-... cargo run --example e2e -- --help
OPENROUTER_API_KEY=sk-... cargo run --example e2e -- chat
OPENROUTER_API_KEY=sk-... cargo run --example e2e -- stream
OPENROUTER_API_KEY=sk-... cargo run --example e2e -- tools

Subcommands: chat, stream, completion, tools, transforms, websearch, models, endpoints, providers, credits, activity, key, listkeys, createkey, updatekey, deletekey. The *key provisioning subcommands require a provisioning key, not a runtime key.

Roadmap

# Phase Status
1 Foundation — client builder, error model, retry/backoff, core types ✅
2 Core Endpoints & Streaming — chat, legacy completions, SSE ✅
3 Advanced Inference — tools, structured outputs, MCP, transforms, web search, provider routing, reasoning ✅
4 Multimodal Inputs — images, PDFs, audio, text files, ContentBuilder ✅
5 Discovery & Account — models, endpoints, providers, credits, activity, key CRUD ✅
6 Org & Beta Surfaces — workspaces, members, guardrails, ZDR, rerank, TTS, video, webhooks, OAuth, [beta] Responses API ✅
7 Testing, Docs & Release — unit + E2E test coverage, docs site, crates.io publish ✅

Reference

License

Released into the public domain under The Unlicense. Same license as the Go SDK.

About

An idiomatic, async Rust SDK for the OpenRouter API (port of openrouter-go).

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages