Queries and insights over the API

Run a trends, funnel or retention query and save it as an insight with the REST API, with copy-paste bodies and the query JSON schema.

Embed
Preview of the card
<a href="https://lucidace.com/help/query-api"><img src="https://lucidace.com/help/_api/public/orgs/af34862a-c837-4478-8335-0505efe40459/spaces/2f4b888b-c4e6-488b-81bd-6e838fb12d2e/pages/55704090-1fa4-46e7-91ca-f3f735258a34/social-card.svg?v=3e7ae33eec8b" alt="Queries and insights over the API" width="600" height="315"></a>

Every chart in Lucida is a query: a JSON document with a kind (trends, funnel, retention, paths, stickiness, lifecycle, ...). You run one with POST /v1/projects/{project_id}/query and keep it as an insight with POST /v1/projects/{project_id}/insights.

Authenticate with Authorization: Bearer <key>: a project secret key or an account key with the analytics scopes. The full JSON Schema of the query is served, without authentication, at GET /v1/query.schema.json on your Lucida host; the rest of the API is in GET /v1/openapi.json.

Run a funnel

curl -X POST "$LUCIDA/v1/projects/$PROJECT/query" \
  -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{
    "query": {
      "kind": "funnel",
      "date_range": {"from": "2026-10-01T00:00:00Z", "to": "2026-11-01T00:00:00Z"},
      "steps": [
        {"source": {"type": "event", "name": "$pageview"}},
        {"source": {"type": "event", "name": "signed_up"}},
        {"source": {"type": "event", "name": "created_project"}}
      ],
      "window": {"unit": "day", "amount": 7}
    }
  }'

The answer is {"columns": [...], "rows": [[...]], "meta": {...}}. A query that runs longer than 5 seconds answers 202 with a query_id; poll GET /v1/projects/{project_id}/query/{query_id} and cancel with DELETE. Add "validate_only": true to check a query without running it.

Every query needs date_range (from, to as RFC 3339 timestamps, or a date-only value such as 2026-10-01, read as that day's UTC midnight, or a relative value: now, today, yesterday, mtd, ytd, all, or -N plus h, d, w, m or y, such as -30d. Relative values are resolved when the query runs, in the project's time zone, so a saved insight or a dashboard template tile with "from": "-30d" always shows the last 30 days). Common optional fields: interval (minute to year, default day), filters, breakdown, compare ({"type": "prev"}), timezone, test_account_filter. Fields the schema does not list are rejected with a 400.

{
  "kind": "trends",
  "date_range": {"from": "2026-10-01T00:00:00Z", "to": "2026-11-01T00:00:00Z"},
  "interval": "day",
  "series": [
    {"source": {"type": "event", "name": "signed_up"}, "math": "dau"},
    {"source": {"type": "event", "name": "purchase"}, "math": "sum", "math_property": "amount"}
  ]
}

Result rows are series, period, bucket, value (plus one bd_N column per breakdown, before value). period is current or previous (with compare). A bucket is the start of the interval in the query timezone, as RFC 3339 text with that zone's real offset (2026-11-03T00:00:00-05:00; a UTC offset reads Z), so a day bucket is local midnight even across a clock change. Counting maths (total, dau, unique_*, weekly_active, monthly_active, first_time_for_user) and sum return every bucket of the range, 0 where nothing happened (per breakdown value that has any event in the period). Other maths (avg, min, max, percentiles, per_user_*) leave empty buckets out, since 0 would read as a measurement. To drill into a bucket send its string unchanged as the insight_actors point bucket; the local wall clock is what matches.

math is total (default), dau (unique persons), unique_sessions, sum/avg/min/max/median/p90/... (these need math_property), and more; the schema lists them all.

Retention

{
  "kind": "retention",
  "date_range": {"from": "2026-09-01T00:00:00Z", "to": "2026-11-01T00:00:00Z"},
  "start": {"source": {"type": "event", "name": "signed_up"}},
  "return_event": {"source": {"type": "event", "name": "$pageview"}},
  "period": "week",
  "periods": 8
}

Leave out return_event to count any event as a return.

periods is the number of periods after the cohort's own period, so periods: 5 returns six columns, 0 to 5 (column 0 is the cohort period itself). A period that has not fully elapsed comes back with retained: null and in_progress: true (and in_progress: false otherwise); do not read the null as zero retention. With rolling time windows a period counts from each person's own first start event, and it is in progress until the latest-starting person in the cohort has lived through it. Alerts and metric trees that read a retention period skip in-progress cohorts instead of counting them as zero.

Filters

filters is a nestable {"and": [...]} or {"or": [...]} of property tests:

{"and": [
  {"key": "plan", "type": "person", "op": "eq", "value": "pro"},
  {"key": "$browser", "op": "in", "value": ["Chrome", "Safari"]}
]}

type is event (default), person, session, group (with group_index), cohort, ... op is one of eq, neq, contains, regex, gt, gte, lt, lte, is_set, is_not_set, in, not_in, between and more. A filter can sit on the whole query or on one series or step ("filters" next to "source").

Breakdown, compare, exclusions

breakdown is a list of {"key", "type"} objects (type as in filters: event, person, group, ...). A breakdown on a group takes group_index and the group property as key:

"breakdown": [{"key": "plan", "type": "person"}]
"breakdown": [{"key": "tier", "type": "group", "group_index": 0}]

A row whose property is missing lands in the bucket $$_lc_none_$$ (the "none" label; the app shows it as "None"). A group breakdown whose key no group of that group_index has is rejected with 400 and errors[0].field of breakdown[0].key (an empty key splits by the group key itself). Other bad breakdown values are rejected the same way, naming the field: breakdown[1].limit, breakdown[0].group_index, breakdown[2].key.

compare runs the same query over the window before date_range and tags those rows previous:

"compare": {"type": "prev"}

A funnel can drop people who did something between two steps with exclusions:

"exclusions": [{"event": {"source": {"type": "event", "name": "refund"}}, "after_step": 1, "until_step": 2}]

Who dropped off: funnel_persons

{
  "kind": "funnel_persons",
  "step": 1,
  "funnel": {"date_range": {"from": "2026-10-01", "to": "2026-11-01"}, "steps": [{"source": {"type": "event", "name": "$pageview"}}, {"source": {"type": "event", "name": "signed_up"}}]},
  "limit": 100
}

step is 1-based and must be from 1 to the number of steps minus one: the people who reached step but not step + 1 (set "converted": true for those who reached step + 1). limit defaults to 100 and is at most 10000. Rows are entity, person_id, distinct_id, session_ids, ordered by entity. The response meta carries total (everyone matching), truncated (more than this page) and next_cursor; send it back as "cursor" in the same query to get the next page.

Save it as an insight

curl -X POST "$LUCIDA/v1/projects/$PROJECT/insights" \
  -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{"name": "Activation funnel", "query": { "kind": "funnel", "...": "as above" }}'

name and query are required; description, tags, folder (a free-text label) and settings (display only) are optional. To put an insight in a folder, send folder_id (a folder id, or null to take it out) in a PATCH, or PUT /objects/insight/{id}/folder; both need edit access to the insight and to the folder. Put insights on a dashboard from the app or through /v1/projects/{project_id}/dashboards.

The query is stored as a JSON object exactly as you sent it, after it is validated. Create does not add default values to it, and relative dates such as -30d stay relative. Postgres stores it as jsonb, so key order is not preserved: the query you read back can list its keys in a different order. Compare queries by value, not by text. To tell a real change from a reorder, compare the ETag header, which is a hash of the key-sorted JSON.

Updated