63 releases (22 breaking)
| new 0.23.2 | Aug 10, 2026 |
|---|---|
| 0.22.0 | Aug 5, 2026 |
| 0.21.0 | Jul 20, 2026 |
| 0.5.0 | Mar 27, 2026 |
| 0.1.3 | Apr 29, 2021 |
#2 in #capture
208,760 downloads per month
Used in 9 crates
(8 directly)
735KB
15K
SLoC
Official Rust SDK for PostHog.
Use client to construct a Client, Event to capture analytics
events, and Client::evaluate_flags with EvaluateFlagsOptions for
feature flag evaluation.
See the PostHog Rust SDK documentation for installation, configuration, and more examples.
Getting started
Add posthog-rs to your Cargo.toml, then initialize a client with your
project API key.
use posthog_rs::{client, EvaluateFlagsOptions, Event};
#[cfg(feature = "async-client")]
#[tokio::main]
async fn main() -> Result<(), posthog_rs::Error> {
let api_key = std::env::var("POSTHOG_API_KEY")
.expect("set POSTHOG_API_KEY to your PostHog project API key");
let posthog = client(api_key.as_str()).await;
let distinct_id = "user-123";
// Capture an analytics event.
let mut event = Event::new("signed_up", distinct_id);
event.insert_prop("plan", "pro")?;
posthog.capture(event);
// Evaluate feature flags once, then read from the snapshot.
let flags = posthog
.evaluate_flags(distinct_id, EvaluateFlagsOptions::default())
.await?;
if flags.is_enabled("new-onboarding") {
let mut event = Event::new("onboarding_step_completed", distinct_id);
event.with_flags(&flags.only_accessed());
posthog.capture(event);
}
Ok(())
}
#[cfg(not(feature = "async-client"))]
fn main() -> Result<(), posthog_rs::Error> {
let api_key = std::env::var("POSTHOG_API_KEY")
.expect("set POSTHOG_API_KEY to your PostHog project API key");
let posthog = client(api_key.as_str());
let distinct_id = "user-123";
// Capture an analytics event.
let mut event = Event::new("signed_up", distinct_id);
event.insert_prop("plan", "pro")?;
posthog.capture(event);
// Evaluate feature flags once, then read from the snapshot.
let flags = posthog.evaluate_flags(distinct_id, EvaluateFlagsOptions::default())?;
if flags.is_enabled("new-onboarding") {
let mut event = Event::new("onboarding_step_completed", distinct_id);
event.with_flags(&flags.only_accessed());
posthog.capture(event);
}
Ok(())
}
Capture is fire-and-forget
Client::capture and Client::capture_batch are the primary API: they
hand the event to a background worker that batches, sends, and retries it,
and return immediately. Delivery failures are not surfaced to the caller —
register ClientOptionsBuilder::on_error to observe them. This is the
right choice for essentially all analytics.
Immediate delivery (advanced)
Client::capture_immediate and Client::capture_batch_immediate send
inline and return a CaptureSummary once the request reaches a terminal
outcome (or an Error if the retry budget is spent). They bypass the
background worker and do not fire on_error — the returned value is the
signal. Reach for them only when the caller must know a batch persisted
before advancing its own durable state (for example, a server-side importer
committing an upstream offset); prefer fire-and-forget everywhere else.
PostHog Rust
The official Rust SDK for PostHog. See the main PostHog docs for more information.
SDK usage examples and code snippets live in the official documentation so they stay up to date.
Documentation
Contributing
See CONTRIBUTING.md for local setup and test instructions.
Releasing
See RELEASING.md.
Dependencies
~25–49MB
~807K SLoC