oRPC
background

Caller

Make your procedures callable in oRPC.

Direct Procedure Calls

You can directly call a procedure if its Global Context can accept undefined. For security reasons, context cannot be passed when invoking such procedures directly.

import { ,  } from '@orpc/server'
import {  } from 'zod'
 
// ❌ Cannot call this procedure directly because undefined is not assignable to 'Context'
const  = .<{ : boolean }>().(() => 'pong')
e1() // Error: Procedure 'e1' cannot be called directly
This expression is not callable. Type 'Procedure<{ auth: boolean; }, undefined, undefined, undefined, string> & { prefix: (prefix: `/${string}`) => DecoratedProcedure<{ auth: boolean; }, undefined, undefined, undefined, string>; route: (opts: RouteOptions) => DecoratedProcedure<...>; use: (<UExtraContext extends Partial<...> | undefined = undefined>(midd...' has no call signatures.
// ✅ Can call this procedure directly because undefined is assignable to 'Context' const = .<{ : boolean } | undefined>().(() => 'pong') const = await () // Ok, output is 'pong' // ✅ Can call this procedure directly because undefined is assignable to 'Context' const = .(.({ : .() })).(({ }) => `Hello, ${}!`) const = .({ }) // call directly const = await ({ : 'World' }) // output is 'Hello, World!' // or through router const = await .({ : 'World' }) // output is 'Hello, World!'

Calling Procedures with Context

For context-sensitive calls, use a Procedure Caller. A Procedure Caller securely provides the required context during invocation.

import { ,  } from '@orpc/server'
 
type  = { ?: { : string } }
 
const  = .<>().(() => 'pong')
 
const  = ({
    : ,
    : async () => {
        // you can access headers, cookies, etc. here to create context
        return { : { : 'example' } }
    },
})
 
const  = await () // output is 'pong'

Now, you can provide context when invoking a procedure. Additionally, you can use gettingCaller as a Server Action.

Calling Routers with Shared Context

To call multiple procedures with shared context, use a Router Caller.

import { ,  } from '@orpc/server'
 
const  = .({
    : .(() => 'pong')
})
 
const  = ({
    : ,
    : {},
})
 
const  = await .() // result is 'pong'

Summary

  • Direct Calls: Use when no context is required, or the context accepts undefined.
  • Procedure Caller: Use for securely calling a single procedure with a specific context.
  • Router Caller: Use for securely calling multiple procedures with shared context.

oRPC provides flexible and secure ways to invoke procedures tailored to your application needs.

On this page