KosmoKrator

sales

Square Lua API for KosmoKrator Agents

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

7 functions 7 read 0 write Bearer token auth

Lua Namespace

Agents call this integration through app.integrations.square.*. Use lua_read_doc("integrations.square") 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.

Square Integration

Overview

The Square integration provides 7 tools for managing payments, customers, orders, and merchant information via the Square API v2.

Tools

ToolTypeDescription
square_list_paymentsreadList payments with optional filtering
square_get_paymentreadRetrieve a payment by ID
square_list_customersreadList customers with optional sorting
square_get_customerreadRetrieve a customer by ID
square_list_ordersreadList orders for a location
square_get_orderreadRetrieve an order by ID
square_get_current_userreadGet the authenticated merchant

Money amounts

Square returns money amounts as integers in the smallest currency unit (e.g., cents for USD). Each money object contains amount (integer) and currency (ISO 4217 code).

-- Example money object
{ amount = 1500, currency = "USD" }  -- $15.00

Authentication

Square uses a Bearer access token. Obtain one from the Square Developer Dashboard → Credentials.

Sandbox tokens start with EAAAEO... (sandbox mode). Production tokens are used for live data.

Payments

List Payments

app.integrations.square_list_payments({
  location_id = "LOCATION_ID",        -- optional: filter by location
  begin_time = "2024-01-01T00:00:00Z", -- optional: start of time range
  end_time = "2024-12-31T23:59:59Z",   -- optional: end of time range
  limit = 20,                          -- optional: 1–100, default 20
  cursor = nil,                        -- optional: pagination cursor
})
-- Returns:
-- {
--   payments = {
--     {
--       id = "...",
--       amount_money = { amount = 1500, currency = "USD" },
--       status = "COMPLETED",
--       source_type = "CARD",
--       created_at = "2024-01-15T10:30:00Z",
--       order_id = "...",
--       customer_id = "...",
--     },
--     ...
--   },
--   cursor = "next_page_cursor_or_nil",
-- }

Get Payment

app.integrations.square_get_payment({
  id = "PAYMENT_ID",
})
-- Returns:
-- {
--   id = "...",
--   amount_money = { amount = 1500, currency = "USD" },
--   tip_money = { amount = 200, currency = "USD" },
--   total_money = { amount = 1700, currency = "USD" },
--   status = "COMPLETED",
--   source_type = "CARD",
--   card_details = { ... },
--   processing_fee = { { amount = 54, currency = "USD" } },
--   order_id = "...",
--   customer_id = "...",
--   created_at = "...",
--   updated_at = "...",
-- }

Customers

List Customers

app.integrations.square_list_customers({
  limit = 20,                 -- optional: 1–100, default 20
  cursor = nil,               -- optional: pagination cursor
  sort_field = "CREATED_AT",  -- optional: DEFAULT, CREATED_AT, FAMILY_NAME, GIVEN_NAME
  sort_order = "DESC",        -- optional: ASC, DESC
})
-- Returns:
-- {
--   customers = {
--     {
--       id = "...",
--       given_name = "Jane",
--       family_name = "Doe",
--       email_address = "[email protected]",
--       phone_number = "+1234567890",
--       created_at = "...",
--     },
--     ...
--   },
--   cursor = "next_page_cursor_or_nil",
-- }

Get Customer

app.integrations.square_get_customer({
  id = "CUSTOMER_ID",
})
-- Returns:
-- {
--   id = "...",
--   given_name = "Jane",
--   family_name = "Doe",
--   email_address = "[email protected]",
--   phone_number = "+1234567890",
--   address = { ... },
--   company_name = "Acme Inc.",
--   note = "VIP customer",
--   cards = { ... },
--   created_at = "...",
--   updated_at = "...",
-- }

Orders

List Orders

app.integrations.square_list_orders({
  location_id = "LOCATION_ID",         -- required
  limit = 20,                          -- optional: 1–100, default 20
  cursor = nil,                        -- optional: pagination cursor
  states = "OPEN,COMPLETED",           -- optional: comma-separated states (OPEN, COMPLETED, CANCELED)
})
-- Returns:
-- {
--   orders = {
--     {
--       id = "...",
--       location_id = "...",
--       state = "COMPLETED",
--       total_money = { amount = 2500, currency = "USD" },
--       total_tax_money = { amount = 200, currency = "USD" },
--       total_discount_money = { amount = 0, currency = "USD" },
--       created_at = "...",
--       updated_at = "...",
--     },
--     ...
--   },
--   cursor = "next_page_cursor_or_nil",
-- }

