os — the global builder
os is the root Builder instance. All procedure and router definitions start here.
Builder methods
.$context<TContext>()
Sets the expected initial context type for all procedures. Call this first when you want type-safe context.
.$config(config)
Overrides builder-level configuration.
.$meta<TMeta>(initialMeta)
Sets the initial metadata type for procedures.
.$route(initialRoute)
Sets the initial route definition (useful as a router-level default).
.$input<TSchema>(initialSchema)
Sets the initial input schema. Procedures inherit this schema.
.use(middleware)
Adds middleware to the pipeline. Middleware runs for every procedure defined after this call.
.errors(errorMap)
Registers type-safe errors. Error constructors are available in procedure handlers via errors.*.
.meta(meta)
Sets or merges metadata onto subsequent procedures.
.route(route)
Sets or merges route options (method, path, tags, etc.) onto subsequent procedures.
.input(schema)
Defines the input validation schema for a procedure.
.output(schema)
Defines the output validation schema for a procedure.
.handler(fn)
Defines the procedure handler. Returns a DecoratedProcedure.
.prefix(path)
Prefixes all procedures in a router with the given path. Only affects procedures with explicit .route({ path: ... }).
.tag(...tags)
Adds OpenAPI tags to all procedures in the subsequent router.
.router(router)
Applies all accumulated options (middleware, errors, prefix, tags) to a router.
.lazy(loader)
Creates a lazy-loaded router. The loader is called only when the route is first matched.
.middleware(fn)
Creates a DecoratedMiddleware (useful for reuse and composition).
ORPCError
Thrown inside procedure handlers or middleware to signal known errors.
Properties
| Property | Type | Description |
|---|---|---|
code | string | The error code (e.g. 'NOT_FOUND') |
status | number | HTTP status code |
message | string | Human-readable message |
data | TData | Optional typed payload |
defined | boolean | true if registered in an error map |
cause | unknown | Original cause (from ErrorOptions) |
Common error codes
BAD_REQUEST (400), UNAUTHORIZED (401), FORBIDDEN (403), NOT_FOUND (404), CONFLICT (409), UNPROCESSABLE_CONTENT (422), INTERNAL_SERVER_ERROR (500)
Types
RouterClient<TRouter, TClientContext>
Infers a fully type-safe callable client type from a router. Nested procedures become nested async functions.
InferRouterInputs<TRouter>
Infers the input types of all procedures in a router.
InferRouterOutputs<TRouter>
Infers the output types of all procedures in a router.
createRouterClient()
Creates a server-side client for calling procedures directly (without HTTP).
lazy()
Wraps a dynamic import in a lazy router for improved cold start times.
EventPublisher
Publishes events to multiple async iterator subscribers. Used for SSE/streaming.
eventIterator()
Used in output schemas to declare that a procedure returns an event iterator (async iterable).
isDefinedError() and safe()
Lifecycle hooks
Hooks for observing procedure execution:| Hook | Called when |
|---|---|
onStart(fn) | Before the handler runs |
onSuccess(fn) | Handler completes successfully |
onError(fn) | Handler throws an error |
onFinish(fn) | Always, after success or error |
RPCHandler adapters
Handlers for different runtimes. All support plugins.
- Fetch
- Node.js
- AWS Lambda
