This directory contains example applications demonstrating how to use the PostHog Rust SDK, including feature flags, local evaluation, configuration, and manual error tracking.
Basic examples of all feature flag operations:
# With a real PostHog API key
export POSTHOG_API_TOKEN=phc_your_key
cargo run --example feature_flags --features async-client
# Without API key (runs in demo mode)
cargo run --example feature_flags --features async-clientShows:
- Boolean feature flag checks
- A/B test variants (multivariate flags)
- Property-based targeting
- Batch flag evaluation
- Feature flag payloads
Demonstrates local flag evaluation for high-performance use cases:
export POSTHOG_API_TOKEN=phc_your_project_key
export POSTHOG_PERSONAL_API_TOKEN=phx_your_personal_key
cargo run --example local_evaluation --features async-clientShows:
- Performance comparison (API vs local evaluation)
- Setting up local evaluation with polling
- Batch flag evaluation
Get a personal API key at: https://app.posthog.com/me/settings
Shows different ways to configure the SDK:
cargo run --example advanced_config --features async-clientShows:
- Basic client setup (US region)
- EU region configuration (GDPR compliance)
- Self-hosted instance configuration
- Production settings with timeouts and geoip configuration
- High-performance local evaluation setup
- Observability with
on_error(terminal capture/flags/poller failures)
Demonstrates manual error tracking capture:
export POSTHOG_API_KEY=phc_your_project_key
cargo run --example error_trackingShows:
- Capturing a Rust error as a PostHog Error Tracking event
- Attaching a distinct ID
- Adding custom exception properties
export POSTHOG_API_TOKEN=phc_your_project_key
cargo run --example custom_http_clients
# Blocking SDK:
cargo run --example custom_http_clients --no-default-featuresUse ClientOptionsBuilder::http_client(reqwest::Client) for async immediate
capture, remote flags, and local-evaluation polling. Use
blocking_http_client(reqwest::blocking::Client) for the blocking SDK and
background capture in both SDK modes. The async setter is only available
with async-client. Either option can be omitted; that path keeps SDK-created
clients and existing defaults.
Supplied clients retain their connection pools, TLS configuration, proxies, and
other client settings. Pass clones to share pools with your application. Use a
compatible version of the SDK's reqwest dependency (currently 0.13); other HTTP
libraries and incompatible reqwest versions are not accepted.
Only supply clients whose default headers and cookies are safe to send to PostHog.
Do not reuse clients carrying Authorization, API keys, or other credentials for
unrelated services. Reqwest does not expose those defaults for the SDK to inspect
or filter. Use a separate client with safe defaults when necessary.
The SDK applies both timeout options even with custom clients:
request_timeout_seconds(default: 30 seconds) applies to immediate capture, background capture, and initial and periodic local-evaluation requests.feature_flags_request_timeout_seconds(default: 3 seconds) applies to remote/flagsrequests.
These options replace the client's request timeout, even if the client's timeout is shorter. Other settings, such as connect timeouts, remain configured on the client. Set the SDK options to customize PostHog request timeouts.
During background shutdown draining, each request is additionally capped at the remaining shutdown deadline. An already-running request can still delay shutdown until its SDK request timeout expires. SDK authentication, request headers, batching, and retries remain in place; account for any additional retries configured by your application.
Construct and finally drop application-owned blocking clients (including copies
held in options/builders) outside an async runtime, or in spawn_blocking. The
SDK releases its own blocking-client handles off the async runtime. Shutting down
the SDK does not invalidate client clones retained by the application.
Custom HTTP clients are separate from TLS crypto-provider selection. The
tls-no-provider feature proposed in #245 is on v1, not this main branch;
changing the provider does not change the reqwest client types.
An MCP server over stdio, built on rmcp, whose tool calls are captured as $mcp_* events:
export POSTHOG_API_KEY=phc_your_project_key
cargo run --example mcp_server --features mcpPoint an MCP client at that command. Shows:
- Instrumenting an
rmcpserver in one call - The
get_more_toolsvirtual tool
Sends one email, then reads its status:
export POSTHOG_API_KEY=phc_your_project_key
export POSTHOG_SECRET_KEY=phs_your_project_secret_key # with transactional_email:write
export EMAIL_TO=you@example.com
# Optional: a template made transactional in PostHog. Without it, the
# example sends HTML it rendered itself.
export POSTHOG_EMAIL_TEMPLATE_ID=your_template_uuid
cargo run --example send_emailShows:
- Sending a template made in PostHog with
TemplateEmail - Sending content your code rendered with
CodeEmail - Reading the email's delivery status
-
Boolean Flags: Simple on/off toggles
FlagValue::Boolean(true) // enabled FlagValue::Boolean(false) // disabled
-
Multivariate Flags: Multiple variants for A/B/n testing
FlagValue::String("control") FlagValue::String("variant-a") FlagValue::String("variant-b")
- Remote Evaluation: Calls PostHog API for the latest flag values
- Local Evaluation: Uses cached flag definitions for faster evaluation (requires personal API key)
- Person Properties: User attributes (country, plan, age, etc.)
- Group Properties: Organization/team attributes for B2B apps
- Feature Rollouts: Gradually release features to users
- A/B Testing: Test different variants to measure impact
- User Targeting: Enable features for specific user segments
- Kill Switches: Quickly disable problematic features
- Beta Programs: Give early access to beta users