KosmoKrator

data

Sanity Lua API for KosmoKrator Agents

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

Lua Namespace

Agents call this integration through app.integrations.sanity.*. Use lua_read_doc("integrations.sanity") 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 Sanity workflow without starting an interactive agent session.

Inline Lua call
kosmo integrations:lua --eval 'dump(app.integrations.sanity.query_documents({query = "example_query", params = "example_params"}))' --json
Read Lua docs headlessly
kosmo integrations:lua --eval 'print(docs.read("sanity"))' --json
kosmo integrations:lua --eval 'print(docs.read("sanity.query_documents"))' --json

Workflow file

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

workflow.lua
local sanity = app.integrations.sanity
local result = sanity.query_documents({query = "example_query", params = "example_params"})

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.sanity, app.mcp.*, docs.*, json.*, and regex.*. Use app.integrations.sanity.default.* or app.integrations.sanity.work.* when you configured named credential accounts.

MCP-only Lua

If the script only needs configured MCP servers and does not need Sanity, 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.

Sanity — Lua API Reference

sanity_query_documents

Query documents using GROQ (Graph-Relational Object Queries).

Parameters

NameTypeRequiredDescription
querystringyesGROQ query string
paramsobjectnoParameters referenced in the query as $paramName

GROQ Basics

GROQ is Sanity’s query language. Common patterns:

  • *[_type == "post"] — all documents of type “post”
  • *[_type == "post" && defined(slug)] — with a slug field
  • *[_type == "post"] | order(publishedAt desc) [0..9] — first 10, newest first
  • *[_type == "post"] {title, slug, publishedAt} — pick specific fields
  • *[_type == "post" && slug.current == $slug][0] — find by slug using params

Examples

-- Get all posts
local result = app.integrations.sanity.query_documents({
  query = '*[_type == "post"] {title, slug, publishedAt}'
})

for _, doc in ipairs(result.result) do
  print(doc.title)
end
-- Get a post by slug using params
local result = app.integrations.sanity.query_documents({
  query = '*[_type == "post" && slug.current == $slug][0]',
  params = { slug = "hello-world" }
})

print(result.result.title)

sanity_get_document

Retrieve a single document by its ID.

Parameters

NameTypeRequiredDescription
idstringyesThe document ID (e.g., "post-123")

Examples

local result = app.integrations.sanity.get_document({
  id = "post-123"
})

print(result.title)

sanity_create_document

Create a new document in the dataset.

Parameters

NameTypeRequiredDescription
documentobjectyesDocument data with a required _type field

Examples

local result = app.integrations.sanity.create_document({
  document = {
    _type = "post",
    title = "My New Post",
    body = "Hello world!"
  }
})

print("Created document: " .. result.results[1].id)

sanity_update_document

Update fields on an existing document.

Parameters

NameTypeRequiredDescription
idstringyesThe document ID to update
setobjectyesFields to update

Examples

local result = app.integrations.sanity.update_document({
  id = "post-123",
  set = {
    title = "Updated Title",
    published = true
  }
})

sanity_delete_document

Delete a document by its ID.

Parameters

NameTypeRequiredDescription
idstringyesThe document ID to delete

Examples

local result = app.integrations.sanity.delete_document({
  id = "post-123"
})

print(result.message)

sanity_list_projects

List all Sanity projects accessible to the authenticated user.

Parameters

None.

Examples

local result = app.integrations.sanity.list_projects()

for _, project in ipairs(result) do
  print(project.displayName .. " (" .. project.id .. ")")
end

sanity_get_current_user

Get the currently authenticated Sanity user.

Parameters

None.

Examples

local result = app.integrations.sanity.get_current_user()

print("Logged in as: " .. result.name .. " (" .. result.email .. ")")

Multi-Account Usage

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

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

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

-- Named accounts
app.integrations.sanity.production.function_name({...})
app.integrations.sanity.staging.function_name({...})

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

Raw agent markdown
# Sanity — Lua API Reference

## sanity_query_documents

Query documents using GROQ (Graph-Relational Object Queries).

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `query` | string | yes | GROQ query string |
| `params` | object | no | Parameters referenced in the query as `$paramName` |

### GROQ Basics

GROQ is Sanity's query language. Common patterns:

- `*[_type == "post"]` — all documents of type "post"
- `*[_type == "post" && defined(slug)]` — with a slug field
- `*[_type == "post"] | order(publishedAt desc) [0..9]` — first 10, newest first
- `*[_type == "post"] {title, slug, publishedAt}` — pick specific fields
- `*[_type == "post" && slug.current == $slug][0]` — find by slug using params

### Examples

