Skip to main content
Server functions allow you to write server-side code that can be called directly from your client components with full type safety. They provide a seamless bridge between client and server, handling serialization, HTTP requests, and error handling automatically.

What Are Server Functions?

Server functions are functions that execute only on the server but can be called from anywhere in your application. They’re ideal for:
  • Database queries
  • API calls to third-party services
  • File system operations
  • Authentication logic
  • Any operation that requires server-side secrets or resources

Creating a Server Function

Use createServerFn to define a server function:
app/routes/posts.tsx

HTTP Methods

Server functions support GET and POST methods:

GET Requests

Best for reading data without side effects:
GET requests serialize data in the URL query string. There’s a 1MB payload size limit for GET requests.

POST Requests

Best for mutations and operations with side effects:
POST requests send data in the request body as JSON.

Input Validation

Validate input data with the inputValidator method:

Using Zod

Custom Validation

Middleware

Add middleware for cross-cutting concerns like authentication, logging, and context:

Authentication Middleware

Logging Middleware

Global Middleware

Apply middleware to all server functions:
app/start.ts

Server Context

Access request context within server functions:
Available context utilities:
  • getRequest() - Get the current request object
  • getRequestHeaders() - Get request headers
  • getCookie(name) - Get a cookie value
  • setCookie(name, value, options) - Set a cookie
  • getSession(config) - Get session data
  • setResponseHeader(name, value) - Set response headers
  • setResponseStatus(code, text) - Set response status

How Server Functions Work

Under the hood, TanStack Start transforms server functions into RPC endpoints:

1. Compilation

During build, the bundler extracts server function bodies into separate endpoints:

2. Request Handling

When called from the client:
  1. Client serializes input with seroval for cross-platform serialization
  2. HTTP request sent to /_server/{functionId}
  3. Server receives request in handleServerAction (from server-functions-handler.ts)
  4. Input validated with inputValidator
  5. Middleware chain executes
  6. Handler function runs
  7. Result serialized and returned

3. Serialization

TanStack Start uses seroval for serialization, supporting:
  • Primitives (string, number, boolean, null, undefined)
  • Objects and arrays
  • Dates
  • RegExp
  • Map and Set
  • Typed arrays
  • Custom serialization adapters

FormData Support

Server functions work with HTML forms:

Error Handling

Handle errors gracefully:

Streaming Responses

Server functions support streaming for real-time data:

Type Safety

Server functions are fully type-safe:

Best Practices

Each server function should do one thing well:
  • Use GET for reading data
  • Use POST for mutations
Always validate input data to prevent security issues:
Don’t expose sensitive information in errors:
Keep handlers clean by extracting common logic:

Server Rendering

Learn how SSR works with server functions

Middleware

Complete guide to middleware patterns

Streaming

Stream data progressively to clients

Server Functions API

Complete API reference