The Valyu Rust SDK provides comprehensive access to Valyu APIs, enabling you to build powerful AI applications with search, content extraction, and answer generation capabilities.
This SDK is currently in alpha. The API is stable, but some features and interfaces may change based on user feedback.
Features
The SDK includes three core APIs:
- Search API - Advanced search across web and proprietary data sources
- Contents API - Extract and process content from URLs with AI
- Answer API - AI-powered answer generation with search integration
Key SDK features:
- Type-safe API - Full type coverage with serde support for all endpoints
- Async/await - Built on tokio and reqwest for efficient async operations
- Builder pattern - Fluent interface for constructing complex requests
- Comprehensive error handling - Detailed error types for all failure scenarios
Installation
Add this to your Cargo.toml:
[dependencies]
valyu = "0.2"
tokio = { version = "1", features = ["full"] }
Authentication
Get your API key from Valyu Platform (free $10 credits included).
Set up authentication in one of two ways:
Environment Variable (Recommended)
export VALYU_API_KEY="your-api-key-here"
use std::env;
use valyu::ValyuClient;
let api_key = env::var("VALYU_API_KEY").expect("VALYU_API_KEY must be set");
let client = ValyuClient::new(api_key);
Direct API Key
use valyu::ValyuClient;
let client = ValyuClient::new("your-api-key-here");
Using .env File
For local development, use the dotenvy crate:
[dependencies]
dotenvy = "0.15"
use std::env;
use valyu::ValyuClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenvy::dotenv().ok();
let api_key = env::var("VALYU_API_KEY")?;
let client = ValyuClient::new(api_key);
Ok(())
}
Quick Start
Here’s a simple example to get you started with search:
use valyu::ValyuClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ValyuClient::new("your-api-key");
// Basic search example
let response = client.search("What are the latest developments in quantum computing?").await?;
if let Some(results) = &response.results {
println!("Found {} results", results.len());
for result in results {
println!("Title: {}", result.title.as_deref().unwrap_or("Untitled"));
println!("URL: {}", result.url.as_deref().unwrap_or("No URL"));
if let Some(content) = &result.content {
println!("Content preview: {}...", &content[..200.min(content.len())]);
}
}
}
Ok(())
}
Error Handling
The SDK includes built-in error handling with the ValyuError type:
use valyu::{ValyuClient, ValyuError};
#[tokio::main]
async fn main() {
let client = ValyuClient::new("your-api-key");
match client.search("test").await {
Ok(response) => {
if response.success {
println!("Success! Found {} results",
response.results.as_ref().map(|r| r.len()).unwrap_or(0));
} else {
eprintln!("API returned error: {:?}", response.error);
}
}
Err(ValyuError::InvalidApiKey) => eprintln!("Invalid API key provided"),
Err(ValyuError::RateLimitExceeded) => eprintln!("Rate limit exceeded - please retry later"),
Err(ValyuError::ServiceUnavailable) => eprintln!("Service temporarily unavailable"),
Err(ValyuError::InvalidRequest(msg)) => eprintln!("Invalid request: {}", msg),
Err(e) => eprintln!("Error: {}", e),
}
}
Custom HTTP Client
Configure custom timeouts and other HTTP settings:
use valyu::ValyuClient;
use std::time::Duration;
let http_client = reqwest::Client::builder()
.timeout(Duration::from_secs(30))
.build()
.unwrap();
let client = ValyuClient::with_client("your-api-key", http_client);
Next Steps
Explore the detailed documentation for each API:
Support