Skip to content

Mental model

Beignet gives a TypeScript application a shared architecture for APIs, business workflows, and infrastructure. Features follow the same conventions as they gain persistence, authorization, background work, and other responsibilities.

Use this page to understand the decisions that structure a Beignet app. Then follow the quickstart to run one, or the Projects tutorial to build a feature through that architecture.

Give each responsibility a home

As a feature grows, it needs answers to three questions:

  • How do callers use it? Contracts describe the HTTP API and share its validation and types with clients. Routes connect that API to the work.
  • What rules must the operation follow? Use cases own business workflows, including authorization. Keeping a rule here lets HTTP handlers, jobs, and scripts call the same operation with an appropriate context.
  • Which services does it need? Ports describe dependencies such as a repository or mailer. Infrastructure adapters supply the implementation, while tests can supply controlled alternatives.

These responsibilities have named locations and dependency rules. A developer or coding agent can find the operation, understand its dependencies, and change its behavior without choosing a new organization for every feature. The app architecture reference maps those responsibilities to files.

Follow one request

For a request to create a todo, the contract describes the URL, input, responses, and errors. A route connects that contract to a use case, which checks permission and performs the work. The use case accesses storage through a port — a typed interface — implemented by a database adapter.

The frontend imports the contract to type its API call and validate its form. A job or script can call the use case directly. The HTTP definition and the business operation have separate responsibilities, so each can be reused without pulling server implementation code into the browser.

Add capabilities as the feature grows

The full-stack Next.js starter includes sign-in, persistent Todos with ownership checks, and a UI. Next.js handles pages and rendering; Beignet organizes the API and application behavior. Other runtimes can use the Web Fetch adapter.

Jobs, notifications, uploads, and schedules have feature-owned definitions, app-level wiring, and providers for external services. Add them with generators when needed, configure their providers, and choose the runtime that will execute them. For example, the background-job guide extends the starter's Todos feature with work that is saved in a transaction and delivered through an outbox. It uses the same feature structure, dependencies, and testing model.

Start with the responsibilities your feature has today. A focused endpoint can use a contract and a handler, as the existing Next.js app guide shows. Introduce a use case when an operation owns business rules or needs to run from another entrypoint; introduce a port when it needs an external dependency. See Route handler or use case?.

Check the structure as you change it

beignet lint checks dependency directions, including runtime imports from use cases into infrastructure. beignet doctor diagnoses application wiring. beignet check runs both alongside the app's configured lint, typecheck, and test scripts. Your feature tests supply the expected behavior; these tools help catch structural mistakes and regressions as you work.

Developers and coding agents use the same conventions and checks. The agent workflow adds source inspection, scaffolding, and guidance for the installed package versions.

The vocabulary below describes the parts of this application model.

The HTTP boundary

  • Contract — describes one HTTP endpoint: method, path, parameters, request body, response statuses, and metadata. Contracts feed server validation, typed clients, React Query and form helpers, and OpenAPI generation. See Contracts.
  • Route group — a feature-owned list that maps contracts to use cases, defined in features/<feature>/routes.ts and composed centrally in server/routes.ts. See Routes and server.
  • Hook — an ordered lifecycle function for infrastructure behavior at the HTTP boundary: auth, CORS, rate limits, logging, response shaping, and error mapping. Hooks can short-circuit before the handler, enrich context, observe responses, or map errors. See Hooks.
  • Context — the per-request value (ctx) the server builds from your context blueprint: the actor, the session, the request id, and the app's ports. Use cases and hooks read everything through it.
  • Error catalog — the app-owned set of named business errors in features/shared/errors.ts. Contracts declare them with .errors(...), use cases throw them, and clients receive them typed. Framework-owned responses such as validation failures stay distinguishable from catalog errors. See Errors and Request lifecycle for how response ownership works.

Application code

  • Use case — a validated application workflow (a command or a query) with typed input and output. The same use case can run from HTTP routes, jobs, schedules, scripts, and tests. See Use cases.
  • Policy — feature-owned business authorization rules. Hooks decide who is signed in; policies decide what they may do. See Authorization.
  • Actor, tenant, and gate — the actor is who is acting, the tenant is the organization scope they act in, and the gate checks policies against both. All three live on the request context.
  • Domain — optional helpers for entities, value objects, and domain events when a feature wants more structure around core business concepts. See Domain modeling.

Dependencies

  • Port — an app-facing dependency interface, used through ctx.ports. Feature repositories live in features/<feature>/ports.ts; app-wide ports live in ports/. See Ports and adapters.
  • Adapter — a concrete implementation of a port, kept in infra/ and wired in infra/port-wiring.ts.
  • Provider — a package that installs or replaces ports at server startup: Drizzle, Redis, Pino, Better Auth, Inngest, mail services, and more. Tests pass mock ports directly instead. See Providers.
  • Unit of Work (UoW) — the transaction boundary at ctx.ports.uow.transaction(...). It commits repository writes together and records events and outbox messages that run after commit. See Database and transactions.

Workflow primitives

Beyond the request path, Beignet names background concepts by the question they answer:

  • Event — "this fact happened"; listeners react to it.
  • Job — "do this work later or outside the request."
  • Best-effort work — "try this after the operation; losing it is acceptable." Use it for non-durable hints after the authoritative mutation succeeds. See Ports and adapters.
  • Schedule — "start this workflow at this time."
  • Notification — "tell a person or team about this."
  • Task — "run this operational entrypoint", such as a backfill.
  • Idempotency key — "this logical command may arrive again."
  • Outbox record — "this side effect must commit with the database write."

See Workflow primitives for the decision table and the common combinations.

An agent capability is a typed, validated entrypoint that exposes an existing application use case to an authenticated AI agent transport. Beignet owns its registry and execution boundary; integrations own agent identity, grants, and protocol transport. See Agent capabilities.

API grammar

Beignet APIs follow one naming rule, so new packages feel predictable:

  • defineX declares something you register: contracts, routes, ports, errors, events, jobs, schedules, policies.
  • createX builds a runtime object you call: servers, clients, providers, and the per-capability factories such as createJobs<AppContext>() that return app-bound defineJob builders.

Builders are immutable: each chained method refines the definition and returns the next builder. Contract-aware adapters then accept the contract you already exported:

typescript
export const createPost = posts
  .post("/")
  .body(CreatePostInputSchema)
  .responses({ 201: PostSchema });

const endpoint = client.endpoint(createPost);
const mutation = rq(createPost).mutationOptions();
const form = rhf(createPost).useForm();

Define the shape once, then bind it to the runtime surface you need. That is the transfer rule behind every Beignet integration.