Skip to content

Client-Side Clients

Call your procedures remotely as if they were local functions.

Installation

sh
npm install @orpc/client@latest
sh
yarn add @orpc/client@latest
sh
pnpm add @orpc/client@latest
sh
bun add @orpc/client@latest
sh
deno install npm:@orpc/client@latest

Creating a Client

This guide uses RPCLink, so make sure your server is set up with RPCHandler or any API that follows the RPC Protocol.

ts
import { createORPCClient } from '@orpc/client'
import { RPCLink } from '@orpc/client/fetch'
import { RouterClient } from '@orpc/server'
import { ContractRouterClient } from '@orpc/contract'

const link = new RPCLink({
  url: 'http://localhost:3000/rpc',
  headers: () => ({
    authorization: 'Bearer token',
  }),
  // fetch: <-- provide fetch polyfill fetch if needed
})

// Create a client for your router
const client: RouterClient<typeof router> = createORPCClient(link)
// Or, create a client using a contract
const client: ContractRouterClient<typeof contract> = createORPCClient(link)

TIP

You can export RouterClient<typeof router> and ContractRouterClient<typeof contract> from server instead.

Calling Procedures

Once your client is set up, you can call your procedures as if they were local functions.

ts
const 
planet
= await
client
.
planet
.
find
({
id
: 1 })
client
.
planet
.
create

Merge Clients

In oRPC, a client is a simple object-like structure. To merge multiple clients, you simply assign each client to a property in a new object:

ts
const clientA: RouterClient<typeof routerA> = createORPCClient(linkA)
const clientB: RouterClient<typeof routerB> = createORPCClient(linkB)
const clientC: RouterClient<typeof routerC> = createORPCClient(linkC)

export const orpc = {
  a: clientA,
  b: clientB,
  c: clientC,
}

Released under the MIT License.