Portkey SDK
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.
Features
- Complete API Coverage: Support for all Portkey API endpoints
- Type Safety: Strongly typed models with comprehensive validation
- Async/Await: Built on modern async Rust with
tokioandreqwest
Installation
Add this to your Cargo.toml:
[]
= { = "1.0", = ["macros", "rt-multi-thread"] }
= { = "0.2", = [] }
Quick Start
Builder Configuration
use ;
use Duration;
async
Environment Variables
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 ;
async
Optional Features
TLS Backend
Choose between two TLS implementations:
# Default: rustls-tls (recommended)
= { = "0.2", = [] }
# Alternative: native-tls
= { = "0.2", = ["native-tls"], = false }
Tracing Support
Enable comprehensive logging and tracing via the tracing crate.
Tracing targets are defined in lib.rs for fine-grained control over log output:
= { = "0.2", = ["tracing"] }
JSON Schema Support for Structured Outputs
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:
= { = "0.2", = ["schema"] }
= { = "0.8", = ["derive"] }
See the structured outputs example for a complete working example.
Examples
The examples/ directory contains usage examples:
# Set your API key
# Run the chat completion example
# Run the embeddings example
# Run the structured outputs example (requires schema feature)
Contributing
Contributions are welcome! Please read our Contributing Guide for details on how to submit pull requests, report issues, and contribute to the project.
License
This project is licensed under the MIT License - see the LICENSE.txt file for details.