---
title: Functions
description: The function kind controls what its handler can read, write, and run.
---

A function is a query, mutation, or action with a `handler` that receives `ctx` first.

<Diagram caption="The function kind limits what its handler can run and which effects it can perform.">
  <div style="display: flex; gap: 1rem; align-items: stretch;">
    <Node title="query" subtitle="Reads the database">
      <Check>Runs queries</Check>
    </Node>
    <Node title="mutation" subtitle="Writes in a transaction" tone="accent">
      <Check>Runs queries and mutations</Check>
      <Check>Emits events and schedules calls</Check>
    </Node>
    <Node title="action" subtitle="Uses external services">
      <Check>Runs any function</Check>
      <Check>Gets services</Check>
    </Node>
  </div>
</Diagram>

```ts twoslash
import { init } from "logic-sdk";
import { z } from "zod";

const { query } = init<{ scope: { accountId: string } }>();
const get = query({
  input: z.object({ propertyId: z.uuid() }),
  async handler() {
    return { nightlyRate: 120 };
  },
});
const properties = { get };
// ---cut---
/** Price a stay at the property's nightly rate. */
export const priceStay = query({
  input: z.object({ propertyId: z.uuid(), nights: z.number().int().positive() }),
  async handler(ctx, { propertyId, nights }) {
    const property = await ctx.run(properties.get, { propertyId });
    return { total: nights * property.nightlyRate };
  },
});
```

- **Context.** See [context members](/docs/reference/api#context-members). Read `ctx.now()` so tests can set the clock.
- **Database.** Declare `db: DrizzleDb<typeof db>` in the `init` config. A query's `ctx.db` reads only. A mutation's `ctx.db` is its transaction.
- **Cancellation.** The registry checks a call's signal before starting. Pass `ctx.signal` to external work in an action.

## Input

Omit `input` for a function without input. Its `input` is `undefined`. Run it with `run(fn)`.

Use `Input<typeof fn>` and `Output<typeof fn>` for a function or its schema. Derive form fields when the shapes match.

```ts
const fields = properties.create.input.pick({ name: true, nightlyRate: true });
```

`run` parses input itself. If parsing at the entry changes the input type, pass the original input. Use a separate schema and translate if the entry accepts different values.

## Helpers and nested calls

Put `ctx` first in helpers. A `QueryCtx` helper can read through a mutation's `ctx`.

Run other functions with `ctx.run`, not directly, so their policy, validation, and `wrap` apply.

Import across modules through their indexes. Use events to break write cycles. The registry reports the call path above 64 levels.

## Private functions

A private function is a function that the module's index does not export. Only `ctx.run` can run it.

Use a private function for a mutation that needs its action's checks. Put private functions shared across files in `data.ts`.

The call ID is the module name with `(private)`, or with the function's `id`. You cannot schedule a private function or run it from a registry or test caller. Test it through the definition that runs it.

## Internal functions

An internal function is a registered function that you do not publish. Other functions, listeners, and [verified callbacks](/docs/guides/callbacks) run it. Without `authorize`, the oRPC and AI integrations refuse to publish it.

## See also

[Authorization](/docs/concepts/authorization), [Transactions](/docs/concepts/transactions), [Output](/docs/concepts/output)
