oRPC is currently pre-stable, please report any issues on our Discord or GitHub 🚧
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

Note: oRPC support both with and without contract-first development. This guide for without contract-first development.

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  = .<>()
export const  = .(({ , ,  }, ) => {
  /** put auth logic here */
  return ({})
})
 
export const  = .({
  : 
    .(
      .({
        : .(),
      }),
    )
    .(async ({ ,  }) => {
      return {
        : 'example',
        : 'example',
      }
    }),
 
  : {
    : 
      .(
        .({
          : .(),
        }),
      )
      .(
        .({
          : .(),
          : .(),
          : .(),
        }),
      )
      .(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

import {  } from '@orpc/server/node'
import {  } from '@orpc/openapi/node'
import {  } from 'node:http'
import {  } from 'examples/server'
import {  } from '@orpc/zod'
 
const  = new (, {
  : [
    new (),
  ],
})
 
const  = new ()
 
const  = (async (, ) => {
  if (.?.('/api')) {
    const {  } = await .(, , {
      : '/api',
      : {},
    })
 
    if(){
      return 
    }
  }
 
  if(.?.('/rpc')){
    const {  } = await .(, , {
      : '/rpc',
      : {},
    })
 
    if(){
      return 
    }
  }
 
  . = 404
  .('Not found')
})
 
.(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 {  } from '@orpc/client/fetch'
import type {  } from 'examples/server'
 
const  = new ({
  : 'http://localhost:3000/rpc',
  // fetch: optional override for the default fetch function
  // headers: provide additional headers
})
 
const  = <typeof  /* or contract router */>()
 
//  File upload out of the box
const  = await ..({
  : 'My Amazing Title',
  : 'This is a detailed description of my content',
  : (.('thumb') as HTMLInputElement).[0]!,
})
 
..
  • createPost
  • getPost
// typesafe and completion out of box

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

On this page