brick.ts

Services and Actions

Define modular backend services and typed actions with runtime schema validation.

llms.txt

Services structure backend domains in Brick-TS, while actions provide typed business logic executable both in-process and over HTTP.

For bidirectional JSON messages, declare a typed WebSocket with service.socket(). Socket handlers use the owning service's context and appear in the generated browser client.

Minimal Example

Define a service, register an action with TypeBox input/output schemas, and invoke it either directly in TypeScript or through HTTP:

src/services/users.ts
import { defineService, t } from "@brickkit/core";

// Mock data store
const users = new Map([
  ["usr_1", { id: "usr_1", name: "Ada Lovelace", email: "ada@example.com" }],
]);

// 1. Define the service domain
export const usersService = defineService("users");

// 2. Define a typed action
export const getUser = usersService.action({
  name: "getUser",
  description: "Retrieve a user profile by unique ID",
  method: "GET",
  path: "/:id", // GET /api/users/:id
  input: t.Object({
    id: t.String({ minLength: 1 }),
  }),
  output: t.Object({
    id: t.String(),
    name: t.String(),
    email: t.String(),
  }),
  execute: ({ input, error }) => {
    const user = users.get(input.id);
    if (!user) {
      // Error helpers throw; `return` lets TypeScript narrow `user`
      return error.NOT_FOUND(`User with ID '${input.id}' not found`);
    }
    return user;
  },
});

Brick doesn't register TypeBox string formats such as email or uuid. A schema with an unregistered format fails validation ("Unknown format"), so register formats with TypeBox's FormatRegistry before using them.

Services (defineService)

A service groups a database handle, a context resolver, resources, and custom actions:

src/services/orders.ts
import { defineDatabase, defineService } from "@brickkit/core";
import { ordersTable } from "./schema";

const database = defineDatabase({ tables: { ordersTable } });

export const ordersService = defineService("orders", {
  database,
  context: async (baseCtx) => {
    const token = baseCtx.request.headers.get("authorization");
    return { token, tenantId: "tenant_default" };
  },
});

Service Options Table

OptionTypeDescription
databaseDatabaseHandleDatabase handle created via defineDatabase(). Accessible in actions as ctx.db.
context(baseCtx: BaseContext) => Promise<T> | TContext resolver function. Injects user, session, or tenant properties into every action.
depsRecord<string, Port>Typed slots from port<T>() that brick({ deps }) fills. Read as ctx.deps. See Dependencies.

Duplicate names

Names must be unique. A service name is unique within the app. Action, tool, socket and resource names are unique within their service, and resource actions such as page.list count as action names. A duplicate fails at startup, and the error names both files:

Brick: duplicate action "ship" in service "orders"
  src/services/orders/ship.ts:12
  src/services/orders/legacy.ts:30

The same name defined again from the same file replaces the old definition. That is how brick dev reloads an edited file.

Actions (service.action / defineAction)

Actions are the core unit of compute in Brick-TS. Every action is registered with TypeBox schemas and an execution function.

Action Configuration Table

PropertyTypeDefaultDescription
namestring(Required)Unique name within the service (e.g. "createOrder").
descriptionstringundefinedHuman-readable description exported to the OpenAPI 3.1 operation.
inputTSchemaundefinedTypeBox schema for validating input payload or query parameters.
outputTSchemaundefinedTypeBox schema for validating return data. Mismatches throw ValidationError (status 500).
method"GET" | "POST" | "PUT" | "PATCH" | "DELETE""POST"HTTP method exposed on the API gateway.
pathstring${prefix}/${service}/${action}Route relative to the service: "/:id/summary" on orders mounts at /api/orders/:id/summary. Don't repeat the prefix. The default uses kebab-case (getUser → /api/users/get-user).
authorize(params) => boolean | Promise<boolean>undefinedGuard run after input validation. Returning false rejects with UNAUTHORIZED (401) without ctx.user, FORBIDDEN (403) with one.
errorsRecord<string, { status?, message }>undefinedCustom error catalog added to error helpers. status defaults to 400.
tagsstring[][service name]Extra OpenAPI tags, added after the service tag; also included in introspection.
emitsstring[]undefinedMetadata included in introspection.
execute({ input, ctx, error }) => Promise<T> | T(Required)Implementation function. Receives validated input, resolved context, and error helpers.

Monomorphic Execution (In-Process vs HTTP)

An action can be called in-process without network overhead or data serialization:

src/example-call.ts
import { getUser } from "./services/users";

// Direct in-process execution with full validation and authorization:
const user = await getUser({ input: { id: "usr_1" } });
console.log(user.name); // "Ada Lovelace"

// Alternative shorthand:
const result = await getUser.run({ id: "usr_1" });

When the app is served via brick(), the gateway mounts the action as an HTTP endpoint:

curl http://localhost:4000/api/users/usr_1

Both invocations run the same steps in the same order: input validation, authorize, execute, then output validation.

File Uploads

Put t.File() or t.Files() in an action's input to take a multipart/form-data body. The other fields arrive as form values, coerced by their schema like query parameters.

src/services/files.ts
export const uploadFile = filesService.action({
  name: "uploadFile",
  input: t.Object({
    file: t.File({ maxSize: 25 * 1024 * 1024 }),
    folder: t.Optional(t.String()),
  }),
  execute: async ({ input }) => {
    const bytes = new Uint8Array(await input.file.arrayBuffer());
    // ...
  },
});

When every file field declares maxSize (and t.Files also maxItems), a request whose Content-Length is over their sum plus 1 MiB is refused with PAYLOAD_TOO_LARGE (413) before its body is read. A body without Content-Length is still capped by Bun's maxRequestBodySize (128 MB by default) and checked against maxSize once read. The typed client sends FormData when a field holds a Blob, and OpenAPI describes the field as format: binary. MCP carries JSON only, so a tool can't take files: defineTool throws for a file field.

Path Parameters and Custom Routes

An explicit path is relative to the service, so path: "/:id" on users mounts at /api/users/:id. When the path has parameters, Brick merges them into input. Declare them in the input schema so they're validated; a parameter the schema types as a number is coerced to one. See URL Scheme for every route shape:

src/services/users.ts
export const deleteUser = usersService.action({
  name: "deleteUser",
  method: "DELETE",
  path: "/:id", // DELETE /api/users/:id
  input: t.Object({
    id: t.String(),
  }),
  output: t.Object({
    success: t.Boolean(),
  }),
  execute: async ({ input, error }) => {
    return { success: true };
  },
});

When called via HTTP DELETE /api/users/usr_1, Brick extracts id from the path and validates it against input. When called in-process, pass { id: "usr_1" } in the input object.