Serper SDK
A minimalistic yet ergonomic Rust SDK for the Serper Google Search API. Built with a focus on type safety, modularity, and developer experience.
Features
- ๐ Type-safe API with comprehensive error handling
- ๐๏ธ Modular architecture with clear separation of concerns
- โก Async-first design with concurrent request support
- ๐ง Flexible configuration with environment variable support
- ๐งช Comprehensive testing with extensive test coverage
- ๐ Rich documentation with examples and API references
Quick Start
1. Get Your API Key
First, sign up at serper.dev to get your free API key. The service offers generous free tier limits.
2. Add to Your Project
Add this to your Cargo.toml:
[]
= "0.1.0"
= { = "1.0", = ["full"] }
Basic usage:
use ;
async
Architecture
The SDK is organized into focused modules with clear responsibilities:
core- Fundamental types and error handlingsearch- Query construction and response parsinghttp- Transport layer and HTTP client functionalityconfig- Configuration management with environment variable supportutils- Common utilities and validation helpers
For detailed architecture information, see ARCHITECTURE.md.
Examples
Advanced Query Building
use ;
let service = new?;
// Using SearchQueryBuilder
let query = new
.query
.location
.country
.language
.page
.num_results
.build?;
let response = service.search.await?;
Builder Pattern Search
let response = service.search_with.await?;
Concurrent Searches
let queries = vec!;
// Execute up to 3 searches concurrently
let results = service.search_concurrent.await?;
for in results.iter.enumerate
Custom Configuration
use ;
use Duration;
// Using service builder
// Option 1: Using environment variables (recommended)
let config = from_env?;
let service = new?;
// Option 2: Manual configuration
let api_key = var?;
let service = new
.api_key
.timeout
.user_agent
.build?;
// Using configuration object
let api_key = var?;
let config = new
.with_timeout
.with_max_concurrent
.with_logging;
Environment Variables
Set environment variables:
Then use in your code:
use SdkConfig;
let config = from_env?;
// Configuration loaded from environment variables
Processing Different Result Types
let response = service.search.await?;
// Direct answers
if let Some = &response.answer_box
// Knowledge graph
if let Some = &response.knowledge_graph
// Organic results
for result in response.organic_results
Error Handling
The SDK provides comprehensive error handling with specific error types:
use SerperError;
match service.search.await
Response Types
The SDK provides rich response types for different result categories:
Organic Results
Answer Box
Knowledge Graph
Configuration Options
SdkConfig Fields
| Field | Type | Default | Description |
|---|---|---|---|
api_key |
String |
Required | Serper API key |
base_url |
String |
"https://google.serper.dev" |
API base URL |
timeout |
Duration |
30s |
Request timeout |
max_concurrent_requests |
usize |
5 |
Max concurrent requests |
default_headers |
HashMap<String, String> |
{"Content-Type": "application/json"} |
Default headers |
user_agent |
String |
"serper-sdk/{version}" |
User agent string |
enable_logging |
bool |
false |
Enable request/response logging |
Environment Variables
| Variable | Description | Default |
|---|---|---|
SERPER_API_KEY |
API key (required) | None |
SERPER_BASE_URL |
API base URL | "https://google.serper.dev" |
SERPER_TIMEOUT_SECS |
Timeout in seconds | 30 |
SERPER_MAX_CONCURRENT |
Max concurrent requests | 5 |
SERPER_USER_AGENT |
Custom user agent | "serper-sdk/{version}" |
SERPER_ENABLE_LOGGING |
Enable logging ("true"/"false") |
false |
Testing
Run the test suite:
# Run all tests
# Run specific module tests
# Run with output
# Run integration tests
The SDK includes comprehensive tests:
- 54 total tests across all modules
- Unit tests for individual module functionality
- Integration tests for module interactions
- Edge case tests for error conditions and boundary cases
Documentation
API Documentation
Generate and view the API documentation:
Module Documentation
Detailed documentation for each module is available in the docs/modules/ directory:
- Core Module - Fundamental types and error handling
- Search Module - Query construction and response handling
- HTTP Module - Transport layer and HTTP client
- Config Module - Configuration management
- Utils Module - Common utilities and helpers
Architecture Documentation
- Architecture Overview - Complete architecture documentation
- Module Dependencies - Module relationships and dependencies
Contributing
We welcome contributions! Please see our Contributing Guide for details.
Development Setup
- Clone the repository:
- Install dependencies:
- Run tests:
- Check formatting:
- Run clippy:
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
See CHANGELOG.md for a detailed history of changes.
Documentation
- ๐ Primary Documentation - Complete API reference and examples on GitHub Pages
- ๐ docs.rs Mirror - Alternative API reference on docs.rs
- ๐ง Local Documentation - Generate locally with
cargo doc --open
Support
- ๐ Issue Tracker
- ๐ฌ Discussions
Author
This SDK is created and maintained by Hamze Ghalebi, CTO at Remolab. Remolab is a technology company focused on innovative software solutions and API integrations.
Related Projects
- Serper API - The official Serper Google Search API
- reqwest - The HTTP client used by this SDK
- serde - Serialization framework used for JSON handling