KosmoKrator

data

Sanity Lua API for KosmoKrator Agents

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

7 functions 4 read 3 write API token auth

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.

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.sanity_query_documents({
  query = "example_query",
  params = "example_params"
})
print(result)

Functions

sanity_query_documents

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

Operation
Read read
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]`).

sanity_get_document

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

Operation
Read read
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").

sanity_create_document

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

Operation
Write write
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"}).

sanity_update_document

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

Operation
Write write
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.

sanity_delete_document

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

Operation
Write write
Full name
sanity.sanity_delete_document
ParameterTypeRequiredDescription
id string yes The document ID to delete (e.g., "post-123").

sanity_list_projects

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

Operation
Read read
Full name
sanity.sanity_list_projects
ParameterTypeRequiredDescription
No parameters.

sanity_get_current_user

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

Operation
Read read
Full name
sanity.sanity_get_current_user
ParameterTypeRequiredDescription
No parameters.