---
title: oRPC procedures
description: Publish a function in an oRPC router and run it from a typed client.
---

`toORPCProcedure` exposes one authorized function as an oRPC procedure.

1. **Define your router**

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

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

    export const router = {
      bookings: { confirm: toORPCProcedure(bookings.confirm) },
    };
    ```

    - **Policies and schemas.** See [Publishing](/docs/concepts/authorization#publishing).
    - **Procedure.** Add oRPC middleware with `.use()` and metadata with `.meta()`.

2. **Bind the request's caller**

    ```ts
    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.

3. **Run the procedure from the client**

    ```ts
    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](/docs/reference/errors#kinds-and-statuses).

## See also

- [Output](/docs/concepts/output)
- [HTTP routes](/docs/guides/http)
