KosmoKrator

analytics

PostHog Lua API for KosmoKrator Agents

Agent-facing Lua documentation and function reference for the PostHog KosmoKrator integration.

15 functions 11 read 4 write API token auth

Lua Namespace

Agents call this integration through app.integrations.posthog.*. Use lua_read_doc("integrations.posthog") inside KosmoKrator to discover the same reference at runtime.

Agent-Facing Lua Docs

This is the rendered version of the full Lua documentation exposed to agents when they inspect the integration namespace.

PostHog — Lua API Reference

posthog_capture_event

Send (capture) a custom event to PostHog.

Parameters

NameTypeRequiredDescription
eventstringyesThe name of the event to capture (e.g., "signup", "purchase", "button_clicked").
distinct_idstringyesA unique identifier for the user performing the event (e.g., user ID, email, or anonymous ID).
propertiesobjectnoOptional key-value properties to attach to the event.
timestampstringnoOptional ISO 8601 timestamp for the event. Defaults to the current server time.

posthog_list_events

List events with optional filtering and pagination.

Parameters

NameTypeRequiredDescription
limitintegernoMaximum number of events to return (default: 100).
offsetintegernoNumber of events to skip for pagination (default: 0).
eventstringnoFilter by event name (e.g., "$pageview", "signup").
distinct_idstringnoFilter by distinct user ID.
person_idstringnoFilter by internal person UUID.
afterstringnoOnly return events after this timestamp (ISO 8601).
beforestringnoOnly return events before this timestamp (ISO 8601).

posthog_get_event

Get details of a specific event by ID.

Parameters

NameTypeRequiredDescription
event_idstringyesThe unique identifier of the event to retrieve.

posthog_list_persons

List persons (users) with optional search.

Parameters

NameTypeRequiredDescription
limitintegernoMaximum number of persons to return (default: 100).
offsetintegernoNumber of persons to skip for pagination (default: 0).
searchstringnoSearch query to filter persons by name, email, or distinct ID.

posthog_get_person

Get details of a specific person by ID.

Parameters

NameTypeRequiredDescription
person_idstringyesThe unique identifier (UUID) of the person to retrieve.

posthog_list_feature_flags

List all feature flags in the project.

Parameters

NameTypeRequiredDescription
limitintegernoMaximum number of feature flags to return (default: 100).
offsetintegernoNumber of feature flags to skip for pagination (default: 0).

posthog_get_feature_flag

Get details of a specific feature flag.

Parameters

NameTypeRequiredDescription
flag_idintegeryesThe unique identifier of the feature flag to retrieve.

posthog_create_feature_flag

Create a new feature flag.

Parameters

NameTypeRequiredDescription
namestringyesHuman-readable name for the feature flag (e.g., "New checkout flow").
keystringyesUnique key used to reference the flag in code (e.g., "new-checkout"). Must be lowercase with hyphens.
activebooleannoWhether the flag should be active immediately (default: true).
filtersobjectnoOptional filter conditions for targeting specific users or groups.
rollout_percentageintegernoPercentage of users to roll out to (0–100). Omit for 100%.

posthog_update_feature_flag

Update an existing feature flag.

Parameters

NameTypeRequiredDescription
flag_idintegeryesThe unique identifier of the feature flag to update.
activebooleannoSet the flag active (true) or inactive (false).
filtersobjectnoNew filter conditions for targeting specific users or groups.
rollout_percentageintegernoNew rollout percentage (0–100).

posthog_delete_feature_flag

Delete a feature flag.

Parameters

NameTypeRequiredDescription
flag_idintegeryesThe unique identifier of the feature flag to delete.

posthog_list_insights

List saved insights in the project.

Parameters

NameTypeRequiredDescription
limitintegernoMaximum number of insights to return (default: 100).
offsetintegernoNumber of insights to skip for pagination (default: 0).
typestringnoFilter by insight type: TRENDS, FUNNELS, RETENTION, PATHS, LIFECYCLE, or STICKINESS.

posthog_get_insight

Get details of a specific insight by ID.

Parameters

NameTypeRequiredDescription
insight_idintegeryesThe unique identifier of the insight to retrieve.

posthog_list_dashboards

List dashboards in the project.

Parameters

NameTypeRequiredDescription
limitintegernoMaximum number of dashboards to return (default: 100).
offsetintegernoNumber of dashboards to skip for pagination (default: 0).

posthog_get_dashboard

Get details of a specific dashboard by ID.

Parameters

