oRPC
background

Server Actions

Use oRPC to improve your server actions.

Server Actions

Server actions allow you to use oRPC procedures directly as form handlers in frameworks that support server actions (like Next.js). They provide automatic type coercion and FormData handling out of the box.

Context Requirements

Server actions are automatically enabled for procedures that have either:

  • No context requirements (undefined context)
  • Optional context (Context | undefined)
"use server"
 
import {  } from '@orpc/server'
import {  } from 'zod'
 
// This creates a server action since context is undefined
const  = 
  .(.({ : .() }))
  .(() => 'result')
 
// This also creates a server action since context is optional
const  = 
  .<{ : boolean } | undefined>()
  .(() => 'result')
 
// This won't be a server action since it requires context
const  = 
  .<{ : boolean }>()
  .(() => 'result')

Usage Examples

Define Server Action

Create type-safe server actions with full input validation:

"use server"
 
import {  } from '@orpc/server'
import {  } from '@orpc/zod'
import {  } from 'zod'
import {  } from "next/navigation"
 
export const  = 
  .(
    .({
      : .({
        : .(),
        : .(),
        : .(.().('image/*')).(),
      }),
    }),
  )
  .(() => {
    ('/posts/new')
  })

Form Integration

Use with React forms and get automatic type coercion:

import {  } from 'examples/server-action'
import * as React from 'react'
 
export default function () {
  return (
    < ={}>
      {/* Nested objects use square bracket notation */}
      < ="text" ="nested[title]"  />
      < ="text" ="nested[description]"  />
      <
        ="files"
        ="file"
        ="nested[thumbs][]"
        
        ="image/*"
      />
    </>
  )
}

Direct Client Calls

Call server actions directly from client components:

"use client"
 
import {  } from 'examples/server-action'
import * as React from 'react'
 
export default function () {
  async function () {
    const  = (.('files') as HTMLInputElement).
 
    // Call with plain object - types are preserved
    await ({
      : {
        : 'hello',
        : 'world',
        :  ? [...] : null,
      },
    })
  }
 
  return < ={}>Create User</>
}

On this page