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