| Crates.io | portkey-sdk |
| lib.rs | portkey-sdk |
| version | 0.2.0 |
| created_at | 2025-12-08 05:31:09.526269+00 |
| updated_at | 2025-12-09 16:53:53.424791+00 |
| description | Unofficial Rust SDK for Portkey AI Gateway: route, observe, and optimize LLM requests across 200+ providers |
| homepage | https://github.com/martsokha/portkey |
| repository | https://github.com/martsokha/portkey |
| max_upload_size | |
| id | 1972766 |
| size | 385,897 |
A Rust client library for the Portkey AI Gateway. This SDK provides a type-safe, ergonomic interface for managing AI gateway operations, chat completions, embeddings, images, audio, and analytics.
tokio and reqwestAdd this to your Cargo.toml:
[dependencies]
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
portkey-sdk = { version = "0.2", features = [] }
use portkey_sdk::{builder::AuthMethod, PortkeyConfig, Result};
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<()> {
let client = PortkeyConfig::builder()
.with_api_key("your-portkey-api-key")
.with_auth_method(AuthMethod::virtual_key("your-virtual-key"))
.with_base_url("https://api.portkey.ai/v1")
.with_timeout(Duration::from_secs(60))
.build_client()?;
// Use the client for API calls...
Ok(())
}
The SDK can be configured using environment variables:
| Variable | Required | Default | Description |
|---|---|---|---|
PORTKEY_API_KEY |
Yes | - | Your Portkey API key from console |
PORTKEY_BASE_URL |
No | https://api.portkey.ai/v1 |
Custom API base URL |
PORTKEY_TIMEOUT_SECS |
No | 30 |
Request timeout in seconds (max: 300) |
PORTKEY_VIRTUAL_KEY |
No | - | Virtual key for routing |
PORTKEY_TRACE_ID |
No | - | Trace ID for request tracking |
PORTKEY_CACHE_NAMESPACE |
No | - | Cache namespace for response caching |
PORTKEY_CACHE_FORCE_REFRESH |
No | false |
Force refresh cached responses |
use portkey_sdk::{PortkeyClient, Result};
#[tokio::main]
async fn main() -> Result<()> {
let client: PortkeyClient = PortkeyClient::from_env()?;
Ok(())
}
Choose between two TLS implementations:
# Default: rustls-tls (recommended)
portkey-sdk = { version = "0.2", features = [] }
# Alternative: native-tls
portkey-sdk = { version = "0.2", features = ["native-tls"], default-features = false }
Enable comprehensive logging and tracing via the tracing crate.
Tracing targets are defined in lib.rs for fine-grained control over log output:
portkey-sdk = { version = "0.2", features = ["tracing"] }
Enable JSON Schema support via the schemars crate to use structured outputs with custom types.
This feature provides helper methods to create JSON schema configurations and parse structured responses:
portkey-sdk = { version = "0.2", features = ["schema"] }
schemars = { version = "0.8", features = ["derive"] }
See the structured outputs example for a complete working example.
The examples/ directory contains usage examples:
# Set your API key
export PORTKEY_API_KEY="your-api-key"
export PORTKEY_VIRTUAL_KEY="your-virtual-key"
# Run the chat completion example
cargo run --example chat_completion
# Run the embeddings example
cargo run --example embeddings
# Run the structured outputs example (requires schema feature)
cargo run --example structured_outputs --features schema
Contributions are welcome! Please read our Contributing Guide for details on how to submit pull requests, report issues, and contribute to the project.
This project is licensed under the MIT License - see the LICENSE.txt file for details.