Get Order

app.integrations.square_get_order({
  id = "ORDER_ID",
})
-- Returns:
-- {
--   id = "...",
--   location_id = "...",
--   state = "COMPLETED",
--   line_items = { ... },
--   total_money = { amount = 2500, currency = "USD" },
--   total_tax_money = { amount = 200, currency = "USD" },
--   total_discount_money = { amount = 0, currency = "USD" },
--   total_service_charge_money = { ... },
--   tenders = { ... },
--   returns = { ... },
--   customer_id = "...",
--   created_at = "...",
--   updated_at = "...",
--   closed_at = "...",
-- }

Current User

Get Current User

app.integrations.square_get_current_user({})
-- Returns:
-- {
--   id = "...",
--   business_name = "My Store",
--   country = "US",
--   currency = "USD",
--   status = "ACTIVE",
--   main_location_id = "...",
--   created_at = "...",
-- }

Pagination

Square uses cursor-based pagination. When a response includes a cursor field with a non-null value, pass it to the next request to retrieve the next page.

-- Paginate through all payments
local all_payments = {}
local cursor = nil

repeat
  local result = app.integrations.square_list_payments({
    limit = 100,
    cursor = cursor,
  })

  for _, payment in ipairs(result.payments or {}) do
    table.insert(all_payments, payment)
  end

  cursor = result.cursor
until cursor == nil or cursor == ""

-- all_payments now contains all pages

Common Workflows

Find a customer by email, then list their payments

-- List customers and find by email
local customers = app.integrations.square_list_customers({ limit = 100 })
local target = nil
for _, c in ipairs(customers.customers or {}) do
  if c.email_address == "[email protected]" then
    target = c
    break
  end
end

if target then
  -- List payments (filter client-side by customer_id)
  local payments = app.integrations.square_list_payments({ limit = 100 })
  local customer_payments = {}
  for _, p in ipairs(payments.payments or {}) do
    if p.customer_id == target.id then
      table.insert(customer_payments, p)
    end
  end
end

Verify connection and get merchant info

local merchant = app.integrations.square_get_current_user({})
print("Connected as: " .. merchant.business_name)
print("Country: " .. merchant.country)
print("Currency: " .. merchant.currency)

Notes

  • Money amounts are in the smallest currency unit (cents for USD). Divide by 100 to get the dollar amount.
  • Order IDs use the format ORDER_ID at a specific location. Use list_orders with a location_id to find orders.
  • Location ID is required for listing orders. You can find locations via the Square Dashboard or by using get_current_user to find the main_location_id.
  • Rate limits — Square enforces rate limits per access token. If you hit limits, implement backoff.
  • Sandbox vs Production — Use sandbox tokens for testing. All API calls behave identically but operate on test data.

Multi-Account Usage

If multiple Square accounts are configured:

-- Use the default account
app.integrations.square_list_payments({ limit = 10 })

-- Use a named account
app.integrations.square_list_payments({ limit = 10, account = "store-2" })
Raw agent markdown
# Square Integration

## Overview

The Square integration provides 7 tools for managing payments, customers, orders, and merchant information via the Square API v2.

### Tools

| Tool | Type | Description |
|------|------|-------------|
| `square_list_payments` | read | List payments with optional filtering |
| `square_get_payment` | read | Retrieve a payment by ID |
| `square_list_customers` | read | List customers with optional sorting |
| `square_get_customer` | read | Retrieve a customer by ID |
| `square_list_orders` | read | List orders for a location |
| `square_get_order` | read | Retrieve an order by ID |
| `square_get_current_user` | read | Get the authenticated merchant |

### Money amounts

Square returns money amounts as integers in the smallest currency unit (e.g., cents for USD). Each money object contains `amount` (integer) and `currency` (ISO 4217 code).

```lua
-- Example money object
{ amount = 1500, currency = "USD" }  -- $15.00
```

## Authentication

Square uses a Bearer access token. Obtain one from the **Square Developer Dashboard → Credentials**.

Sandbox tokens start with `EAAAEO...` (sandbox mode). Production tokens are used for live data.

## Payments

### List Payments

