---
title: API
description: Look up registry options, definers, callers, and context members.
---

`init` types definitions and creates the registry factory.

## `init`

```ts
const { query, mutation, action, event, listener, cron, error, createRegistry } = init<Config>();
```

{/* source: packages/logic-sdk/src/core/types.ts (Config) */}

| Field | Type |
|---|---|
| `scope` | Required. App-defined scope. |
| `db` | Shared database type or `ReadWrite<Reader, Tx>`. |
| `services` | Dependencies for actions, listeners, and crons. |
| `config` | Plain settings for every `ctx`. |
| `meta` | Metadata shape for functions, listeners, and crons. |

## `createRegistry`

Use the same `init` for the registry, functions, listeners, and crons.

Mutations need a database adapter. If `init` declares `db`, supply `database`. Supply `services` and `config` if their types require fields. Module keys prefix IDs.

<AutoTypeTable path="../../packages/logic-sdk/src/core/types.ts" name="RegistryOptions" />

By default, `wrap` runs `next()`, `onError` logs to `console.error`, and `now` reads the clock.

### Database options

<AutoTypeTable path="../../packages/logic-sdk/src/core/types.ts" name="DatabaseOptions" />

### Job options

<AutoTypeTable path="../../packages/logic-sdk/src/core/types.ts" name="JobsOptions" />

<AutoTypeTable path="../../packages/logic-sdk/src/core/types.ts" name="JobScope" />

See [when `load` is required](/docs/concepts/jobs#scope).

## Registry and caller

{/* source: packages/logic-sdk/src/core/types.ts (Registry, Caller, RunArgs) */}

| Member | Result |
|---|---|
| `registry.as(scope)` | A caller bound to the scope. |
| `registry.definitions` | Registered definitions and their IDs. |
| `registry.idOf(definition)` | Registered ID. Throws for unregistered definitions. |
| `registry.jobs` | [Job operations](/docs/concepts/jobs#operations). |
| `caller.scope` | Bound scope. |
| `caller.run(fn, input?, options?)` | Output or a thrown error. |
| `caller.try(fn, input?, options?)` | `{ ok: true, value }` or `{ ok: false, error }`. Unexpected exceptions still throw. |

Omit input only if its type permits `undefined`.

### Call options

<AutoTypeTable path="../../packages/logic-sdk/src/core/types.ts" name="RunOptions" />

## Context members

| Member | Query | Mutation | Action | Listener | Cron |
|---|---|---|---|---|---|
| `scope`, `config`, `error`, `now()` | Yes | Yes | Yes | Yes | Yes |
| `run` | Queries | Queries and mutations | All functions | All functions | All functions |
| `db` | Reader | Transaction | | | |
| `services`, `signal` | | | Yes | Yes | Yes |
| `emit`, `schedule` | | Yes | | | |
| `event` | | | | Yes | |
| `cron` | | | | | Yes |

- **`db`.** Nested queries use the open transaction.
- **`event`.** Includes `id`, `name`, parsed `payload`, `occurredAt`, and `attempt`.
- **`cron`.** Includes `id`, `scheduledAt`, and `attempt`.
- **`signal`.** Comes from the call or the listener's timeout.

{/* source: packages/logic-sdk/src/core/types.ts (context interfaces) */}

## Definers

Each definer's `id` accepts ASCII letters, digits, `_`, and `-`. It defaults to the export name. Preserve IDs that queued jobs or clients reference.

### `query`, `mutation`, `action`

{/* source: packages/logic-sdk/src/core/types.ts (Def) */}

| Field | Value |
|---|---|
| `handler` | Required. Async `(ctx, input)`. |
| `input` | Input schema. Omit for a function without input. |
| `output` | Output schema. Omit to return the handler's value unchanged. |
| `authorize` | Sync policy. A type guard narrows `ctx.scope`. [Publish only with a policy](/docs/concepts/authorization#publishing). |
| `meta` | Metadata typed by `init`. |

### `event` and `error`

{/* source: packages/logic-sdk/src/core/registry.ts (init) */}

| Definer | Field | Value |
|---|---|---|
| `event` | `payload` | Required schema. |
| `error` | `kind` | Any kind except `invalid`. Defaults to `rejected`. |
| `error` | `details` | Sync schema. The error carries the parsed details. |
| `error` | `message` | String with optional `$field` or function of parsed details. Defaults by kind. |
| `error` | `meta` | Error metadata. |

### `listener` and `cron`

{/* source: packages/logic-sdk/src/core/registry.ts (init, CronDef) */}

| Definer | Field | Value |
|---|---|---|
| Both | `handler` | Required. Async `(ctx, payload)` or, for a cron, `(ctx)`. |
| Both | `attempts` | Total attempts. Default 1. |
| Both | `queue` | Declared queue. Default `default`. |
| Both | `meta` | Metadata typed by `init`. |
| Both | `retry` | Sync predicate for application errors. Default `conflict`. |
| Both | `failed` | Async `(ctx, payload, error)` or, for a cron, `(ctx, error)`. |
| Listener | `event` | Required. Returns the event definition. |
| Listener | `when` | Sync payload filter. Default accepts all. |
| Listener | `scope` | Sync function of parsed payload. Default emitter scope. |
| Listener | `delay` | Seconds before first attempt. Default 0. |
| Listener | `key` | Value for ordered delivery. Exclusive with `unique`. |
| Listener | `unique` | Value for dropping later jobs while one waits. |
| Listener | `timeout` | Seconds per attempt. Aborts `ctx.signal`. |
| Cron | `schedule` | Required five-field schedule. |
| Cron | `timezone` | IANA time zone. Default UTC. |
| Cron | `scope` | Required whole scope. |

## Type helpers

{/* source: packages/logic-sdk/src/core/types.ts */}

| Type | Result |
|---|---|
| `Input<typeof fn>` | Input before validation. Also accepts a schema type. |
| `Output<typeof fn>` | Return type. For a schema, the parsed type. |
| `AppErrorOf<typeof registry>` | Error union typed by code. |
| `CallerOf<typeof registry>` | Caller with the registry's scope and errors. |
| `EventOf<typeof registry>` | Union of `{ name, payload }` with unparsed payload types. |

## See also

- [Functions](/docs/concepts/functions)
- [Adapters](/docs/reference/adapters)
- [Errors](/docs/reference/errors)
