Expand description
§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")
.referer("https://my-app.example.com")
.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(())
}§Client basics
Clientis cheap to clone (Arc<ClientInner>). Clone it freely across tasks; all clones share the samereqwest::Clientand connection pool.- The builder validates eagerly: missing
api_keyreturnsError::MissingField; a malformedbase_urlreturnsError::InvalidInput. - Custom HTTP client: pass your own
reqwest::Clientto the builder; the builder’stimeout()is ignored in that case. - Retries (3 attempts by default) cover 429 and 5xx responses plus
transport-level errors. Configure with
.retry(max, base_delay).
§More recipes
Re-exports§
pub use client::Client;pub use client::ClientBuilder;pub use error::Error;pub use error::Result;pub use retry::RetryConfig;pub use stream::EventStream;pub use tool_call_accumulator::ToolCallAccumulator;pub use types::create_file_parser_plugin;pub use types::create_user_message_with_audio;pub use types::create_user_message_with_audio_bytes;pub use types::create_user_message_with_base64_image;pub use types::create_user_message_with_base64_image_bytes;pub use types::create_user_message_with_base64_pdf;pub use types::create_user_message_with_files;pub use types::create_user_message_with_image;pub use types::create_user_message_with_image_detail;pub use types::create_user_message_with_images;pub use types::create_user_message_with_pdf;pub use types::create_user_message_with_text_content;pub use types::create_user_message_with_text_file;pub use types::create_user_message_with_text_files;pub use types::encode_image_bytes_to_base64;pub use types::encode_image_to_base64;pub use types::ActivityData;pub use types::ActivityOptions;pub use types::ActivityResponse;pub use types::Annotation;pub use types::ApiKey;pub use types::AudioFormat;pub use types::ChatCompletionRequest;pub use types::ChatCompletionResponse;pub use types::Choice;pub use types::CompletionRequest;pub use types::CompletionResponse;pub use types::Content;pub use types::ContentBuilder;pub use types::ContentPart;pub use types::CreateKeyRequest;pub use types::CreateKeyResponse;pub use types::CreditsData;pub use types::CreditsResponse;pub use types::DeleteKeyData;pub use types::DeleteKeyResponse;pub use types::Delta;pub use types::File;pub use types::FileAnnotation;pub use types::FileParserEngine;pub use types::FilePdfConfig;pub use types::FilePluginConfig;pub use types::FileRef;pub use types::FunctionCall;pub use types::FunctionDef;pub use types::GetKeyByHashResponse;pub use types::ImageDetail;pub use types::ImageUrl;pub use types::InputAudio;pub use types::KeyData;pub use types::KeyRateLimit;pub use types::KeyResponse;pub use types::ListKeysOptions;pub use types::ListKeysResponse;pub use types::ListModelsOptions;pub use types::Message;pub use types::Model;pub use types::ModelArchitecture;pub use types::ModelDefaultParameters;pub use types::ModelEndpoint;pub use types::ModelEndpointPricing;pub use types::ModelEndpointsArchitecture;pub use types::ModelEndpointsData;pub use types::ModelEndpointsResponse;pub use types::ModelPerRequestLimits;pub use types::ModelPricing;pub use types::ModelTopProvider;pub use types::ModelsResponse;pub use types::Plugin;pub use types::Provider;pub use types::ProviderInfo;pub use types::ProvidersResponse;pub use types::ReasoningConfig;pub use types::ResponseFormat;pub use types::Role;pub use types::Tool;pub use types::ToolCall;pub use types::ToolChoice;pub use types::UpdateKeyRequest;pub use types::UpdateKeyResponse;pub use types::UrlCitation;pub use types::Usage;pub use types::WebPluginConfig;pub use types::AssignKeysRequest;pub use types::AssignKeysResponse;pub use types::AssignMembersRequest;pub use types::AssignMembersResponse;pub use types::BulkAddWorkspaceMembersResponse;pub use types::BulkRemoveWorkspaceMembersResponse;pub use types::CreateGuardrailRequest;pub use types::CreateWorkspaceRequest;pub use types::CreateWorkspaceResponse;pub use types::DeleteGuardrailResponse;pub use types::DeleteWorkspaceResponse;pub use types::GetWorkspaceResponse;pub use types::Guardrail;pub use types::GuardrailKeyAssignment;pub use types::GuardrailMemberAssignment;pub use types::ListGuardrailKeyAssignmentsResponse;pub use types::ListGuardrailMemberAssignmentsResponse;pub use types::ListGuardrailsOptions;pub use types::ListGuardrailsResponse;pub use types::ListOrganizationMembersOptions;pub use types::ListOrganizationMembersResponse;pub use types::ListWorkspacesOptions;pub use types::ListWorkspacesResponse;pub use types::OrganizationMember;pub use types::OrganizationMemberRole;pub use types::PercentileStats;pub use types::PublicEndpoint;pub use types::PublicEndpointPricing;pub use types::RerankDocument;pub use types::RerankRequest;pub use types::RerankResponse;pub use types::RerankResult;pub use types::RerankUsage;pub use types::ResetInterval;pub use types::SpeechFormat;pub use types::SpeechProvider;pub use types::SpeechRequest;pub use types::SpeechResponse;pub use types::UpdateGuardrailRequest;pub use types::UpdateWorkspaceRequest;pub use types::UpdateWorkspaceResponse;pub use types::VideoAspectRatio;pub use types::VideoContentPartImage;pub use types::VideoContentResponse;pub use types::VideoFrameImage;pub use types::VideoFrameType;pub use types::VideoGenerationRequest;pub use types::VideoGenerationResponse;pub use types::VideoGenerationUsage;pub use types::VideoImageUrl;pub use types::VideoModel;pub use types::VideoModelsResponse;pub use types::VideoProvider;pub use types::VideoResolution;pub use types::VideoStatus;pub use types::Workspace;pub use types::WorkspaceMember;pub use types::WorkspaceMemberRole;pub use types::ZdrEndpointsResponse;
Modules§
- client
ClientandClientBuilder.- error
- Error model for the crate.
- mcp
- Convert Model Context Protocol tool
definitions into OpenRouter
Tools. - oauth
- OAuth PKCE helpers for the OpenRouter authorization flow.
- responses
- [BETA] OpenRouter Responses API.
- retry
- Retry + exponential-backoff-with-jitter middleware.
- stream
- SSE parser + generic
EventStream<T>. - tool_
call_ accumulator - Accumulate streaming tool-call deltas into complete
ToolCalls. - types
- Shared serde types for messages, requests, and responses.
- webhooks
- Parser for OpenRouter Broadcast webhook payloads (OTLP JSON traces).