```lua
-- Get all posts
local result = app.integrations.sanity.query_documents({
  query = '*[_type == "post"] {title, slug, publishedAt}'
})

for _, doc in ipairs(result.result) do
  print(doc.title)
end
```

```lua
-- Get a post by slug using params
local result = app.integrations.sanity.query_documents({
  query = '*[_type == "post" && slug.current == $slug][0]',
  params = { slug = "hello-world" }
})

print(result.result.title)
```

---

## sanity_get_document

Retrieve a single document by its ID.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `id` | string | yes | The document ID (e.g., `"post-123"`) |

### Examples

```lua
local result = app.integrations.sanity.get_document({
  id = "post-123"
})

print(result.title)
```

---

## sanity_create_document

Create a new document in the dataset.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `document` | object | yes | Document data with a required `_type` field |

### Examples

```lua
local result = app.integrations.sanity.create_document({
  document = {
    _type = "post",
    title = "My New Post",
    body = "Hello world!"
  }
})

print("Created document: " .. result.results[1].id)
```

---

## sanity_update_document

Update fields on an existing document.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `id` | string | yes | The document ID to update |
| `set` | object | yes | Fields to update |

### Examples

```lua
local result = app.integrations.sanity.update_document({
  id = "post-123",
  set = {
    title = "Updated Title",
    published = true
  }
})
```

---

## sanity_delete_document

Delete a document by its ID.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `id` | string | yes | The document ID to delete |

### Examples

```lua
local result = app.integrations.sanity.delete_document({
  id = "post-123"
})

print(result.message)
```

---

## sanity_list_projects

List all Sanity projects accessible to the authenticated user.

### Parameters

None.

### Examples

```lua
local result = app.integrations.sanity.list_projects()

for _, project in ipairs(result) do
  print(project.displayName .. " (" .. project.id .. ")")
end
```

---

## sanity_get_current_user

Get the currently authenticated Sanity user.

### Parameters

None.

### Examples

```lua
local result = app.integrations.sanity.get_current_user()

print("Logged in as: " .. result.name .. " (" .. result.email .. ")")
```

---

## Multi-Account Usage

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

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

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

-- Named accounts
app.integrations.sanity.production.function_name({...})
app.integrations.sanity.staging.function_name({...})
```

All functions are identical across accounts — only the credentials differ.
Metadata-derived Lua example
local result = app.integrations.sanity.query_documents({query = "example_query", params = "example_params"})
print(result)

Functions

query_documents Read

Query documents in Sanity using GROQ (Graph-Relational Object Queries). Returns matching documents with their fields.

Lua path
app.integrations.sanity.query_documents
Full name
sanity.sanity_query_documents
ParameterTypeRequiredDescription
query string yes GROQ query string (e.g., `*[_type == "post"]`). See https://www.sanity.io/docs/groq for syntax.
params object no Optional parameters referenced in the query as $paramName (e.g., `{"type": "post"}` used as `*[_type == $type]`).
get_document Read

Retrieve a single Sanity document by its ID. Returns the full document with all fields.

Lua path
app.integrations.sanity.get_document
Full name
sanity.sanity_get_document
ParameterTypeRequiredDescription
id string yes The document ID (e.g., "post-123" or a draft ID like "drafts.post-123").
create_document Write

Create a new document in the Sanity dataset. The document data must include a _type field matching a schema type.

Lua path
app.integrations.sanity.create_document
Full name
sanity.sanity_create_document
ParameterTypeRequiredDescription
document object yes Document data as a JSON object. Must include a "_type" field (e.g., {"_type": "post", "title": "Hello", "body": "World"}).
update_document Write

Update an existing Sanity document by applying a patch with the specified fields.

Lua path
app.integrations.sanity.update_document
Full name
sanity.sanity_update_document
ParameterTypeRequiredDescription
id string yes The document ID to update (e.g., "post-123").
set object yes Fields to update as a JSON object (e.g., {"title": "Updated Title", "published": true}). Only the specified fields are changed.
delete_document Write

Delete a document from the Sanity dataset by its ID. This action is permanent.

Lua path
app.integrations.sanity.delete_document
Full name
sanity.sanity_delete_document
ParameterTypeRequiredDescription
id string yes The document ID to delete (e.g., "post-123").
list_projects Read

List all Sanity projects accessible to the authenticated user. Requires a management API token.

Lua path
app.integrations.sanity.list_projects
Full name
sanity.sanity_list_projects
ParameterTypeRequiredDescription
No parameters.
get_current_user Read

Get the currently authenticated Sanity user. Useful for verifying credentials and checking user identity.

Lua path
app.integrations.sanity.get_current_user
Full name
sanity.sanity_get_current_user
ParameterTypeRequiredDescription
No parameters.