Skip to content

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

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

Define App Router

We'll use Zod for schema validation (optional, any standard schema is supported).

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

ts
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

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

ts
const  = await ..({ : 1 })

..

Next Steps

This guide introduced the RPC aspects of oRPC. To explore OpenAPI integration, visit the OpenAPI Guide.

Released under the MIT License.