statsig-client
A Rust client for Statsig's feature flag and experimentation platform. Built for production use with proper error handling, caching, and batch processing.
Why this client?
A reliable, type-safe Rust client for Statsig designed for client-side applications. Perfect for:
-
Desktop and mobile apps
-
Edge functions and middleware
-
Anywhere you need client-side feature flags
-
Works with client keys (like the JS SDK)
-
Handles network failures gracefully with retries
-
Caches responses to reduce API calls and latency
-
Validates inputs before they hit the wire
-
Supports batch operations for better performance
-
Uses builder patterns for clean, readable code
Installation
[]
= "0.1.0"
= { = "1", = ["rt-multi-thread", "macros"] }
Getting Started
use ;
async
Configuration
Tweak the client behavior to fit your needs:
use ;
use Duration;
let config = builder
.api_key
.timeout // Request timeout
.cache_ttl // Cache for 5 minutes
.cache_max_capacity // Max cached items
.retry_attempts // Retry failed requests
.retry_delay // Delay between retries
.build;
let client = with_config.await?;
Rich User Context
Add custom data to target your features better:
let user = builder
.user_id
.email
.country
.custom
.private_attributes
.build?;
Dynamic Configs
Fetch configuration values:
let config = client.get_config.await?;
let theme = config.get.and_then.unwrap_or;
let max_items = config.get.and_then.unwrap_or;
println!;
Event Tracking
Log user actions for analytics:
// Simple event
client.log_event.await?;
// Event with metadata
use HashMap;
let mut metadata = new;
metadata.insert;
metadata.insert;
client.log_event_with_metadata.await?;
Batch Operations
Check multiple flags at once to reduce API calls:
let gates = client.check_gates.await?;
for in gates
Error Handling
Things go wrong. Here's how to handle it:
match client.check_gate.await
What Gets Cached?
The client caches responses to reduce latency and API costs:
- Feature gate results (default: 5 minutes)
- Dynamic config values (default: 5 minutes)
- Cache keys include user hash + entity name
- Automatic cache invalidation on errors
Performance Tips
- Batch your checks - Use
check_gates()instead of multiplecheck_gate()calls - Reuse the client - Create one client and share it across your app
- Adjust cache TTL - Longer TTLs reduce API calls but may delay updates
- Set appropriate timeouts - Balance between reliability and responsiveness
License
MIT OR Apache-2.0 - pick whichever works for you.
Contributing
Found a bug or want to add something? Open an issue or send a PR.