### Install oRPC Server and Client Packages Source: https://orpc.unnoq.com/docs/getting-started Installs the latest versions of the oRPC server and client packages using various package managers like npm, yarn, pnpm, bun, and deno. ```shell npm install @orpc/server@latest @orpc/client@latest ``` ```shell yarn add @orpc/server@latest @orpc/client@latest ``` ```shell pnpm add @orpc/server@latest @orpc/client@latest ``` ```shell bun add @orpc/server@latest @orpc/client@latest ``` ```shell deno add npm:@orpc/server@latest npm:@orpc/client@latest ``` -------------------------------- ### Create oRPC Client Source: https://orpc.unnoq.com/docs/getting-started Initializes an oRPC client using `createORPCClient` and `RPCLink` for making requests to the server. It configures the client with the server URL and authorization headers. ```typescript import type { RouterClient } from '@orpc/server' import { createORPCClient } from '@orpc/client' import { RPCLink } from '@orpc/client/fetch' const link = new RPCLink ({ url : 'http://127.0.0.1:3000', headers : { Authorization : 'Bearer token' }, }) export const orpc : RouterClient = createORPCClient ( link ) ``` -------------------------------- ### Call oRPC Procedures Source: https://orpc.unnoq.com/docs/getting-started Demonstrates how to call oRPC procedures (e.g., `find`, `create`) using the generated client. It highlights the end-to-end type-safety and auto-completion features. ```typescript const planet = await orpc . planet . find ({ id : 1 }) orpc . planet . create ``` -------------------------------- ### Create Node.js Server for oRPC Source: https://orpc.unnoq.com/docs/getting-started Sets up an HTTP server using Node.js to handle oRPC requests. It integrates the RPCHandler, CORSPlugin, and an error interceptor for logging. ```typescript import { createServer } from 'node:http' import { RPCHandler } from '@orpc/server/node' import { CORSPlugin } from '@orpc/server/plugins' import { onError } from '@orpc/server' const handler = new RPCHandler ( router , { plugins : [new CORSPlugin ()], interceptors : [ onError (( error ) => { console . error ( error ) }), ], }) const server = createServer (async ( req , res ) => { const result = await handler . handle ( req , res , { context : { headers : req . headers } }) if (! result . matched ) { res . statusCode = 404 res . end('No procedure matched') } }) server . listen ( 3000, '127.0.0.1', () => console . log ('Listening on 127.0.0.1:3000') ) ``` -------------------------------- ### Define oRPC App Router with Zod Schema Validation Source: https://orpc.unnoq.com/docs/getting-started Defines an oRPC application router using Zod for input schema validation. It includes procedures for listing, finding, and creating planets, with context-aware authorization for the create procedure. ```typescript import type { IncomingHttpHeaders } from 'node:http' import { ORPCError , os } from '@orpc/server' import * as z from 'zod' const PlanetSchema = z . object ({ id : z . number (). int (). min (1), name : z . string (), description : z . string (). optional () }) export const listPlanet = os . input ( z . object ({ limit : z . number (). int (). min (1). max (100). optional (), cursor : z . number (). int (). min (0). default (0), }), ) . handler (async ({ input }) => { // your list code here return [{ id : 1, name : 'name' }] }) export const findPlanet = os . input ( PlanetSchema . pick ({ id : true })) . handler (async ({ input }) => { // your find code here return { id : 1, name : 'name' } }) export const createPlanet = os . $context <{ headers : IncomingHttpHeaders }>() . use (({ context , next }) => { const user = parseJWT ( context . headers . authorization ?. split (' ')[1]) if ( user ) { return next ({ context : { user } }) } throw new ORPCError ('UNAUTHORIZED') }) . input ( PlanetSchema . omit ({ id : true })) . handler (async ({ input , context }) => { // your create code here return { id : 1, name : 'name' } }) export const router = { planet : { list : listPlanet , find : findPlanet , create : createPlanet } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.