Struct OpenRouterClient

Source
pub struct OpenRouterClient { /* private fields */ }

Implementations§

Source§

impl OpenRouterClient

Source

pub fn builder() -> OpenRouterClientBuilder

Source

pub fn set_api_key(&mut self, api_key: impl Into<String>)

Sets the API key after client construction.

§Arguments
  • api_key - The API key to set
§Example
let mut client = OpenRouterClient::builder().build();
client.set_api_key("your_api_key");
Source

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();
Source

pub fn set_provisioning_key(&mut self, provisioning_key: impl Into<String>)

Sets the provisioning key after client construction.

§Arguments
  • provisioning_key - The provisioning key to set
§Example
let mut client = OpenRouterClient::builder().build();
client.set_provisioning_key("your_provisioning_key");
Source

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();
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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),
    }
}
Source

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);
Source

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);
Source

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);
Source

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);
Source

pub async fn list_models(&self) -> Result<Vec<Model>, OpenRouterError>

Returns a list of models available through the API.

§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().await?;
println!("{:?}", models);
Source

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);
Source

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);
Source

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);

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T