```lua
app.integrations.square_list_payments({
  location_id = "LOCATION_ID",        -- optional: filter by location
  begin_time = "2024-01-01T00:00:00Z", -- optional: start of time range
  end_time = "2024-12-31T23:59:59Z",   -- optional: end of time range
  limit = 20,                          -- optional: 1–100, default 20
  cursor = nil,                        -- optional: pagination cursor
})
-- Returns:
-- {
--   payments = {
--     {
--       id = "...",
--       amount_money = { amount = 1500, currency = "USD" },
--       status = "COMPLETED",
--       source_type = "CARD",
--       created_at = "2024-01-15T10:30:00Z",
--       order_id = "...",
--       customer_id = "...",
--     },
--     ...
--   },
--   cursor = "next_page_cursor_or_nil",
-- }
```

### Get Payment

```lua
app.integrations.square_get_payment({
  id = "PAYMENT_ID",
})
-- Returns:
-- {
--   id = "...",
--   amount_money = { amount = 1500, currency = "USD" },
--   tip_money = { amount = 200, currency = "USD" },
--   total_money = { amount = 1700, currency = "USD" },
--   status = "COMPLETED",
--   source_type = "CARD",
--   card_details = { ... },
--   processing_fee = { { amount = 54, currency = "USD" } },
--   order_id = "...",
--   customer_id = "...",
--   created_at = "...",
--   updated_at = "...",
-- }
```

## Customers

### List Customers

```lua
app.integrations.square_list_customers({
  limit = 20,                 -- optional: 1–100, default 20
  cursor = nil,               -- optional: pagination cursor
  sort_field = "CREATED_AT",  -- optional: DEFAULT, CREATED_AT, FAMILY_NAME, GIVEN_NAME
  sort_order = "DESC",        -- optional: ASC, DESC
})
-- Returns:
-- {
--   customers = {
--     {
--       id = "...",
--       given_name = "Jane",
--       family_name = "Doe",
--       email_address = "[email protected]",
--       phone_number = "+1234567890",
--       created_at = "...",
--     },
--     ...
--   },
--   cursor = "next_page_cursor_or_nil",
-- }
```

### Get Customer

```lua
app.integrations.square_get_customer({
  id = "CUSTOMER_ID",
})
-- Returns:
-- {
--   id = "...",
--   given_name = "Jane",
--   family_name = "Doe",
--   email_address = "[email protected]",
--   phone_number = "+1234567890",
--   address = { ... },
--   company_name = "Acme Inc.",
--   note = "VIP customer",
--   cards = { ... },
--   created_at = "...",
--   updated_at = "...",
-- }
```

## Orders

### List Orders

```lua
app.integrations.square_list_orders({
  location_id = "LOCATION_ID",         -- required
  limit = 20,                          -- optional: 1–100, default 20
  cursor = nil,                        -- optional: pagination cursor
  states = "OPEN,COMPLETED",           -- optional: comma-separated states (OPEN, COMPLETED, CANCELED)
})
-- Returns:
-- {
--   orders = {
--     {
--       id = "...",
--       location_id = "...",
--       state = "COMPLETED",
--       total_money = { amount = 2500, currency = "USD" },
--       total_tax_money = { amount = 200, currency = "USD" },
--       total_discount_money = { amount = 0, currency = "USD" },
--       created_at = "...",
--       updated_at = "...",
--     },
--     ...
--   },
--   cursor = "next_page_cursor_or_nil",
-- }
```

### Get Order

```lua
app.integrations.square_get_order({
  id = "ORDER_ID",
})
-- Returns:
-- {
--   id = "...",
--   location_id = "...",
--   state = "COMPLETED",
--   line_items = { ... },
--   total_money = { amount = 2500, currency = "USD" },
--   total_tax_money = { amount = 200, currency = "USD" },
--   total_discount_money = { amount = 0, currency = "USD" },
--   total_service_charge_money = { ... },
--   tenders = { ... },
--   returns = { ... },
--   customer_id = "...",
--   created_at = "...",
--   updated_at = "...",
--   closed_at = "...",
-- }
```

## Current User

### Get Current User

```lua
app.integrations.square_get_current_user({})
-- Returns:
-- {
--   id = "...",
--   business_name = "My Store",
--   country = "US",
--   currency = "USD",
--   status = "ACTIVE",
--   main_location_id = "...",
--   created_at = "...",
-- }
```

## Pagination

Square uses cursor-based pagination. When a response includes a `cursor` field with a non-null value, pass it to the next request to retrieve the next page.

