Skip to Content

Rust SDK

GoGreen feature flag and experimentation SDK for Rust. Evaluate flags, stream real-time updates, and track events with an async API.

Requirements

  • Rust 1.70 or later
  • Tokio async runtime

Installation

Add to your Cargo.toml:

[dependencies] gogreen-sdk = "0.1"

Or from a git dependency (until published to crates.io):

[dependencies] gogreen-sdk = { git = "https://github.com/MarkDurbin104/GoGreen", path = "sdk/rust" }

Quick start

use gogreen_sdk::{Client, Config, EvaluationContext}; #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { let config = Config::new( "your-sdk-key", "your-environment-id", EvaluationContext::new("user-123"), ) .api_base_url("https://app.gogreenflags.com"); let client = Client::new(config).await?; // Wait for initial config (e.g. up to 10 seconds) client.wait_for_initialization(std::time::Duration::from_secs(10)).await; // Evaluate flags let show_new_ui = client.bool_variation("new-ui", false).await; let plan = client.string_variation("plan", "free").await; let discount = client.number_variation("discount", 0.0).await; // With context override let ctx = EvaluationContext::new("other-user").with_attribute("country", "US"); let value = client.bool_variation_detail("feature", true, Some(&ctx)).await; // Track events for experimentation client.track("purchase", None).await; let mut props = std::collections::HashMap::new(); props.insert("amount".to_string(), serde_json::json!(99.99)); client.track("purchase", Some(props)).await; client.close().await; Ok(()) }

Configuration

OptionDescriptionDefault
sdk_keySDK key for authenticationrequired
env_idEnvironment IDrequired
default_contextDefault evaluation context (user key + attributes)required
api_base_urlEvaluation API base URLhttp://localhost:8092
stream_urlSSE stream URLsame as api_base_url
events_urlEvents ingestion URLsame as api_base_url
enable_streamingReal-time SSE updatestrue
offlineOffline mode (no network)false
request_timeoutHTTP request timeout10s
flush_intervalEvent batch flush interval30s
bootstrapPreloaded flag values (for testing/offline)none

API reference

Client

  • Client::new(config) — Create a client (async). Fetches initial config unless offline or bootstrap is set.
  • wait_for_initialization(timeout) — Block until initial config is ready or timeout.
  • bool_variation(key, default) / bool_variation_detail(key, default, context) — Boolean flag.
  • string_variation(key, default) / string_variation_detail(...) — String flag.
  • number_variation(key, default) / number_variation_detail(...) — Number (f64) flag.
  • json_variation(key, default) / json_variation_detail(...) — JSON value flag.
  • set_context(ctx) / identify(ctx) — Set evaluation context.
  • track(event_key, properties) — Track a custom event for experimentation.
  • enter_offline_mode() / exit_offline_mode() — Toggle offline mode.
  • close() — Flush events and stop streaming (call before drop when possible).
  • Client::test_client(flags) — Create an offline client with fixed flag values for testing.

EvaluationContext

  • EvaluationContext::new(key) — Context with only a key.
  • with_attribute(k, v) / with_custom(map) — Add custom attributes for targeting.

Testing

let client = Client::test_client([ ("my-flag", serde_json::json!(true)), ("plan", serde_json::json!("pro")), ]).await?; assert!(client.bool_variation("my-flag", false).await); assert_eq!(client.string_variation("plan", "free").await, "pro"); client.close().await;

Building and testing from source

cd sdk/rust cargo build cargo test