bosshogg 2026.5.3

BossHogg — the agent-first PostHog CLI. Feature flags, HogQL queries, insights, dashboards, cohorts, persons, events, experiments, and more — from the terminal or from a Claude Code / Cursor / other coding-agent loop. Ships with a Claude Code skill (~200 idle tokens) that teaches models how to use it.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// src/commands/capture.rs
//! `bosshogg capture` — event / batch / identify.
//!
//! IMPORTANT: Capture uses the public PostHog ingest API, NOT the personal API.
//! Authentication is `api_key` in the request body (phc_ project token), not
//! an Authorization header.
//!
//! The project_token (phc_) is read from the active context.
//! All subcommands are gated on `yes || confirm` because they post real events.

use std::path::PathBuf;
use std::time::Duration;

use clap::{Args, Subcommand};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};

use reqwest::header::{HeaderMap, HeaderValue, USER_AGENT};

use crate::commands::util::{gate_destructive, read_json_file};
use crate::config;
use crate::error::{BosshoggError, Result};
use crate::output;

// ── Typed response ────────────────────────────────────────────────────────────

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct CaptureResponse {
    pub status: i32,
}

// ── Clap tree ─────────────────────────────────────────────────────────────────

#[derive(Args, Debug)]
pub struct CaptureArgs {
    #[command(subcommand)]
    pub command: CaptureCommand,
}

#[derive(Subcommand, Debug)]
pub enum CaptureCommand {
    /// Send a single event to PostHog (uses project token, not personal key).
    ///
    /// WARNING: this posts real events to your PostHog project.
    Event {
        /// Event name.
        #[arg(long)]
        event: String,
        /// Distinct ID of the user/actor.
        #[arg(long)]
        distinct_id: String,
        /// Optional path to a JSON file with event properties.
        #[arg(long)]
        properties_file: Option<PathBuf>,
    },
    /// Send a batch of events from a JSONL file (one JSON object per line).
    ///
    /// WARNING: this posts real events to your PostHog project.
    Batch {
        /// Path to a JSONL file where each line is an event object.
        #[arg(long)]
        file: PathBuf,
    },
    /// Send an identify call to set person properties.
    ///
    /// WARNING: this posts real events to your PostHog project.
    Identify {
        /// Distinct ID to identify.
        #[arg(long)]
        distinct_id: String,
        /// Path to a JSON file with person properties.
        #[arg(long)]
        properties_file: PathBuf,
    },
}

// ── Dispatch ──────────────────────────────────────────────────────────────────

pub async fn execute(
    args: CaptureArgs,
    json_mode: bool,
    debug: bool,
    context: Option<&str>,
    yes: bool,
) -> Result<()> {
    // Load config to get project_token + host from the active context.
    let cfg = config::load()?;
    let ctx_name: &str = if let Some(c) = context {
        c
    } else {
        cfg.current_context
            .as_deref()
            .ok_or_else(|| BosshoggError::Config("no active context".into()))?
    };

    let ctx = cfg
        .contexts
        .get(ctx_name)
        .ok_or_else(|| BosshoggError::Config(format!("context `{ctx_name}` not found")))?;

    let project_token = ctx
        .project_token
        .clone()
        .ok_or_else(|| BosshoggError::Config(
            "no project_token (phc_) on current context — set via `bosshogg config set-context --project-token phc_...`".into()
        ))?;

    let host = ctx.host.trim_end_matches('/').to_string();

    // Build a minimal reqwest client — no Authorization header (capture API
    // takes project token in the body). HTTPS-only is enforced for Cloud;
    // self-hosted users opt into plaintext via per-context `allow_http=true`
    // or `BOSSHOGG_ALLOW_HTTP=1` (matches the main Client's posture).
    let allow_http =
        ctx.allow_http || std::env::var("BOSSHOGG_ALLOW_HTTP").ok().as_deref() == Some("1");
    if allow_http && host.starts_with("http://") {
        tracing::warn!(
            host = %host,
            "TLS downgraded via BOSSHOGG_ALLOW_HTTP or context.allow_http; plaintext is unsafe over public networks"
        );
    }
    let mut capture_headers = HeaderMap::new();
    capture_headers.insert(
        USER_AGENT,
        HeaderValue::from_static(concat!("bosshogg/", env!("CARGO_PKG_VERSION"))),
    );
    let http = reqwest::Client::builder()
        .https_only(!allow_http)
        .gzip(true)
        .timeout(Duration::from_secs(30))
        .default_headers(capture_headers)
        .build()
        .map_err(BosshoggError::Http)?;

    match args.command {
        CaptureCommand::Event {
            event,
            distinct_id,
            properties_file,
        } => {
            capture_event(
                &http,
                &host,
                &project_token,
                event,
                distinct_id,
                properties_file,
                json_mode,
                yes,
                debug,
            )
            .await
        }
        CaptureCommand::Batch { file } => {
            capture_batch(&http, &host, &project_token, file, json_mode, yes, debug).await
        }
        CaptureCommand::Identify {
            distinct_id,
            properties_file,
        } => {
            capture_identify(
                &http,
                &host,
                &project_token,
                distinct_id,
                properties_file,
                json_mode,
                yes,
                debug,
            )
            .await
        }
    }
}

// ── Helpers ───────────────────────────────────────────────────────────────────

