openrouter_rs/api/
mod.rs

1//! # OpenRouter API Endpoints
2//!
3//! This module provides implementations for all OpenRouter API endpoints,
4//! organized by functionality. Each submodule contains the request/response
5//! types and methods for interacting with specific API endpoints.
6//!
7//! ## 📡 Available Endpoints
8//!
9//! ### Chat Completions ([`chat`])
10//! Modern chat-based API for conversational AI interactions:
11//! - Standard chat completions
12//! - Streaming responses
13//! - Reasoning tokens support
14//! - System/user/assistant message handling
15//!
16//! ```rust
17//! use openrouter_rs::api::chat::{ChatCompletionRequest, Message};
18//! use openrouter_rs::types::Role;
19//!
20//! let request = ChatCompletionRequest::builder()
21//!     .model("anthropic/claude-sonnet-4")
22//!     .messages(vec![Message::new(Role::User, "Hello, world!")])
23//!     .build()?;
24//! # Ok::<(), Box<dyn std::error::Error>>(())
25//! ```
26//!
27//! ### Text Completions ([`completion`])
28//! Legacy text completion API for prompt-based interactions:
29//! - Simple text-in, text-out interface
30//! - Backward compatibility with older applications
31//!
32//! ### Model Information ([`models`])
33//! Retrieve information about available models:
34//! - List all available models
35//! - Filter by category (programming, reasoning, etc.)
36//! - Filter by supported parameters
37//! - Get detailed model specifications
38//!
39//! ```rust
40//! use openrouter_rs::types::ModelCategory;
41//!
42//! // Get all models in the programming category
43//! let models = client.list_models_by_category(ModelCategory::Programming).await?;
44//! # Ok::<(), Box<dyn std::error::Error>>(())
45//! ```
46//!
47//! ### API Key Management ([`api_keys`])
48//! Manage and validate API keys:
49//! - Get current API key information
50//! - List all API keys for account
51//! - Validate key permissions
52//!
53//! ### Credit Management ([`credits`])
54//! Monitor usage and billing:
55//! - Check current credit balance
56//! - View usage statistics
57//! - Track spending by model
58//!
59//! ### Generation Data ([`generation`])
60//! Access detailed generation metadata:
61//! - Token counts and pricing
62//! - Model performance metrics
63//! - Request/response timestamps
64//!
65//! ### Authentication ([`auth`])
66//! Handle authentication and authorization:
67//! - OAuth2 flows
68//! - API key validation
69//! - Permission management
70//!
71//! ### Error Handling ([`errors`])
72//! Structured error responses from the API:
73//! - Rate limiting errors
74//! - Authentication failures
75//! - Model availability issues
76//!
77//! ## 🚀 Quick Examples
78//!
79//! ### Basic Chat
80//! ```rust
81//! use openrouter_rs::{OpenRouterClient, api::chat::*};
82//! use openrouter_rs::types::Role;
83//!
84//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
85//! let client = OpenRouterClient::builder()
86//!     .api_key("your_key")
87//!     .build()?;
88//!
89//! let request = ChatCompletionRequest::builder()
90//!     .model("google/gemini-2.5-flash")
91//!     .messages(vec![Message::new(Role::User, "Hello!")])
92//!     .build()?;
93//!
94//! let response = client.send_chat_completion(&request).await?;
95//! # Ok(())
96//! # }
97//! ```
98//!
99//! ### Model Discovery
100//! ```rust
101//! use openrouter_rs::OpenRouterClient;
102//!
103//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
104//! let client = OpenRouterClient::builder()
105//!     .api_key("your_key")
106//!     .build()?;
107//!
108//! let models = client.list_models().await?;
109//! println!("Found {} models", models.len());
110//! # Ok(())
111//! # }
112//! ```
113//!
114//! ## ⚠️ Error Handling
115//!
116//! All API methods return `Result` types that should be handled appropriately:
117//!
118//! ```rust
119//! use openrouter_rs::error::OpenRouterError;
120//!
121//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
122//! match client.send_chat_completion(&request).await {
123//!     Ok(response) => println!("Success: {:?}", response),
124//!     Err(OpenRouterError::RateLimitExceeded) => {
125//!         println!("Rate limit hit, retrying later...");
126//!     }
127//!     Err(OpenRouterError::InvalidApiKey) => {
128//!         println!("Check your API key configuration");
129//!     }
130//!     Err(e) => println!("Other error: {}", e),
131//! }
132//! # Ok(())
133//! # }
134//! ```
135
136pub mod api_keys;
137pub mod auth;
138pub mod chat;
139pub mod completion;
140pub mod credits;
141pub mod errors;
142pub mod generation;
143pub mod models;