oRPC
background

Quick Start

Get started with oRPC

Introduction

oRPC is a powerful combination of RPC and OpenAPI, offering an exceptional developer experience powered by TypeScript. It's designed to be simple and straightforward to use.

The oRPC ecosystem primarily consists of:

Server

The foundation where you implement your business logic, equipped with numerous utilities for enhanced developer experience.

Client

The interface for communicating with the oRPC server, featuring a fully typed client for seamless integration.

Contract

Enables painless contract-first development approach, ensuring consistent API design.

OpenAPI

Effortlessly publish your API under OpenAPI specification, maintaining industry standards.

Installation

This guide covers the essential server and client components. For additional information, see the Contract Guide and OpenAPI Guide.

npm i @orpc/server @orpc/client

Define Your First Router

import { , , type , type  } from '@orpc/server'
import {  } from '@orpc/zod'
import {  } from 'zod'
 
export type  = { ?: { : string } }
 
// global pub, authed completely optional
export const  /** public access */ = .<>()
export const  /** require authed */ = .((, , ) => /** put auth logic here */ .({}))
 
export const  = .({
  : 
    .(
      .({
        : .(),
      }),
    )
    .(async (, , ) => {
      return {
        : `Hello, ${.}!`,
      }
    }),
 
  : {
    : 
      .(
        .({
          : .({}),
        }),
      )
      .(
        .({
          : .(),
          : .(),
          : .(),
        }),
      )
      .(async (, , ) => {
        if (!.) {
          throw new ({
            : 'UNAUTHORIZED',
          })
        }
 
        const  = await .({
          : {
            : ., // from now user not undefined-able
          },
        })
 
        // do something on success
 
        return 
      })
      .((, , ) => {
        return {
          : 'example',
          : 'example',
          : 'example',
        }
      }),
 
    : 
      .(
        .({
          : .(),
          : .(),
          : .().('image/*'),
        }),
      )
      .(async (, , ) => {
        . // file upload out of the box
 
        return {
          : 'example',
          : .,
          : .,
        }
      }),
  },
})
 
export type  = <typeof >
export type  = <typeof >

In oRPC middleware is very useful and fully typed you can find more info here

Start Your Server

This example uses @whatwg-node/server to create a Node server with Fetch API.

import { ,  } from '@orpc/server/fetch'
import {  } from '@orpc/openapi/fetch'
import {  } from 'node:http'
import {  } from '@whatwg-node/server'
import {  } from 'examples/server'
 
const  = (
  ((: Request) => {
    const  = new (.)
 
    if (..('/api')) {
      return ({
        ,
        ,
        : '/api',
        : {},
        : [
          (),
          (),
        ],
      })
    }
 
    return new ('Not found', { : 404 })
  }),
)
 
.(3000, () => {
  // eslint-disable-next-line no-console
  .('Server is available at http://localhost:3000')
})

Start the server and visit http://localhost:3000/api/getting?name=yourname to see the result.

Client Usage

Use the fully typed client in any environment:

import { ,  } from '@orpc/client'
import type {  } from 'examples/server'
 
const  = <typeof  /* or contract router */>({
  : 'http://localhost:3000/api',
  // fetch: optional override for the default fetch function
  // headers: provide additional headers
})
 
//  File upload out of the box
const  = await ..({
  : 'My Amazing Title',
  : 'This is a detailed description of my content',
  : (.('thumb') as HTMLInputElement).[0]!,
})
 
..
  • create
  • find
// typesafe and completion out of box

That's all you need to get started with oRPC!

On this page