```lua
-- Paginate through all payments
local all_payments = {}
local cursor = nil

repeat
  local result = app.integrations.square_list_payments({
    limit = 100,
    cursor = cursor,
  })

  for _, payment in ipairs(result.payments or {}) do
    table.insert(all_payments, payment)
  end

  cursor = result.cursor
until cursor == nil or cursor == ""

-- all_payments now contains all pages
```

## Common Workflows

### Find a customer by email, then list their payments

```lua
-- List customers and find by email
local customers = app.integrations.square_list_customers({ limit = 100 })
local target = nil
for _, c in ipairs(customers.customers or {}) do
  if c.email_address == "[email protected]" then
    target = c
    break
  end
end

if target then
  -- List payments (filter client-side by customer_id)
  local payments = app.integrations.square_list_payments({ limit = 100 })
  local customer_payments = {}
  for _, p in ipairs(payments.payments or {}) do
    if p.customer_id == target.id then
      table.insert(customer_payments, p)
    end
  end
end
```

### Verify connection and get merchant info

```lua
local merchant = app.integrations.square_get_current_user({})
print("Connected as: " .. merchant.business_name)
print("Country: " .. merchant.country)
print("Currency: " .. merchant.currency)
```

## Notes

- **Money amounts** are in the smallest currency unit (cents for USD). Divide by 100 to get the dollar amount.
- **Order IDs** use the format `ORDER_ID` at a specific location. Use `list_orders` with a `location_id` to find orders.
- **Location ID** is required for listing orders. You can find locations via the Square Dashboard or by using `get_current_user` to find the `main_location_id`.
- **Rate limits** — Square enforces rate limits per access token. If you hit limits, implement backoff.
- **Sandbox vs Production** — Use sandbox tokens for testing. All API calls behave identically but operate on test data.

## Multi-Account Usage

If multiple Square accounts are configured:

```lua
-- Use the default account
app.integrations.square_list_payments({ limit = 10 })

-- Use a named account
app.integrations.square_list_payments({ limit = 10, account = "store-2" })
```

Metadata-Derived Lua Example

local result = app.integrations.square.square_get_current_user({})
print(result)

Functions

square_get_current_user

Get the current authenticated Square merchant account. Returns merchant details including business name, country, currency, and status.

Operation
Read read
Full name
square.square_get_current_user
ParameterTypeRequiredDescription
No parameters.

square_get_customer

Retrieve a Square customer by ID. Returns full customer details including email, phone, address, and cards on file.

Operation
Read read
Full name
square.square_get_customer
ParameterTypeRequiredDescription
id string yes Square customer ID.

square_get_order

Retrieve a Square order by ID. Returns full order details including line items, totals, taxes, and discounts.

Operation
Read read
Full name
square.square_get_order
ParameterTypeRequiredDescription
id string yes Square order ID.

square_get_payment

Retrieve a Square payment by ID. Returns full payment details including amount, status, card details, and processing fees.

Operation
Read read
Full name
square.square_get_payment
ParameterTypeRequiredDescription
id string yes Square payment ID.

square_list_customers

List Square customers with optional filtering. Supports pagination with cursor.

Operation
Read read
Full name
square.square_list_customers
ParameterTypeRequiredDescription
limit integer no Number of customers to return (1–100, default 20).
cursor string no Cursor for pagination — returned from a previous request.
sort_field string no Sort field (DEFAULT, CREATED_AT, FAMILY_NAME, GIVEN_NAME).
sort_order string no Sort order (ASC, DESC).

square_list_orders

List Square orders for a specific location. Requires a location_id. Supports filtering by order states and pagination with cursor.

Operation
Read read
Full name
square.square_list_orders
ParameterTypeRequiredDescription
location_id string yes Square location ID.
limit integer no Number of orders to return (1–100, default 20).
cursor string no Cursor for pagination — returned from a previous request.
states array no Filter by order states (OPEN, COMPLETED, CANCELED). Pass as a comma-separated string.

square_list_payments

List Square payments with optional filtering. Supports filtering by location ID, begin_time / end_time (ISO 8601), and pagination with cursor.

Operation
Read read
Full name
square.square_list_payments
ParameterTypeRequiredDescription
location_id string no Filter by location ID.
begin_time string no Start of time range (ISO 8601, e.g., "2024-01-01T00:00:00Z").
end_time string no End of time range (ISO 8601).
limit integer no Number of payments to return (1–100, default 20).
cursor string no Cursor for pagination — returned from a previous request.