KosmoKrator

analytics

Segment Lua API for KosmoKrator Agents

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

Lua Namespace

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

Call Lua from the Headless CLI

Use kosmo integrations:lua when a shell script, CI job, cron job, or another coding CLI should run a deterministic Segment workflow without starting an interactive agent session.

Inline Lua call
kosmo integrations:lua --eval 'dump(app.integrations.segment.identify_user({userId = "example_userId", traits = "example_traits"}))' --json
Read Lua docs headlessly
kosmo integrations:lua --eval 'print(docs.read("segment"))' --json
kosmo integrations:lua --eval 'print(docs.read("segment.identify_user"))' --json

Workflow file

Put repeatable logic in a Lua file, then execute it with JSON output for the calling process.

workflow.lua
local segment = app.integrations.segment
local result = segment.identify_user({userId = "example_userId", traits = "example_traits"})

dump(result)
Run the workflow
kosmo integrations:lua workflow.lua --json
kosmo integrations:lua workflow.lua --force --json
Namespace note. integrations:lua exposes app.integrations.segment, app.mcp.*, docs.*, json.*, and regex.*. Use app.integrations.segment.default.* or app.integrations.segment.work.* when you configured named credential accounts.

MCP-only Lua

If the script only needs configured MCP servers and does not need Segment, use the narrower mcp:lua command.

MCP Lua command
# Use mcp:lua for MCP-only scripts; use integrations:lua for this integration namespace.
kosmo mcp:lua --eval 'dump(mcp.servers())' --json

Agent-Facing Lua Docs

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

Segment — Lua API Reference

identify

Identify a user in Segment with their traits. Links metadata about a user to a known userId.

Parameters

NameTypeRequiredDescription
userIdstringyesThe unique identifier for the user in your database
traitsobjectnoKey-value pairs of user traits (e.g., name, email, plan, role, company)

Examples

-- Identify a user with traits
app.integrations.segment.identify({
  userId = "user-42",
  traits = {
    name = "Jane Doe",
    email = "jane@example.com",
    plan = "pro",
    role = "admin"
  }
})
-- Update a user's plan
app.integrations.segment.identify({
  userId = "user-42",
  traits = {
    plan = "enterprise",
    upgraded_at = "2026-04-05"
  }
})

track

Track a custom event for a user in Segment.

Parameters

NameTypeRequiredDescription
eventstringyesThe name of the event (e.g., “Order Completed”)
userIdstringyesThe unique identifier for the user
propertiesobjectnoKey-value pairs of event properties

Examples

-- Track a purchase event
app.integrations.segment.track({
  event = "Order Completed",
  userId = "user-42",
  properties = {
    revenue = 99.99,
    currency = "USD",
    productId = "widget-3000",
    quantity = 2
  }
})
-- Track a button click
app.integrations.segment.track({
  event = "CTA Clicked",
  userId = "user-42",
  properties = {
    button = "Upgrade to Pro",
    page = "/pricing"
  }
})

page

Record a page view in Segment.

Parameters

NameTypeRequiredDescription
namestringyesThe name of the page viewed
userIdstringyesThe unique identifier for the user
propertiesobjectnoKey-value pairs of page properties (url, referrer, title, path)

Examples

-- Record a page view
app.integrations.segment.page({
  name = "Product Listing",
  userId = "user-42",
  properties = {
    url = "/products/widgets",
    category = "Widgets",
    referrer = "https://google.com"
  }
})

group

Associate a user with a group (organization, company, account) in Segment.

Parameters

NameTypeRequiredDescription
groupIdstringyesThe unique identifier for the group
userIdstringyesThe unique identifier for the user
traitsobjectnoKey-value pairs of group traits (name, plan, industry)

Examples

-- Add user to an organization
app.integrations.segment.group({
  groupId = "org-123",
  userId = "user-42",
  traits = {
    name = "Acme Corp",
    plan = "enterprise",
    industry = "Technology",
    employee_count = 250
  }
})

get_workspace

Get details of a Segment workspace by its slug. Requires an API token.

Parameters

NameTypeRequiredDescription
slugstringyesThe workspace slug (e.g., “my-workspace”)

Examples

