---
title: Introduction
description: Write business logic once and run it from any caller.
---

Logic SDK holds the business logic of a TypeScript app: the layer between your routes and your database. You write each rule one time, as a function. Then a route, an AI agent, a job, a script or a test runs it the same way.

<Diagram caption="Every caller runs the same functions, with the same policy, transaction and events.">
  <Fan>
    <Node slot="from" title="Caller" subtitle="Route, agent, job, test">
      <Check>Runs as a scope</Check>
    </Node>
    <Node title="mutation" subtitle="One transaction" tone="accent">
      <Check>Policy passed</Check>
      <Check>Input is valid</Check>
    </Node>
    <Node title="Listeners" subtitle="Run after commit">
      <Check>Email, sync, AI</Check>
    </Node>
  </Fan>
</Diagram>

```ts
/** Confirm a pending booking */
export const confirm = mutation({
  input: z.object({ bookingId: z.uuid() }),
  authorize: staff,

  async handler(ctx, { bookingId }) {
    const booking = await findBooking(ctx, bookingId);
    if (booking.status !== "pending") throw ctx.error(notPending);

    // another module, in the same transaction
    await ctx.run(availability.hold, rangeOf(booking));
    // listeners get it after commit
    ctx.emit(confirmed, { bookingId });

    return setStatus(ctx, booking.id, "confirmed");
  },
});

// a route, an agent, a job or a test: bind who calls, then run
await registry.as(scope).run(bookings.confirm, { bookingId });
```

## What you get

- **One transaction across modules.** Nested mutations commit together. A failure anywhere rolls back all of them.
- **Side effects after commit.** A mutation emits events. Listeners send the email only when the data is safe.
- **A policy on every call.** `authorize` runs for a route, an agent, a job and a nested call.
- **Typed errors.** A caller switches on error codes, and TypeScript checks each code.
- **Tests without mocks.** Each test runs on a real database, in a transaction that rolls back.
- **Any runtime.** Node, Bun and Cloudflare Workers, with any framework. No decorators, classes or code generation.

## The five kinds

| Kind | Use it for | Transaction |
|---|---|---|
| `query` | Read data | The caller's, if any |
| `mutation` | Change data | One, shared by nested mutations |
| `action` | Payments, email, AI, any network call | None |
| `listener` | React to an event | None, runs after commit |
| `cron` | Work on a schedule | None |

Services (a mailer, a payment client) reach only actions, listeners and crons. A mutation can roll back, and an email cannot come back.

**[Installation](/docs/installation)**

Add the package and a database adapter.

**[Quickstart](/docs/quickstart)**

One module, from definition to test.