async fn send_capture(
    http: &reqwest::Client,
    url: &str,
    body: &Value,
    debug: bool,
) -> Result<CaptureResponse> {
    if debug {
        // Redact the api_key (project token) before logging.
        let mut redacted_body = body.clone();
        if let Some(key) = redacted_body.get("api_key").and_then(Value::as_str) {
            redacted_body["api_key"] = Value::String(crate::util::redact_key(key));
        }
        tracing::debug!(url, body = %redacted_body, "capture request");
    }

    let resp = http
        .post(url)
        .json(body)
        .send()
        .await
        .map_err(BosshoggError::Http)?;

    let status = resp.status();
    if debug {
        tracing::debug!(status = status.as_u16(), "capture response");
    }

    if !status.is_success() {
        let body_text = resp.text().await.unwrap_or_default();
        return Err(BosshoggError::ServerError {
            status: status.as_u16(),
            message: body_text
                .lines()
                .next()
                .unwrap_or("capture failed")
                .to_string(),
        });
    }

    let bytes = resp.bytes().await.map_err(BosshoggError::Http)?;
    if bytes.is_empty() {
        return Ok(CaptureResponse { status: 1 });
    }
    serde_json::from_slice::<CaptureResponse>(&bytes).map_err(BosshoggError::Json)
}

fn print_capture_result(resp: &CaptureResponse, json_mode: bool) {
    if json_mode {
        output::print_json(resp);
    } else {
        println!("Event captured (status: {})", resp.status);
    }
}

// ── event ─────────────────────────────────────────────────────────────────────

#[allow(clippy::too_many_arguments)]
async fn capture_event(
    http: &reqwest::Client,
    host: &str,
    project_token: &str,
    event: String,
    distinct_id: String,
    properties_file: Option<PathBuf>,
    json_mode: bool,
    yes: bool,
    debug: bool,
) -> Result<()> {
    gate_destructive(
        yes,
        &format!("send event `{event}` to PostHog (real production data); continue?"),
    )?;

    let properties: Value = if let Some(p) = properties_file.as_deref() {
        read_json_file(p).await?
    } else {
        json!({})
    };

    let body = json!({
        "api_key": project_token,
        "event": event,
        "distinct_id": distinct_id,
        "properties": properties,
    });

    let url = format!("{host}/i/v0/e");
    let resp = send_capture(http, &url, &body, debug).await?;
    print_capture_result(&resp, json_mode);
    Ok(())
}

// ── batch ─────────────────────────────────────────────────────────────────────

async fn capture_batch(
    http: &reqwest::Client,
    host: &str,
    project_token: &str,
    file: PathBuf,
    json_mode: bool,
    yes: bool,
    debug: bool,
) -> Result<()> {
    gate_destructive(
        yes,
        "send batch events to PostHog (real production data); continue?",
    )?;

    let raw = tokio::fs::read_to_string(&file)
        .await
        .map_err(|e| BosshoggError::Config(format!("read {}: {e}", file.display())))?;

    let mut events: Vec<Value> = Vec::new();
    for (i, line) in raw.lines().enumerate() {
        let line = line.trim();
        if line.is_empty() {
            continue;
        }
        let v: Value = serde_json::from_str(line)
            .map_err(|e| BosshoggError::BadRequest(format!("line {}: {e}", i + 1)))?;
        events.push(v);
    }

    if events.is_empty() {
        return Err(BosshoggError::BadRequest(
            "batch file contained no events".into(),
        ));
    }

    let body = json!({
        "api_key": project_token,
        "batch": events,
    });

    let url = format!("{host}/batch");
    let resp = send_capture(http, &url, &body, debug).await?;
    print_capture_result(&resp, json_mode);
    Ok(())
}

// ── identify ──────────────────────────────────────────────────────────────────

#[allow(clippy::too_many_arguments)]
async fn capture_identify(
    http: &reqwest::Client,
    host: &str,
    project_token: &str,
    distinct_id: String,
    properties_file: PathBuf,
    json_mode: bool,
    yes: bool,
    debug: bool,
) -> Result<()> {
    gate_destructive(
        yes,
        &format!("send identify for `{distinct_id}` to PostHog (real production data); continue?"),
    )?;

    let properties = read_json_file(&properties_file).await?;

    let body = json!({
        "api_key": project_token,
        "event": "$identify",
        "distinct_id": distinct_id,
        "properties": {
            "$set": properties,
        },
    });

    let url = format!("{host}/i/v0/e");
    let resp = send_capture(http, &url, &body, debug).await?;
    print_capture_result(&resp, json_mode);
    Ok(())
}

// ── unit tests ────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn capture_response_roundtrip() {
        let raw = r#"{"status": 1}"#;
        let r: CaptureResponse = serde_json::from_str(raw).unwrap();
        assert_eq!(r.status, 1);
    }

    #[test]
    fn capture_response_serialize() {
        let r = CaptureResponse { status: 1 };
        let s = serde_json::to_string(&r).unwrap();
        assert!(s.contains("\"status\":1"));
    }

    #[test]
    fn event_body_shape() {
        let body = json!({
            "api_key": "phc_testtoken",
            "event": "page_view",
            "distinct_id": "user123",
            "properties": {"url": "https://example.com"},
        });
        assert_eq!(body["api_key"], "phc_testtoken");
        assert_eq!(body["event"], "page_view");
        assert_eq!(body["distinct_id"], "user123");
    }

    #[test]
    fn batch_body_shape() {
        let events = vec![
            json!({"event": "click", "distinct_id": "u1"}),
            json!({"event": "view", "distinct_id": "u2"}),
        ];
        let body = json!({
            "api_key": "phc_test",
            "batch": events,
        });
        assert!(body["batch"].as_array().unwrap().len() == 2);
    }

    #[test]
    fn identify_body_shape() {
        let props = json!({"name": "Alice", "plan": "pro"});
        let body = json!({
            "api_key": "phc_test",
            "event": "$identify",
            "distinct_id": "alice@example.com",
            "properties": { "$set": props },
        });
        assert_eq!(body["event"], "$identify");
        assert_eq!(body["properties"]["$set"]["name"], "Alice");
    }
}