NameTypeRequiredDescription
dashboard_idintegeryesThe unique identifier of the dashboard to retrieve.

posthog_list_cohorts

List cohorts in the project.

Parameters

NameTypeRequiredDescription
limitintegernoMaximum number of cohorts to return (default: 100).
offsetintegernoNumber of cohorts to skip for pagination (default: 0).

Examples

Capture a custom event

local result = app.integrations.posthog.posthog_capture_event({
  event = "purchase",
  distinct_id = "user-123",
  properties = {
    product = "Pro Plan",
    amount = 49.99
  }
})

List recent pageview events

local result = app.integrations.posthog.posthog_list_events({
  event = "$pageview",
  limit = 20,
  after = "2026-04-01T00:00:00Z"
})
for _, event in ipairs(result.results) do
  print(event.distinct_id .. " - " .. event.properties.pathname)
end

Create a feature flag with 50% rollout

local result = app.integrations.posthog.posthog_create_feature_flag({
  name = "New Dashboard",
  key = "new-dashboard",
  active = true,
  rollout_percentage = 50
})

List dashboards

local result = app.integrations.posthog.posthog_list_dashboards({
  limit = 10
})
for _, dashboard in ipairs(result.results) do
  print(dashboard.name .. " (ID: " .. dashboard.id .. ")")
end

Multi-Account Usage

If you have multiple posthog accounts configured, use account-specific namespaces:

-- Default account (always works)
app.integrations.posthog.function_name({...})

-- Explicit default (portable across setups)
app.integrations.posthog.default.function_name({...})

-- Named accounts
app.integrations.posthog.work.function_name({...})
app.integrations.posthog.personal.function_name({...})

All functions are identical across accounts — only the credentials differ.

Raw agent markdown
# PostHog — Lua API Reference

## posthog_capture_event

Send (capture) a custom event to PostHog.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `event` | string | yes | The name of the event to capture (e.g., `"signup"`, `"purchase"`, `"button_clicked"`). |
| `distinct_id` | string | yes | A unique identifier for the user performing the event (e.g., user ID, email, or anonymous ID). |
| `properties` | object | no | Optional key-value properties to attach to the event. |
| `timestamp` | string | no | Optional ISO 8601 timestamp for the event. Defaults to the current server time. |

## posthog_list_events

List events with optional filtering and pagination.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `limit` | integer | no | Maximum number of events to return (default: 100). |
| `offset` | integer | no | Number of events to skip for pagination (default: 0). |
| `event` | string | no | Filter by event name (e.g., `"$pageview"`, `"signup"`). |
| `distinct_id` | string | no | Filter by distinct user ID. |
| `person_id` | string | no | Filter by internal person UUID. |
| `after` | string | no | Only return events after this timestamp (ISO 8601). |
| `before` | string | no | Only return events before this timestamp (ISO 8601). |

## posthog_get_event

Get details of a specific event by ID.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `event_id` | string | yes | The unique identifier of the event to retrieve. |

## posthog_list_persons

List persons (users) with optional search.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `limit` | integer | no | Maximum number of persons to return (default: 100). |
| `offset` | integer | no | Number of persons to skip for pagination (default: 0). |
| `search` | string | no | Search query to filter persons by name, email, or distinct ID. |

## posthog_get_person

Get details of a specific person by ID.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `person_id` | string | yes | The unique identifier (UUID) of the person to retrieve. |

## posthog_list_feature_flags

List all feature flags in the project.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `limit` | integer | no | Maximum number of feature flags to return (default: 100). |
| `offset` | integer | no | Number of feature flags to skip for pagination (default: 0). |

## posthog_get_feature_flag

Get details of a specific feature flag.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `flag_id` | integer | yes | The unique identifier of the feature flag to retrieve. |

## posthog_create_feature_flag

Create a new feature flag.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `name` | string | yes | Human-readable name for the feature flag (e.g., `"New checkout flow"`). |
| `key` | string | yes | Unique key used to reference the flag in code (e.g., `"new-checkout"`). Must be lowercase with hyphens. |
| `active` | boolean | no | Whether the flag should be active immediately (default: true). |
| `filters` | object | no | Optional filter conditions for targeting specific users or groups. |
| `rollout_percentage` | integer | no | Percentage of users to roll out to (0–100). Omit for 100%. |

## posthog_update_feature_flag

Update an existing feature flag.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `flag_id` | integer | yes | The unique identifier of the feature flag to update. |
| `active` | boolean | no | Set the flag active (true) or inactive (false). |
| `filters` | object | no | New filter conditions for targeting specific users or groups. |
| `rollout_percentage` | integer | no | New rollout percentage (0–100). |

