> ## Documentation Index
> Fetch the complete documentation index at: https://docs.valyu.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Rust Quickstart

> Everything you need to use Valyu in Rust

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.

<Note>
  This SDK is currently in **alpha**. The API is stable, but some features and interfaces may change based on user feedback.
</Note>

## Features

The SDK includes three core APIs:

* **[Search API](rust-sdk/search)** - Advanced search across web and proprietary data sources
* **[Contents API](rust-sdk/contents)** - Extract and process content from URLs with AI
* **[Answer API](rust-sdk/answer)** - 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`:

```toml theme={null}
[dependencies]
valyu = "0.2"
tokio = { version = "1", features = ["full"] }
```

## Authentication

Get your API key from [Valyu Platform](https://platform.valyu.ai) (free \$10 credits included).

Set up authentication in one of two ways:

### Environment Variable (Recommended)

```bash theme={null}
export VALYU_API_KEY="your-api-key-here"
```

```rust theme={null}
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

```rust theme={null}
use valyu::ValyuClient;

let client = ValyuClient::new("your-api-key-here");
```

### Using .env File

For local development, use the `dotenvy` crate:

```toml theme={null}
[dependencies]
dotenvy = "0.15"
```

```rust theme={null}
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:

```rust theme={null}
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:

```rust theme={null}
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:

```rust theme={null}
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:

<CardGroup cols={2}>
  <Card title="Search API" icon="magnifying-glass" href="rust-sdk/search">
    Advanced search across web and proprietary sources
  </Card>

  <Card title="Contents API" icon="book" href="rust-sdk/contents">
    Extract and process web content with AI
  </Card>

  <Card title="Answer API" icon="messages" href="rust-sdk/answer">
    Generate AI-powered answers with search
  </Card>
</CardGroup>

## Support

* **Discord**: [Join our community](https://discord.gg/umtmSsppRY)
* **GitHub**: [valyuAI/valyu-rust](https://github.com/valyuAI/valyu-rust)
* **Crate Documentation**: [docs.rs/valyu](https://docs.rs/valyu)
* **Support**: [contact@valyu.ai](mailto:contact@valyu.ai)
