Server Functions

Server Functions are TanStack Start's server-side RPCs. They run on the server but can be called from loaders, components, hooks, event handlers, and other server functions.

Basic Form

import { createServerFn } from '@tanstack/react-start'

export const getTodos = createServerFn({ method: 'GET' }).handler(async () => {
  return db.todo.findMany()
})

Input Validation

const createTodo = createServerFn({ method: 'POST' })
  .validator((input: { title: string }) => {
    if (!input.title.trim()) throw new Error('Title required')
    return { title: input.title.trim() }
  })
  .handler(async ({ data }) => db.todo.create({ data }))

Use schema validation in production and never trust client input.

GET vs POST

MethodUse
GETReads, no side effects, cacheable
POSTCreate, update, delete, side effects

Server functions are same-origin RPCs. Use Server Routes for public APIs and webhooks.

Next Steps