## posthog_delete_feature_flag

Delete a feature flag.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `flag_id` | integer | yes | The unique identifier of the feature flag to delete. |

## posthog_list_insights

List saved insights in the project.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `limit` | integer | no | Maximum number of insights to return (default: 100). |
| `offset` | integer | no | Number of insights to skip for pagination (default: 0). |
| `type` | string | no | Filter by insight type: `TRENDS`, `FUNNELS`, `RETENTION`, `PATHS`, `LIFECYCLE`, or `STICKINESS`. |

## posthog_get_insight

Get details of a specific insight by ID.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `insight_id` | integer | yes | The unique identifier of the insight to retrieve. |

## posthog_list_dashboards

List dashboards in the project.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `limit` | integer | no | Maximum number of dashboards to return (default: 100). |
| `offset` | integer | no | Number of dashboards to skip for pagination (default: 0). |

## posthog_get_dashboard

Get details of a specific dashboard by ID.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `dashboard_id` | integer | yes | The unique identifier of the dashboard to retrieve. |

## posthog_list_cohorts

List cohorts in the project.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `limit` | integer | no | Maximum number of cohorts to return (default: 100). |
| `offset` | integer | no | Number of cohorts to skip for pagination (default: 0). |

## Examples

### Capture a custom event

```lua
local result = app.integrations.posthog.posthog_capture_event({
  event = "purchase",
  distinct_id = "user-123",
  properties = {
    product = "Pro Plan",
    amount = 49.99
  }
})
```

### List recent pageview events

```lua
local result = app.integrations.posthog.posthog_list_events({
  event = "$pageview",
  limit = 20,
  after = "2026-04-01T00:00:00Z"
})
for _, event in ipairs(result.results) do
  print(event.distinct_id .. " - " .. event.properties.pathname)
end
```

### Create a feature flag with 50% rollout

```lua
local result = app.integrations.posthog.posthog_create_feature_flag({
  name = "New Dashboard",
  key = "new-dashboard",
  active = true,
  rollout_percentage = 50
})
```

### List dashboards

```lua
local result = app.integrations.posthog.posthog_list_dashboards({
  limit = 10
})
for _, dashboard in ipairs(result.results) do
  print(dashboard.name .. " (ID: " .. dashboard.id .. ")")
end
```

---

## Multi-Account Usage

If you have multiple posthog accounts configured, use account-specific namespaces:

```lua
-- Default account (always works)
app.integrations.posthog.function_name({...})

-- Explicit default (portable across setups)
app.integrations.posthog.default.function_name({...})

-- Named accounts
app.integrations.posthog.work.function_name({...})
app.integrations.posthog.personal.function_name({...})
```

All functions are identical across accounts — only the credentials differ.

Metadata-Derived Lua Example

local result = app.integrations.posthog.posthog_capture_event({
  event = "example_event",
  distinct_id = "example_distinct_id",
  properties = "example_properties",
  timestamp = "example_timestamp"
})
print(result)

Functions

posthog_capture_event

Send (capture) a custom event to PostHog for a specific user. The event will appear in the PostHog events stream and can be used in insights and funnels.

Operation
Write write
Full name
posthog.posthog_capture_event
ParameterTypeRequiredDescription
event string yes The name of the event to capture (e.g., "signup", "purchase", "button_clicked").
distinct_id string yes A unique identifier for the user performing the event (e.g., user ID, email, or anonymous ID).
properties object no Optional key-value properties to attach to the event.
timestamp string no Optional ISO 8601 timestamp for the event. Defaults to the current server time.

posthog_list_events

List events from PostHog with optional filtering by event name, user, date range, and pagination.

Operation
Read read
Full name
posthog.posthog_list_events
ParameterTypeRequiredDescription
limit integer no Maximum number of events to return (default: 100).
offset integer no Number of events to skip for pagination (default: 0).
event string no Filter by event name (e.g., "$pageview", "signup").
distinct_id string no Filter by distinct user ID.
person_id string no Filter by internal person UUID.
after string no Only return events after this timestamp (ISO 8601, e.g., "2025-01-01T00:00:00Z").
before string no Only return events before this timestamp (ISO 8601, e.g., "2025-12-31T23:59:59Z").

posthog_get_event

Get details of a specific PostHog event by its unique ID, including all event properties and metadata.

