pub struct OpenRouterClient { /* private fields */ }
Implementations§
Source§impl OpenRouterClient
impl OpenRouterClient
pub fn builder() -> OpenRouterClientBuilder
Sourcepub fn set_api_key(&mut self, api_key: impl Into<String>)
pub fn set_api_key(&mut self, api_key: impl Into<String>)
Sourcepub fn clear_api_key(&mut self)
pub fn clear_api_key(&mut self)
Clears the currently set API key.
§Example
let mut client = OpenRouterClient::builder().api_key("your_api_key").build();
client.clear_api_key();
Sourcepub fn set_provisioning_key(&mut self, provisioning_key: impl Into<String>)
pub fn set_provisioning_key(&mut self, provisioning_key: impl Into<String>)
Sourcepub fn clear_provisioning_key(&mut self)
pub fn clear_provisioning_key(&mut self)
Clears the currently set provisioning key.
§Example
let mut client = OpenRouterClient::builder().build();
client.set_provisioning_key("your_provisioning_key");
client.clear_provisioning_key();
Sourcepub async fn create_api_key(
&self,
name: &str,
limit: Option<f64>,
) -> Result<ApiKey, OpenRouterError>
pub async fn create_api_key( &self, name: &str, limit: Option<f64>, ) -> Result<ApiKey, OpenRouterError>
Creates a new API key. Requires a Provisioning API key.
§Arguments
name
- The display name for the new API key.limit
- Optional credit limit for the new API key.
§Returns
Result<api_keys::ApiKey, OpenRouterError>
- The created API key.
§Example
let client = OpenRouterClient::builder().api_key("your_api_key").build();
let api_key = client.create_api_key("New API Key", Some(100.0)).await?;
println!("{:?}", api_key);
Sourcepub async fn get_current_api_key_info(
&self,
) -> Result<ApiKeyDetails, OpenRouterError>
pub async fn get_current_api_key_info( &self, ) -> Result<ApiKeyDetails, OpenRouterError>
Get information on the API key associated with the current authentication session.
§Returns
Result<api_keys::ApiKeyDetails, OpenRouterError>
- The details of the current API key.
§Example
let client = OpenRouterClient::builder().api_key("your_api_key").build();
let api_key_details = client.get_current_api_key_info().await?;
println!("{:?}", api_key_details);
Sourcepub async fn delete_api_key(&self, hash: &str) -> Result<bool, OpenRouterError>
pub async fn delete_api_key(&self, hash: &str) -> Result<bool, OpenRouterError>
Deletes an API key. Requires a Provisioning API key.
§Arguments
hash
- The hash of the API key to delete.
§Returns
Result<bool, OpenRouterError>
- A boolean indicating whether the deletion was successful.
§Example
let client = OpenRouterClient::builder().provisioning_key("your_provisioning_key").build();
let success = client.delete_api_key("api_key_hash").await?;
println!("Deletion successful: {}", success);
Sourcepub async fn update_api_key(
&self,
hash: &str,
name: Option<String>,
disabled: Option<bool>,
limit: Option<f64>,
) -> Result<ApiKey, OpenRouterError>
pub async fn update_api_key( &self, hash: &str, name: Option<String>, disabled: Option<bool>, limit: Option<f64>, ) -> Result<ApiKey, OpenRouterError>
Updates an existing API key. Requires a Provisioning API key.
§Arguments
hash
- The hash of the API key to update.name
- Optional new display name for the API key.disabled
- Optional flag to disable the API key.limit
- Optional new credit limit for the API key.
§Returns
Result<api_keys::ApiKey, OpenRouterError>
- The updated API key.
§Example
let client = OpenRouterClient::builder().provisioning_key("your_provisioning_key").build();
let updated_api_key = client.update_api_key("api_key_hash", Some("Updated Name".to_string()), Some(false), Some(200.0)).await?;
println!("{:?}", updated_api_key);
Sourcepub async fn list_api_keys(
&self,
offset: Option<f64>,
include_disabled: Option<bool>,
) -> Result<Vec<ApiKey>, OpenRouterError>
pub async fn list_api_keys( &self, offset: Option<f64>, include_disabled: Option<bool>, ) -> Result<Vec<ApiKey>, OpenRouterError>
Returns a list of all API keys associated with the account. Requires a Provisioning API key.
§Arguments
offset
- Optional offset for the API keys.include_disabled
- Optional flag to include disabled API keys.
§Returns
Result<Vec<api_keys::ApiKey>, OpenRouterError>
- A list of API keys.
§Example
let client = OpenRouterClient::builder().provisioning_key("your_provisioning_key").build();
let api_keys = client.list_api_keys(Some(0.0), Some(true)).await?;
println!("{:?}", api_keys);
Sourcepub async fn get_api_key(&self, hash: &str) -> Result<ApiKey, OpenRouterError>
pub async fn get_api_key(&self, hash: &str) -> Result<ApiKey, OpenRouterError>
Returns details about a specific API key. Requires a Provisioning API key.
§Arguments
hash
- The hash of the API key to retrieve.
§Returns
Result<api_keys::ApiKey, OpenRouterError>
- The details of the specified API key.
§Example
let client = OpenRouterClient::builder().provisioning_key("your_provisioning_key").build();
let api_key = client.get_api_key("api_key_hash").await?;
println!("{:?}", api_key);
Sourcepub async fn exchange_code_for_api_key(
&self,
code: &str,
code_verifier: Option<&str>,
code_challenge_method: Option<CodeChallengeMethod>,
) -> Result<AuthResponse, OpenRouterError>
pub async fn exchange_code_for_api_key( &self, code: &str, code_verifier: Option<&str>, code_challenge_method: Option<CodeChallengeMethod>, ) -> Result<AuthResponse, OpenRouterError>
Exchange an authorization code from the PKCE flow for a user-controlled API key.
§Arguments
code
- The authorization code received from the OAuth redirect.code_verifier
- The code verifier if code_challenge was used in the authorization request.code_challenge_method
- The method used to generate the code challenge.
§Returns
Result<auth::AuthResponse, OpenRouterError>
- The API key and user ID associated with the API key.
§Example
let client = OpenRouterClient::builder().api_key("your_api_key").build();
let auth_response = client.exchange_code_for_api_key("auth_code", Some("code_verifier"), Some(auth::CodeChallengeMethod::S256)).await?;
println!("{:?}", auth_response);
Sourcepub async fn send_chat_completion(
&self,
request: &ChatCompletionRequest,
) -> Result<CompletionsResponse, OpenRouterError>
pub async fn send_chat_completion( &self, request: &ChatCompletionRequest, ) -> Result<CompletionsResponse, OpenRouterError>
Send a chat completion request to a selected model.
§Arguments
request
- The chat completion request built using ChatCompletionRequest::builder().
§Returns
Result<chat::ChatCompletionResponse, OpenRouterError>
- The response from the chat completion request.
§Example
let client = OpenRouterClient::builder().api_key("your_api_key").build();
let request = chat::ChatCompletionRequest::builder()
.model("deepseek/deepseek-chat-v3-0324:free")
.messages(vec![chat::Message::new(chat::Role::User, "What is the meaning of life?")])
.max_tokens(100)
.temperature(0.7)
.build()?;
let response = client.send_chat_completion(&request).await?;
println!("{:?}", response);
Sourcepub async fn stream_chat_completion(
&self,
request: &ChatCompletionRequest,
) -> Result<BoxStream<'static, Result<CompletionsResponse, OpenRouterError>>, OpenRouterError>
pub async fn stream_chat_completion( &self, request: &ChatCompletionRequest, ) -> Result<BoxStream<'static, Result<CompletionsResponse, OpenRouterError>>, OpenRouterError>
Streams chat completion events from a selected model.
§Arguments
request
- The chat completion request built using ChatCompletionRequest::builder().
§Returns
Result<BoxStream<'static, Result<CompletionsResponse, OpenRouterError>>, OpenRouterError>
- A stream of chat completion events or an error.
§Example
let client = OpenRouterClient::builder().api_key("your_api_key").build();
let request = chat::ChatCompletionRequest::builder()
.model("deepseek/deepseek-chat-v3-0324:free")
.messages(vec![chat::Message::new(chat::Role::User, "Tell me a joke.")])
.max_tokens(50)
.temperature(0.5)
.build()?;
let mut stream = client.stream_chat_completion(&request).await?;
while let Some(event) = stream.next().await {
match event {
Ok(event) => println!("{:?}", event),
Err(e) => eprintln!("Error: {:?}", e),
}
}
Sourcepub async fn send_completion_request(
&self,
request: &CompletionRequest,
) -> Result<CompletionsResponse, OpenRouterError>
pub async fn send_completion_request( &self, request: &CompletionRequest, ) -> Result<CompletionsResponse, OpenRouterError>
Send a completion request to a selected model (text-only format).
§Arguments
request
- The completion request built using CompletionRequest::builder().
§Returns
Result<completion::CompletionResponse, OpenRouterError>
- The response from the completion request, containing the generated text and other details.
§Example
let client = OpenRouterClient::builder().api_key("your_api_key").build();
let request = completion::CompletionRequest::builder()
.model("deepseek/deepseek-chat-v3-0324:free")
.prompt("Once upon a time")
.max_tokens(100)
.temperature(0.7)
.build()?;
let response = client.send_completion_request(&request).await?;
println!("{:?}", response);
Sourcepub async fn create_coinbase_charge(
&self,
request: &CoinbaseChargeRequest,
) -> Result<CoinbaseChargeData, OpenRouterError>
pub async fn create_coinbase_charge( &self, request: &CoinbaseChargeRequest, ) -> Result<CoinbaseChargeData, OpenRouterError>
Creates and hydrates a Coinbase Commerce charge for cryptocurrency payments.
§Arguments
request
- The request data built using CoinbaseChargeRequest::builder().
§Returns
Result<credits::CoinbaseChargeData, OpenRouterError>
- The response data containing the charge details.
§Example
let client = OpenRouterClient::builder().api_key("your_api_key").build();
let request = credits::CoinbaseChargeRequest::builder()
.amount(100.0)
.sender("sender_address")
.chain_id(1)
.build()?;
let response = client.create_coinbase_charge(&request).await?;
println!("{:?}", response);
Sourcepub async fn get_credits(&self) -> Result<CreditsData, OpenRouterError>
pub async fn get_credits(&self) -> Result<CreditsData, OpenRouterError>
Returns the total credits purchased and used for the authenticated user.
§Returns
Result<credits::CreditsData, OpenRouterError>
- The response data containing the total credits and usage.
§Example
let client = OpenRouterClient::builder().api_key("your_api_key").build();
let credits_data = client.get_credits().await?;
println!("{:?}", credits_data);
Sourcepub async fn get_generation(
&self,
id: impl Into<String>,
) -> Result<GenerationData, OpenRouterError>
pub async fn get_generation( &self, id: impl Into<String>, ) -> Result<GenerationData, OpenRouterError>
Returns metadata about a specific generation request.
§Arguments
request
- The GenerationRequest built using GenerationRequest::builder().
§Returns
Result<generation::GenerationData, OpenRouterError>
- The metadata of the generation request or an error.
§Example
let client = OpenRouterClient::builder().api_key("your_api_key").build();
let request = generation::GenerationRequest::builder()
.id("generation_id")
.build()?;
let generation_data = client.get_generation(&request).await?;
println!("{:?}", generation_data);
Sourcepub async fn list_models(&self) -> Result<Vec<Model>, OpenRouterError>
pub async fn list_models(&self) -> Result<Vec<Model>, OpenRouterError>
Sourcepub async fn list_models_by_category(
&self,
category: ModelCategory,
) -> Result<Vec<Model>, OpenRouterError>
pub async fn list_models_by_category( &self, category: ModelCategory, ) -> Result<Vec<Model>, OpenRouterError>
Returns a list of models available through the API by category.
§Arguments
category
- The category of the models.
§Returns
Result<Vec<models::Model>, OpenRouterError>
- A list of models or an error.
§Example
let client = OpenRouterClient::builder().api_key("your_api_key").build();
let models = client.list_models_by_category(ModelCategory::TextCompletion).await?;
println!("{:?}", models);
Sourcepub async fn list_models_by_parameters(
&self,
supported_parameters: SupportedParameters,
) -> Result<Vec<Model>, OpenRouterError>
pub async fn list_models_by_parameters( &self, supported_parameters: SupportedParameters, ) -> Result<Vec<Model>, OpenRouterError>
Returns a list of models available for the specified supported parameters.
§Arguments
supported_parameters
- The supported parameters for the models.
§Returns
Result<Vec<models::Model>, OpenRouterError>
- A list of models or an error.
§Example
let client = OpenRouterClient::builder().api_key("your_api_key").build();
let models = client.list_models_by_parameters(SupportedParameters::Tools).await?;
println!("{:?}", models);
Sourcepub async fn list_model_endpoints(
&self,
author: &str,
slug: &str,
) -> Result<EndpointData, OpenRouterError>
pub async fn list_model_endpoints( &self, author: &str, slug: &str, ) -> Result<EndpointData, OpenRouterError>
Returns details about the endpoints for a specific model.
§Arguments
author
- The author of the model.slug
- The slug identifier for the model.
§Returns
Result<models::EndpointData, OpenRouterError>
- The endpoint data or an error.
§Example
let client = OpenRouterClient::builder().api_key("your_api_key").build();
let endpoint_data = client.list_model_endpoints("author_name", "model_slug").await?;
println!("{:?}", endpoint_data);