# Queries

Run HogQL and trends queries over the project's events, tell a cached result from a fresh one and read the event and property taxonomy behind them.

The query route lets a test read back what its code captured, as counts, rows or a trend over time. Load the billing scenario, whose project is 3105, and count its events by name with a HogQL query:

```bash
KEY=phx_rysticexamplepersonalkey000000000000000
curl -s -X POST "$RYSTIC_URL/_rystic/scenario" -o /dev/null -w '%{http_code}\n' -d '{"name":"posthog_billing_backfill"}'
# -> 204
QUERY="$RYSTIC_URL/api/environments/3105/query/"
curl -s -X POST "$QUERY" -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d '{"query":{"kind":"HogQLQuery","query":"select event, count() from events group by event order by event"}}' | jq -c '{columns, results}'
# -> {"columns":["event","count()"],"results":[["invoice_paid",3],["subscription_canceled",1],["subscription_started",2]]}
```

The `posthog_billing_backfill` scenario has six events from two customers between January 12 and February 16, 2026. Its clock starts at the time the Clock column of [Scenarios](https://www.rystic.ai/docs/simulators/posthog.md#scenarios) gives.

## Filter events with HogQL

To check the properties your code sent, select them by path and filter with `where`. Each property column comes back typed `Nullable(String)`, as `types` shows:

```bash
curl -s -X POST "$QUERY" -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d "{\"query\":{\"kind\":\"HogQLQuery\",\"query\":\"select timestamp, distinct_id, properties.plan from events where event = 'invoice_paid' order by timestamp\"}}" | jq -c '.types[2], .results[]'
# -> ["properties.plan","Nullable(String)"]
# -> ["2026-01-12T10:00:05.250Z","cust_ada","team"]
# -> ["2026-01-19T16:30:04.900Z","cust_bo","starter"]
# -> ["2026-02-12T10:00:06.480Z","cust_ada","team"]
curl -s -X POST "$QUERY" -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d "{\"query\":{\"kind\":\"HogQLQuery\",\"query\":\"select distinct_id, count() from events where timestamp >= '2026-02-01' group by distinct_id order by distinct_id\"}}" | jq -c '.results'
# -> [["cust_ada",1],["cust_bo",1]]
```

Sort by a column the query selects. Ordering by any other column returns 400 `hogql_query_error`, so add the column to the `select` list:

```bash
curl -s -X POST "$QUERY" -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d '{"query":{"kind":"HogQLQuery","query":"select event from events order by timestamp"}}' | jq -c '{code, detail}'
# -> {"code":"hogql_query_error","detail":"'timestamp' is not a column"}
```

[Limitations](https://www.rystic.ai/docs/simulators/posthog.md#limitations) lists the query syntax the simulator does not support.

## Run a trends query over a date range

To chart an event over time, send a `TrendsQuery` with a series, an `interval` and a `dateRange`. Weekly buckets start on Sunday, and `resolved_date_range` shows the range the simulator counted:

```bash
curl -s -X POST "$QUERY" -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d '{"query":{"kind":"TrendsQuery","series":[{"kind":"EventsNode","event":"invoice_paid"}],"interval":"week","dateRange":{"date_from":"2026-01-05","date_to":"2026-02-22"}}}' | jq -c '{range: .resolved_date_range.date_from, days: .results[0].days, data: .results[0].data, count: .results[0].count}'
# -> {"range":"2026-01-04T00:00:00Z","days":["2026-01-04","2026-01-11","2026-01-18","2026-01-25","2026-02-01","2026-02-08","2026-02-15","2026-02-22"],"data":[0,1,1,0,0,1,0,0],"count":3}
```

A relative `date_from` such as `-30d` counts back from the scenario's clock, so the range always starts on January 30:

```bash
curl -s -X POST "$QUERY" -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d '{"query":{"kind":"TrendsQuery","series":[{"kind":"EventsNode","event":"invoice_paid"}],"dateRange":{"date_from":"-30d"}}}' | jq -c '{range: .resolved_date_range, count: .results[0].count}'
# -> {"range":{"date_from":"2026-01-30T00:00:00Z","date_to":"2026-03-01T23:59:59.999999Z","read_from":null},"count":1}
```

To total a numeric property, set `math` to `sum` and name it in `math_property`. To split a series by a property value, add a `breakdownFilter`:

```bash
curl -s -X POST "$QUERY" -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d '{"query":{"kind":"TrendsQuery","series":[{"kind":"EventsNode","event":"invoice_paid","math":"sum","math_property":"amount_usd"}],"interval":"month","dateRange":{"date_from":"2026-01-01","date_to":"2026-02-28"}}}' | jq -c '.results[] | {days, data}'
# -> {"days":["2026-01-01","2026-02-01"],"data":[540,450]}
curl -s -X POST "$QUERY" -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d '{"query":{"kind":"TrendsQuery","series":[{"kind":"EventsNode","event":"invoice_paid"}],"interval":"month","dateRange":{"date_from":"2026-01-01","date_to":"2026-02-28"},"breakdownFilter":{"breakdown":"plan","breakdown_type":"event"}}}' | jq -c '.results[] | {breakdown_value, data}'
# -> {"breakdown_value":"team","data":[1,1]}
# -> {"breakdown_value":"starter","data":[1,0]}
```

## Tell a cached result from a fresh one

The simulator caches each query's result under its `cache_key`. A repeat of the same query returns the stored result with `is_cached` set to `true`, even after new events arrive. Capture an event between two identical queries, then force a fresh run with `"refresh": "force_blocking"`:

```bash
COUNT='{"query":{"kind":"HogQLQuery","query":"select count() from events"}}'
FRESH='{"query":{"kind":"HogQLQuery","query":"select count() from events"},"refresh":"force_blocking"}'
curl -s -X POST "$QUERY" -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d "$COUNT" | jq -c '{is_cached, results}'
# -> {"is_cached":false,"results":[[6]]}
curl -s -X POST "$RYSTIC_URL/i/v0/e/" -H 'Content-Type: application/json' -d '{"api_key":"phc_a79fcba754f1e84cb434a866f3e60afac8fe74b7126","event":"invoice_paid","distinct_id":"cust_bo","timestamp":"2026-02-19T16:30:00Z"}'
# -> {"status":"Ok"}
curl -s -X POST "$QUERY" -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d "$COUNT" | jq -c '{is_cached, results}'
# -> {"is_cached":true,"results":[[6]]}
curl -s -X POST "$QUERY" -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d "$FRESH" | jq -c '{is_cached, results}'
# -> {"is_cached":false,"results":[[7]]}
```

A cached HogQL result stays current for five minutes from its run, until the time in `cache_target_age`. Move the simulator's clock past that with [`/_rystic/clock`](https://www.rystic.ai/docs/control-plane.md), and the same query runs fresh again:

```bash
curl -s -X POST "$QUERY" -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d "$COUNT" | jq -c '{is_cached, last_refresh: .last_refresh[:16], cache_target_age: .cache_target_age[:16]}'
# -> {"is_cached":true,"last_refresh":"2026-03-01T00:00","cache_target_age":"2026-03-01T00:05"}
curl -s -X POST "$RYSTIC_URL/_rystic/clock" -o /dev/null -w '%{http_code}\n' -d '{"advance_seconds":360}'
# -> 204
curl -s -X POST "$QUERY" -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d "$COUNT" | jq -c '{is_cached, last_refresh: .last_refresh[:16]}'
# -> {"is_cached":false,"last_refresh":"2026-03-01T00:06"}
```

Capture events with a `timestamp`; [Limitations](https://www.rystic.ai/docs/simulators/posthog.md#limitations) explains why.

## Count unique users per event

To count people rather than events, set a series' `math` to `dau`. The `posthog_signup_funnel` scenario has eight web events from three visitors on February 24, 2026, in project 3101:

```bash
curl -s -X POST "$RYSTIC_URL/_rystic/scenario" -o /dev/null -w '%{http_code}\n' -d '{"name":"posthog_signup_funnel"}'
# -> 204
curl -s -X POST "$RYSTIC_URL/api/environments/3101/query/" -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d '{"query":{"kind":"TrendsQuery","series":[{"kind":"EventsNode","event":"$pageview","math":"dau"},{"kind":"EventsNode","event":"signup_started","math":"dau"},{"kind":"EventsNode","event":"signup_completed","math":"dau"}],"dateRange":{"date_from":"2026-02-24","date_to":"2026-02-24"}}}' | jq -c '[.results[] | {label, count}]'
# -> [{"label":"$pageview","count":3},{"label":"signup_started","count":2},{"label":"signup_completed","count":1}]
```

## Run SQL through the MCP tools

An agent that reaches PostHog through its MCP server sends SQL to `execute_sql`. The simulator returns the rows in `content`, one line each with columns separated by `|`:

```bash
curl -s -X POST "$RYSTIC_URL/_rystic/scenario" -o /dev/null -w '%{http_code}\n' -d '{"name":"posthog_billing_backfill"}'
# -> 204
MCP="$RYSTIC_URL/api/environments/3105/mcp_tools"
curl -s -X POST "$MCP/execute_sql/" -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d '{"args":{"query":"select event, count() from events group by event order by event"}}' | jq -r '.success, .content'
# -> true
# -> event|count()
# -> invoice_paid|3
# -> subscription_canceled|1
# -> subscription_started|2
```

`read_taxonomy` lists the project's event definitions for `kind` `events` and its property definitions for `kind` `properties`, each with its description:

```bash
curl -s -X POST "$MCP/read_taxonomy/" -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d '{"args":{"query":{"kind":"events"}}}' | jq -r '.content | split("\n")[] | select(startswith("- "))'
# -> - `invoice_paid` - An invoice was settled in full.
# -> - `subscription_canceled` - A customer ended a paid subscription.
# -> - `subscription_started` - A customer began a paid subscription. (not seen in the last 30 days)
curl -s -X POST "$MCP/read_taxonomy/" -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -d '{"args":{"query":{"kind":"properties"}}}' | jq -r '.content | split("\n")[] | select(startswith("- "))'
# -> - `amount_usd` (Numeric) - Invoice amount in US dollars.
# -> - `interval` (String) - The billing interval.
# -> - `invoice_id` (String) - The billing system invoice id.
# -> - `plan` (String) - The subscription plan name.
# -> - `reason` (String) - Why the subscription was canceled.
```

The "not seen in the last 30 days" note follows each definition's `last_seen_at`, measured back from the scenario's clock.

## Read event and property definitions

To check the taxonomy a dashboard or agent builds on, list the definitions, or read one event definition by its name:

```bash
DEFS="$RYSTIC_URL/api/projects/3105"
curl -s "$DEFS/event_definitions/" -H "Authorization: Bearer $KEY" | jq -c '{count, names: [.results[].name]}'
# -> {"count":3,"names":["subscription_canceled","invoice_paid","subscription_started"]}
curl -s "$DEFS/event_definitions/by_name/?name=invoice_paid" -H "Authorization: Bearer $KEY" | jq -c '{name, tags, last_seen_at}'
# -> {"name":"invoice_paid","tags":["billing"],"last_seen_at":"2026-02-12T10:00:06.480000Z"}
curl -s "$DEFS/event_definitions/by_name/?name=refund_issued" -H "Authorization: Bearer $KEY" -w '%{http_code}\n'
# -> {"attr":null,"code":"not_found","detail":"Not found.","type":"invalid_request"}
# -> 404
curl -s "$DEFS/property_definitions/" -H "Authorization: Bearer $KEY" | jq -c '.results[] | {name, property_type, is_numerical}'
# -> {"name":"plan","property_type":"String","is_numerical":false}
# -> {"name":"amount_usd","property_type":"Numeric","is_numerical":true}
# -> {"name":"reason","property_type":"String","is_numerical":false}
# -> {"name":"interval","property_type":"String","is_numerical":false}
# -> {"name":"invoice_id","property_type":"String","is_numerical":false}
```

The event list is ordered by `last_seen_at`, most recent first.

## Reference

- The project id differs by scenario: 3104 in `posthog_first_event`, 3105 in `posthog_billing_backfill` and 3101 in `posthog_signup_funnel`.
- The clock moves forward a little with each request, so compare `last_refresh` and `cache_target_age` to the minute, as the examples do.

---

Source: https://www.rystic.ai/docs/simulators/posthog/features/queries · Markdown: https://www.rystic.ai/docs/simulators/posthog/features/queries.md
