Getting Started
oRPC (OpenAPI Remote Procedure Call) combines RPC (Remote Procedure Call) with OpenAPI, allowing you to define and call remote (or local) procedures through a type-safe API while adhering to the OpenAPI specification.
oRPC simplifies RPC service definition, making it easy to build scalable applications, from simple scripts to complex microservices.
This guide covers the basics: defining procedures, handling errors, and integrating with popular frameworks.
Prerequisites
- Node.js 18+ (20+ recommended) | Bun | Deno | Cloudflare Workers
- A package manager: npm | pnpm | yarn | bun | deno
- A TypeScript project (strict mode recommended)
Installation
npm install @orpc/server@latest @orpc/client@latestyarn add @orpc/server@latest @orpc/client@latestpnpm add @orpc/server@latest @orpc/client@latestbun add @orpc/server@latest @orpc/client@latestdeno add npm:@orpc/server@latest npm:@orpc/client@latestDefine App Router
We'll use Zod for schema validation (optional, any standard schema is supported).
import type { IncomingHttpHeaders } from 'node:http'
import { , } from '@orpc/server'
import * as from 'zod'
const = .({
: .().().(1),
: .(),
: .().(),
})
export const =
.(
.({
: .().().(1).(100).(),
: .().().(0).(0),
}),
)
.(async ({ }) => {
// your list code here
return [{ : 1, : 'name' }]
})
export const =
.(.({ : true }))
.(async ({ }) => {
// your find code here
return { : 1, : 'name' }
})
export const =
.<{ : IncomingHttpHeaders }>()
.(({ , }) => {
const = (..?.(' ')[1])
if () {
return ({ : { } })
}
throw new ('UNAUTHORIZED')
})
.(.({ : true }))
.(async ({ , }) => {
// your create code here
return { : 1, : 'name' }
})
export const = {
: {
: ,
: ,
:
}
}
Create Server
Using Node.js as the server runtime, but oRPC also supports other runtimes like Bun, Deno, Cloudflare Workers, etc.
import { } from 'node:http'
import { } from '@orpc/server/node'
import { } from '@orpc/server/plugins'
import { } from '@orpc/server'
const = new (, {
: [new ()],
: [
(() => {
.()
}),
],
})
const = (async (, ) => {
const = await .(, , {
: { : . }
})
if (!.) {
. = 404
.('No procedure matched')
}
})
.(
3000,
'127.0.0.1',
() => .('Listening on 127.0.0.1:3000')
)Learn more about RPCHandler.
Create Client
import type { } from '@orpc/server'
import { } from '@orpc/client'
import { } from '@orpc/client/fetch'
const = new ({
: 'http://127.0.0.1:3000',
: { : 'Bearer token' },
})
export const : <typeof > = ()Supports both client-side clients and server-side clients.
Call Procedure
End-to-end type-safety and auto-completion out of the box.
const = await ..({ : 1 })
..
Next Steps
This guide introduced the RPC aspects of oRPC. To explore OpenAPI integration, visit the OpenAPI Guide.
