Skip to content
Logic SDK
Esc
↑↓navigate↵open⌘Jpreview
On this page

oRPC procedures

Publish a function in an oRPC router and run it from a typed client.

toORPCProcedure exposes one authorized function as an oRPC procedure.

Define your router

Use @orpc/server 2.0 beta. Add the functions you want clients to reach.

import { toORPCProcedure } from "logic-sdk/orpc";

export const router = {
  bookings: { confirm: toORPCProcedure(bookings.confirm) },
};
  • Policies and schemas. See Publishing.
  • Procedure. Add oRPC middleware with .use() and metadata with .meta().

Bind the request's caller

import { RPCHandler } from "@orpc/server/fetch";

const rpc = new RPCHandler(router);

export async function handle(request: Request) {
  const { response } = await rpc.handle(request, {
    prefix: "/rpc",
    context: { caller: registry.as(await scopeFrom(request)) },
  });
  return response ?? new Response("Not found", { status: 404 });
}

Mount handle at /rpc. A signed-out caller can use public procedures. A protected function’s policy gives 403. For a session-only router, reject a missing session with ORPCError("UNAUTHORIZED") before binding the caller.

Run the procedure from the client

import { createORPCClient } from "@orpc/client";
import { RPCLink } from "@orpc/client/fetch";
import type { RouterClient } from "@orpc/server";

const link = new RPCLink({ origin: location.origin, url: "/rpc" });
const client: RouterClient<typeof router> = createORPCClient(link);
const booking = await client.bookings.confirm({ bookingId });

The client gets the confirmed booking with the function’s output type. For failures, see oRPC codes and response shape.

See also

Was this page helpful?