Module api

Source
Expand description

§OpenRouter API Endpoints

This module provides implementations for all OpenRouter API endpoints, organized by functionality. Each submodule contains the request/response types and methods for interacting with specific API endpoints.

§📡 Available Endpoints

§Chat Completions (chat)

Modern chat-based API for conversational AI interactions:

  • Standard chat completions
  • Streaming responses
  • Reasoning tokens support
  • System/user/assistant message handling
use openrouter_rs::api::chat::{ChatCompletionRequest, Message};
use openrouter_rs::types::Role;

let request = ChatCompletionRequest::builder()
    .model("anthropic/claude-sonnet-4")
    .messages(vec![Message::new(Role::User, "Hello, world!")])
    .build()?;

§Text Completions (completion)

Legacy text completion API for prompt-based interactions:

  • Simple text-in, text-out interface
  • Backward compatibility with older applications

§Model Information (models)

Retrieve information about available models:

  • List all available models
  • Filter by category (programming, reasoning, etc.)
  • Filter by supported parameters
  • Get detailed model specifications
use openrouter_rs::types::ModelCategory;

// Get all models in the programming category
let models = client.list_models_by_category(ModelCategory::Programming).await?;

§API Key Management (api_keys)

Manage and validate API keys:

  • Get current API key information
  • List all API keys for account
  • Validate key permissions

§Credit Management (credits)

Monitor usage and billing:

  • Check current credit balance
  • View usage statistics
  • Track spending by model

§Generation Data (generation)

Access detailed generation metadata:

  • Token counts and pricing
  • Model performance metrics
  • Request/response timestamps

§Authentication (auth)

Handle authentication and authorization:

  • OAuth2 flows
  • API key validation
  • Permission management

§Error Handling (errors)

Structured error responses from the API:

  • Rate limiting errors
  • Authentication failures
  • Model availability issues

§🚀 Quick Examples

§Basic Chat

use openrouter_rs::{OpenRouterClient, api::chat::*};
use openrouter_rs::types::Role;

let client = OpenRouterClient::builder()
    .api_key("your_key")
    .build()?;

let request = ChatCompletionRequest::builder()
    .model("google/gemini-2.5-flash")
    .messages(vec![Message::new(Role::User, "Hello!")])
    .build()?;

let response = client.send_chat_completion(&request).await?;

§Model Discovery

use openrouter_rs::OpenRouterClient;

let client = OpenRouterClient::builder()
    .api_key("your_key")
    .build()?;

let models = client.list_models().await?;
println!("Found {} models", models.len());

§⚠️ Error Handling

All API methods return Result types that should be handled appropriately:

use openrouter_rs::error::OpenRouterError;

match client.send_chat_completion(&request).await {
    Ok(response) => println!("Success: {:?}", response),
    Err(OpenRouterError::RateLimitExceeded) => {
        println!("Rate limit hit, retrying later...");
    }
    Err(OpenRouterError::InvalidApiKey) => {
        println!("Check your API key configuration");
    }
    Err(e) => println!("Other error: {}", e),
}

Modules§

api_keys
auth
chat
completion
credits
errors
generation
models