Operation
Read read
Full name
posthog.posthog_get_event
ParameterTypeRequiredDescription
event_id string yes The unique identifier of the event to retrieve.

posthog_list_persons

List persons (users) from PostHog. Optionally search by name, email, or distinct ID to find specific users.

Operation
Read read
Full name
posthog.posthog_list_persons
ParameterTypeRequiredDescription
limit integer no Maximum number of persons to return (default: 100).
offset integer no Number of persons to skip for pagination (default: 0).
search string no Search query to filter persons by name, email, or distinct ID.

posthog_get_person

Get details of a specific PostHog person (user) by their unique ID, including properties and event history metadata.

Operation
Read read
Full name
posthog.posthog_get_person
ParameterTypeRequiredDescription
person_id string yes The unique identifier (UUID) of the person to retrieve.

posthog_list_feature_flags

List all feature flags in the PostHog project, including their status, rollout percentages, and filter conditions.

Operation
Read read
Full name
posthog.posthog_list_feature_flags
ParameterTypeRequiredDescription
limit integer no Maximum number of feature flags to return (default: 100).
offset integer no Number of feature flags to skip for pagination (default: 0).

posthog_get_feature_flag

Get details of a specific PostHog feature flag by its ID, including rollout configuration and filter conditions.

Operation
Read read
Full name
posthog.posthog_get_feature_flag
ParameterTypeRequiredDescription
flag_id integer yes The unique identifier of the feature flag to retrieve.

posthog_create_feature_flag

Create a new feature flag in PostHog with a name, key, and optional rollout configuration.

Operation
Write write
Full name
posthog.posthog_create_feature_flag
ParameterTypeRequiredDescription
name string yes Human-readable name for the feature flag (e.g., "New checkout flow").
key string yes Unique key used to reference the flag in code (e.g., "new-checkout"). Must be lowercase with hyphens.
active boolean no Whether the flag should be active immediately (default: true).
filters object no Optional filter conditions for targeting specific users or groups.
rollout_percentage integer no Percentage of users to roll out to (0–100). Omit for 100%.

posthog_update_feature_flag

Update an existing PostHog feature flag — change its active state, filters, or rollout percentage.

Operation
Write write
Full name
posthog.posthog_update_feature_flag
ParameterTypeRequiredDescription
flag_id integer yes The unique identifier of the feature flag to update.
active boolean no Set the flag active (true) or inactive (false).
filters object no New filter conditions for targeting specific users or groups.
rollout_percentage integer no New rollout percentage (0–100).

posthog_delete_feature_flag

Delete a feature flag from PostHog. This action is permanent and cannot be undone.

Operation
Write write
Full name
posthog.posthog_delete_feature_flag
ParameterTypeRequiredDescription
flag_id integer yes The unique identifier of the feature flag to delete.

posthog_list_insights

List saved insights in the PostHog project. Optionally filter by insight type (e.g., TRENDS, FUNNELS, RETENTION, PATHS).

Operation
Read read
Full name
posthog.posthog_list_insights
ParameterTypeRequiredDescription
limit integer no Maximum number of insights to return (default: 100).
offset integer no Number of insights to skip for pagination (default: 0).
type string no Filter by insight type: TRENDS, FUNNELS, RETENTION, PATHS, LIFECYCLE, or STICKINESS.

posthog_get_insight

Get details of a specific PostHog insight by its ID, including its query configuration and cached results.

Operation
Read read
Full name
posthog.posthog_get_insight
ParameterTypeRequiredDescription
insight_id integer yes The unique identifier of the insight to retrieve.

posthog_list_dashboards

List dashboards in the PostHog project. Dashboards contain collections of insights organized for monitoring.

Operation
Read read
Full name
posthog.posthog_list_dashboards
ParameterTypeRequiredDescription
limit integer no Maximum number of dashboards to return (default: 100).
offset integer no Number of dashboards to skip for pagination (default: 0).

posthog_get_dashboard

Get details of a specific PostHog dashboard by its ID, including its layout and contained insights.

Operation
Read read
Full name
posthog.posthog_get_dashboard
ParameterTypeRequiredDescription
dashboard_id integer yes The unique identifier of the dashboard to retrieve.

posthog_list_cohorts

List cohorts in the PostHog project. Cohorts are dynamic groups of users defined by behavioral or property conditions.

Operation
Read read
Full name
posthog.posthog_list_cohorts
ParameterTypeRequiredDescription
limit integer no Maximum number of cohorts to return (default: 100).
offset integer no Number of cohorts to skip for pagination (default: 0).