OpenRouter API Client Library
OpenRouter API Client Library is a Rust client for interfacing with the OpenRouter API. The library is designed to be modular, type‑safe, and intuitive. It uses a type‑state builder pattern for configuring and validating the client at compile time, ensuring that all required configuration (such as setting the base URL and API key) happens before attempting a request.
Note: This project is still in development. Many features are planned but not yet fully implemented.
Features
- Modular Organization: Organized into clear modules for models, API endpoints, common types, and utilities.
- Type‑State Builder: Guarantees compile‑time validation of client configuration (e.g. base URL, API key, custom headers) for a robust development experience.
- HTTP Integration: Uses reqwest with rustls‑tls for secure asynchronous HTTP requests.
- Robust Error Handling: Centralized error management using the
thiserror
crate ensures consistent error types across the library. - Streaming Support: Now supports streaming chat completions via Server-Sent Events (SSE). The library gracefully skips over comment lines and non‑JSON payloads, letting you update UIs in real‑time.
- Structured Outputs: Optionally request structured responses with JSON Schema validation so that responses strictly follow your defined schema.
- Tool Calling Capability: Define function‑type tools that the model can invoke. Supports concurrent tool calls in a single response with proper validation against expected formats.
- Provider Preferences & Routing: Configure model fallbacks, routing preferences, and provider filtering via a strongly‑typed interface.
- Web Search Endpoint: Easily perform web search queries with type‑safe request and response models.
- Future Roadmap:
- Full Streaming support for real‑time chat completions (currently available for chat yet to be fully integrated in all endpoints).
- Text completion endpoint.
- Endpoints for credits, generation metadata, and available models.
- Extended tests, enhanced documentation, and CI integration.
Getting Started
Installation
Add the following to your project's Cargo.toml
:
[]
= { = "https://github.com/yourusername/openrouter_api.git", = "main" }
Ensure that you have Rust installed (tested with Rust v1.83.0) and that you're using Cargo for building and testing.
Example Usage
Minimal Chat Example
use ;
use ;
async
Minimal Web Search Example
use ;
use ;
async
Streaming Chat Example
The library now supports streaming chat completions via SSE:
use ;
use ;
use StreamExt;
async
Tool Calling & Structured Outputs Example
This example demonstrates how to include tool calling information and request structured output validation using a JSON Schema.
use ;
use json;
async
Running Tests
Before running tests, set the OPENROUTER_API_KEY
environment variable to your API key:
For verbose output:
Implementation Plan
The project is under active development. The roadmap outlines upcoming features and milestones:
Phase 1: Core Functionality (Completed/In Progress)
- Client Framework:
- Type‑state builder pattern for configuration with compile‑time validations.
- Custom headers and robust error propagation.
- Chat Completion Endpoint:
- Synchronous chat completions with JSON decoding and streaming support.
- Core Data Models:
- Definitions for chat messages, requests, responses, and usage.
Phase 2: Additional Endpoints and Features
- Streaming Support:
- Streaming API for chat completions via Server‑Sent Events (SSE).
- Web Search Endpoint:
- New endpoint for web search queries with strongly‑typed request/response models.
- Tool Calling & Structured Outputs:
- Support for invoking callable functions and validating structured responses via JSON Schema.
- Provider Preferences & Routing:
- Configuration options for model fallbacks, routing, and provider filtering.
Phase 3: Robust Testing & Documentation
- Test Coverage:
- Expand unit and integration tests, including streaming-specific tests.
- Documentation Improvements:
- Enhance inline documentation, API docs, and usage examples in the
/examples
directory.
- Enhance inline documentation, API docs, and usage examples in the
- Continuous Integration (CI):
- Set up CI pipelines for automated builds and tests.
Contributing
Contributions are welcome! Please open an issue or submit a pull request with your ideas or fixes. Follow the code style guidelines and ensure that all tests pass.
License
Distributed under either the MIT license or the Apache License, Version 2.0. See LICENSE for details.