-- Get workspace details
local ws = app.integrations.segment.get_workspace({
  slug = "my-workspace"
})
print(ws.name)
print(ws.id)

list_sources

List all sources in a Segment workspace. Requires an API token.

Parameters

NameTypeRequiredDescription
slugstringyesThe workspace slug

Examples

-- List all sources
local result = app.integrations.segment.list_sources({
  slug = "my-workspace"
})

for _, source in ipairs(result.sources or {}) do
  print(source.name .. " (" .. source.id .. ")")
end

get_source

Get details of a specific Segment source. Requires an API token.

Parameters

NameTypeRequiredDescription
slugstringyesThe workspace slug
idstringyesThe source ID

Examples

-- Get source details
local source = app.integrations.segment.get_source({
  slug = "my-workspace",
  id = "abc123"
})
print(source.name)
print(source.write_key)

get_current_user

Get the currently authenticated Segment user. Requires an API token. Useful for verifying credentials.

Parameters

None.

Examples

-- Verify API token is working
local user = app.integrations.segment.get_current_user({})
print("Authenticated as: " .. user.email)

Multi-Account Usage

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

-- Default account (always works)
app.integrations.segment.track({...})

-- Explicit default (portable across setups)
app.integrations.segment.default.track({...})

-- Named accounts
app.integrations.segment.production.track({...})
app.integrations.segment.staging.track({...})

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

Raw agent markdown
# Segment — Lua API Reference

## identify

Identify a user in Segment with their traits. Links metadata about a user to a known userId.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `userId` | string | yes | The unique identifier for the user in your database |
| `traits` | object | no | Key-value pairs of user traits (e.g., name, email, plan, role, company) |

### Examples

```lua
-- Identify a user with traits
app.integrations.segment.identify({
  userId = "user-42",
  traits = {
    name = "Jane Doe",
    email = "jane@example.com",
    plan = "pro",
    role = "admin"
  }
})
```

```lua
-- Update a user's plan
app.integrations.segment.identify({
  userId = "user-42",
  traits = {
    plan = "enterprise",
    upgraded_at = "2026-04-05"
  }
})
```

---

## track

Track a custom event for a user in Segment.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `event` | string | yes | The name of the event (e.g., "Order Completed") |
| `userId` | string | yes | The unique identifier for the user |
| `properties` | object | no | Key-value pairs of event properties |

### Examples

```lua
-- Track a purchase event
app.integrations.segment.track({
  event = "Order Completed",
  userId = "user-42",
  properties = {
    revenue = 99.99,
    currency = "USD",
    productId = "widget-3000",
    quantity = 2
  }
})
```

```lua
-- Track a button click
app.integrations.segment.track({
  event = "CTA Clicked",
  userId = "user-42",
  properties = {
    button = "Upgrade to Pro",
    page = "/pricing"
  }
})
```

---

## page

Record a page view in Segment.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `name` | string | yes | The name of the page viewed |
| `userId` | string | yes | The unique identifier for the user |
| `properties` | object | no | Key-value pairs of page properties (url, referrer, title, path) |

### Examples

```lua
-- Record a page view
app.integrations.segment.page({
  name = "Product Listing",
  userId = "user-42",
  properties = {
    url = "/products/widgets",
    category = "Widgets",
    referrer = "https://google.com"
  }
})
```

---

## group

Associate a user with a group (organization, company, account) in Segment.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `groupId` | string | yes | The unique identifier for the group |
| `userId` | string | yes | The unique identifier for the user |
| `traits` | object | no | Key-value pairs of group traits (name, plan, industry) |

### Examples

```lua
-- Add user to an organization
app.integrations.segment.group({
  groupId = "org-123",
  userId = "user-42",
  traits = {
    name = "Acme Corp",
    plan = "enterprise",
    industry = "Technology",
    employee_count = 250
  }
})
```

---

## get_workspace

Get details of a Segment workspace by its slug. Requires an API token.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `slug` | string | yes | The workspace slug (e.g., "my-workspace") |

### Examples

```lua
-- Get workspace details
local ws = app.integrations.segment.get_workspace({
  slug = "my-workspace"
})
print(ws.name)
print(ws.id)
```

---

## list_sources

