brick.ts

Server & Environment Configuration

Complete configuration reference for brick() server options, environment variables, and secrets.

llms.txt

This reference documents all configuration options for the brick() HTTP server runtime, CLI environment variables, and secrets management.

Project and tooling settings (entry, paths, dev, build, gen and lint) live in brick.config.ts; runtime behaviour stays in brick().

CreateServerOptions for brick()

When constructing the server gateway via brick(options), configure the following settings:

OptionTypeDefaultDescription
portnumberundefinedAccepted by the type but not read by brick(). Pass the port to app.listen(port) instead.
prefixstring"/api"Base path for every action, resource, socket and plugin route: ${prefix}/<service>/<action>, ${prefix}/<service>/<resource>/.... See URL Scheme.
servicesService[]All registered servicesArray of services to mount. Defaults to getGlobalRegistry().list().
titlestring"Brick-TS API Mesh"Application title rendered in OpenAPI 3.1 and documentation headers.
versionstring"1.0.0"Application API version in OpenAPI info.version.
descriptionstringundefinedNarrative API description for OpenAPI specifications.
docsbooleantrueEnables or disables the OpenAPI specification, Scalar, and Swagger UI routes.
openApiPathstring"/openapi.json"Path where raw OpenAPI 3.1 JSON is served. Not affected by prefix.
docsPathstring"/docs"Path where Scalar interactive API documentation is mounted. Not affected by prefix.
swaggerPathstring"/swagger"Path where Swagger UI documentation is mounted. Not affected by prefix.
requestLoggingboolean | RequestLoggingOptionstrueLogs METHOD path → status (ms) per request. Options: { enabled?: boolean; logger?: (msg: string) => void }.
referenceboolean | ReferenceOptionsfalsetrue mounts a Scalar page at /reference. Options mount one only with a path; documents adds OpenAPI documents to the dashboard's APIs tab.
compilerbooleantrueWhen true, plugins prepare their work at server construction (CRUD prewarms its statements); false skips that.
compilationCompiledApplicationundefinedPrecompiled handlers (used by the bundle that brick build emits).
secrets{ source?, validate? }undefinedSecret source and boot validation. resolveSecrets() runs at construction unless validate: false.
endpointsEndpointDefinition[]undefinedRaw HTTP routes mounted outside the JSON action mesh.
corsboolean | string | string[] | (origin, request) => boolean | { origin?, credentials? }undefinedCORS for browser callers on other origins. Origins are compared exactly; others get no Access-Control-Allow-Origin. See CORS.
dashboardDashboardOptionsundefinedRead-only monitoring dashboard brick start serves on its own port, behind auth. Options: { port?: number; host?: string; auth?: { token?: string | string[]; authorize?: (request: Request) => boolean | Response | Promise<boolean | Response> }; historyDays?: number }. Required auth beyond loopback; turns on telemetry. See Monitoring Dashboard.
documentationstring | DocumentationOptionsundefinedFolder of .md/.mdx pages shown in the brick dev dashboard's Docs tab. Options: { dir: string; components?: string }. Relative paths start at the app entry's folder. Needs fumadocs-core and @mdx-js/mdx.

brick() always mounts GET /_health and GET /_brick/services at the root, regardless of prefix.

CORS

Browsers only let a page on another origin read your API when the response allows that origin. Set cors to name the origins:

src/app.ts
export default brick({
  services,
  cors: { origin: "https://app.example.com", credentials: true },
});

origin takes:

  • true (the default): only the app's own origin, as the browser sees it (it honors X-Forwarded-Proto and X-Forwarded-Host).
  • A string or an array: exact origins, such as "https://app.example.com". "*" is refused at startup.
  • A predicate (origin, request) => boolean, for rules such as preview deployments.

cors also takes the origin rule directly: cors: true, cors: "https://app.example.com", cors: [a, b] or cors: (origin) => ….

For an allowed origin, Brick echoes it in Access-Control-Allow-Origin and adds Vary: Origin. With credentials: true it also sends Access-Control-Allow-Credentials: true, so the browser sends cookies and reads the response. An origin that isn't allowed gets no CORS headers, and the browser blocks the call. Brick never reflects an origin the rule doesn't allow.

Preflight requests (OPTIONS with Access-Control-Request-Method) are answered with 204 before routing and plugin middleware, so a middleware doesn't refuse them. Responses from middleware carry the CORS headers too. An allowed preflight lists GET, POST, PUT, PATCH, DELETE and echoes the requested headers. CORS headers are also set on error responses, so the browser can read the error.

Environment Variables

The framework and CLI inspect the following environment variables:

PORT

  • Used by: brick dev, brick start, and compiled bundles (dist/brick/server.js). brick() itself does not read PORT.
  • Precedence: For dev/start, an explicit --port <number> flag wins over PORT, which wins over dev.port in brick.config.ts (brick dev only), which wins over the default 4000. Compiled bundles use PORT, falling back to the --port value given to brick build (default 4000).
  • Constraints: Must be an integer between 1 and 65535. Out-of-range values or non-numeric strings make the process exit with an error.

Secrets Management (@brickkit/core)

Brick-TS includes a secret referencing utility. Each defineSecret() call registers a reference; values resolve from process.env (or a custom SecretSource) when you read them. A secret without a default is required:

src/secrets.ts
import { defineSecret, resolveSecrets } from "@brickkit/core";

// 1. Declare references to environment variables:
export const dbPassword = defineSecret("DB_PASSWORD");
export const apiKey = defineSecret("STRIPE_API_KEY").default(""); // a default makes it optional
export const port = defineSecret("PORT").default("4000").transform(Number);

// 2. Access secrets at runtime:
const password = dbPassword.require(); // Throws if missing or invalid
const key = apiKey.value(); // Returns the value, or undefined when unset

// 3. Validate all declared secrets (brick() also does this at construction):
resolveSecrets(); // Throws one error listing every missing required secret

SecretRef methods: .default(value), .transform(fn), .validate(fn), .url(), .value(source?), .require(source?), and .isSet(source?). listSecrets() returns { name, hasDefault, isSet } metadata without values.

Overriding Secrets in Tests

In integration tests, override secret values without mutating process.env:

test/helpers.ts
import { overrideSecret, resetSecrets } from "@brickkit/core";

overrideSecret("DB_PASSWORD", "test-secret");

// Clears all overrides AND the secret registry:
resetSecrets();