openrouter2
openrouter2 is a typed Rust client for the OpenRouter API. Version 0.4
targets the full current non-deprecated route set from the OpenRouter OpenAPI
spec snapshot dated 2026-07-08.
Clients can store an optional default API key. Keys are wrapped in a redacted
zeroizing type, and individual calls can override or suppress auth through
RequestOptions.
Install
[]
= "0.4"
= { = "0.13", = false, = ["json", "rustls"] }
= { = "1", = ["macros", "rt-multi-thread"] }
Async support is enabled by default. To enable the blocking client in addition to the default async client:
= { = "0.4", = ["blocking"] }
To build with only the blocking client:
= { = "0.4", = false, = ["blocking"] }
Async Usage
use ;
async
Blocking Usage
use ;
API Surface
- Async client:
AsyncOpenRouterClientbehind default featureasync. - Blocking client:
BlockingOpenRouterClientbehind featureblocking. - Optional stored
ApiKeywith redactedDebugoutput and zeroization on drop. - Route-complete flat methods for all non-deprecated OpenRouter routes.
- Typed request/response structs for high-value inference and management APIs, with raw extras for newly added fields.
- Typed query structs for paginated and filterable list APIs.
- Unknown-preserving string enums and flattened raw
serde_json::Valueextras. - Typed SSE streaming for chat, responses, and messages.
- Raw JSON, binary, and multipart escape hatches for new API fields/routes.
RequestOptionsfor per-call headers such asHTTP-Referer,X-Title,X-Session-Id, custom headers, API-key override, and explicit no-auth calls.- First-class multipart speech-to-text helper via
TranscriptionFileRequest.
try_new accepts HTTPS OpenRouter base URLs. Local test servers and trusted
proxies must opt in explicitly with try_new_unchecked_base_url.
Observability
The crate emits tracing events for HTTP request start, completion, non-2xx
responses, and transport errors. It does not install or configure a tracing
subscriber; applications remain in control of where those events go.
Trace fields include method, route template, status, elapsed milliseconds,
request id when present, query count, and whether the request was authenticated.
Request and response bodies are not logged. Bearer tokens, OpenRouter-style
sk-... keys, credentials, query values, and sensitive headers such as
Authorization, Cookie, Set-Cookie, token, password, secret, and API-key
fields are redacted before they reach tracing or API error metadata.
Deprecated OpenRouter operations are intentionally skipped. The deprecated Coinbase credits endpoint is not modeled.
Examples
Runnable examples live in examples/:
stored_key_chat.rssends a chat completion with a stored client key.transcribe_file.rssubmits multipart audio transcription.workspace_members.rslists workspace members with typed pagination.per_request_key_override.rsoverrides the stored key for one request.
MSRV
The minimum supported Rust version is 1.88.