List all sources in a Segment workspace. Requires an API token.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `slug` | string | yes | The workspace slug |

### Examples

```lua
-- List all sources
local result = app.integrations.segment.list_sources({
  slug = "my-workspace"
})

for _, source in ipairs(result.sources or {}) do
  print(source.name .. " (" .. source.id .. ")")
end
```

---

## get_source

Get details of a specific Segment source. Requires an API token.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `slug` | string | yes | The workspace slug |
| `id` | string | yes | The source ID |

### Examples

```lua
-- Get source details
local source = app.integrations.segment.get_source({
  slug = "my-workspace",
  id = "abc123"
})
print(source.name)
print(source.write_key)
```

---

## get_current_user

Get the currently authenticated Segment user. Requires an API token. Useful for verifying credentials.

### Parameters

None.

### Examples

```lua
-- Verify API token is working
local user = app.integrations.segment.get_current_user({})
print("Authenticated as: " .. user.email)
```

---

## Multi-Account Usage

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

```lua
-- Default account (always works)
app.integrations.segment.track({...})

-- Explicit default (portable across setups)
app.integrations.segment.default.track({...})

-- Named accounts
app.integrations.segment.production.track({...})
app.integrations.segment.staging.track({...})
```

All functions are identical across accounts — only the credentials differ.
Metadata-derived Lua example
local result = app.integrations.segment.identify_user({userId = "example_userId", traits = "example_traits"})
print(result)

Functions

identify_user Write

Identify a user in Segment with their traits. Links metadata about a user (name, email, plan, etc.) to a known userId so all their events can be attributed correctly.

Lua path
app.integrations.segment.identify_user
Full name
segment.segment_identify
ParameterTypeRequiredDescription
userId string yes The unique identifier for the user in your database.
traits object no Key-value pairs of user traits (e.g., name, email, plan, role, company).
track_event Write

Track a custom event for a user in Segment. Records actions your users perform along with optional properties describing the action.

Lua path
app.integrations.segment.track_event
Full name
segment.segment_track
ParameterTypeRequiredDescription
event string yes The name of the event being tracked (e.g., "Order Completed", "Button Clicked").
userId string yes The unique identifier for the user in your database.
properties object no Key-value pairs of event properties (e.g., revenue, category, productId).
page_view Write

Record a page view in Segment. Tracks when a user views a page, along with optional properties about the page.

Lua path
app.integrations.segment.page_view
Full name
segment.segment_page
ParameterTypeRequiredDescription
name string yes The name of the page viewed (e.g., "Homepage", "Product Listing").
userId string yes The unique identifier for the user in your database.
properties object no Key-value pairs of page properties (e.g., url, referrer, title, path).
group Write

Associate a user with a group (organization, company, account) in Segment. Lets you record group membership along with optional group traits.

Lua path
app.integrations.segment.group
Full name
segment.segment_group
ParameterTypeRequiredDescription
groupId string yes The unique identifier for the group (e.g., company ID, organization ID).
userId string yes The unique identifier for the user in your database.
traits object no Key-value pairs of group traits (e.g., name, plan, industry, employee_count).
get_workspace Read

Get details of a Segment workspace by its slug. Requires an API token to be configured.

Lua path
app.integrations.segment.get_workspace
Full name
segment.segment_get_workspace
ParameterTypeRequiredDescription
slug string yes The workspace slug (e.g., "my-workspace").
list_sources Read

List all sources in a Segment workspace. Requires an API token to be configured.

Lua path
app.integrations.segment.list_sources
Full name
segment.segment_list_sources
ParameterTypeRequiredDescription
slug string yes The workspace slug (e.g., "my-workspace").
get_source Read

Get details of a specific Segment source by ID. Requires an API token to be configured.

Lua path
app.integrations.segment.get_source
Full name
segment.segment_get_source
ParameterTypeRequiredDescription
slug string yes The workspace slug (e.g., "my-workspace").
id string yes The source ID (e.g., "abc123").
get_current_user Read

Get the currently authenticated Segment user. Useful for verifying API token credentials are correct.

Lua path
app.integrations.segment.get_current_user
Full name
segment.segment_get_current_user
ParameterTypeRequiredDescription
No parameters.