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
UsecreateServerFn to define a server function:
app/routes/posts.tsx
HTTP Methods
Server functions supportGET and POST methods:
GET Requests
Best for reading data without side effects:POST Requests
Best for mutations and operations with side effects:Input Validation
Validate input data with theinputValidator 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:getRequest()- Get the current request objectgetRequestHeaders()- Get request headersgetCookie(name)- Get a cookie valuesetCookie(name, value, options)- Set a cookiegetSession(config)- Get session datasetResponseHeader(name, value)- Set response headerssetResponseStatus(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:- Client serializes input with
serovalfor cross-platform serialization - HTTP request sent to
/_server/{functionId} - Server receives request in
handleServerAction(fromserver-functions-handler.ts) - Input validated with
inputValidator - Middleware chain executes
- Handler function runs
- 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
Keep server functions focused
Keep server functions focused
Each server function should do one thing well:
Use appropriate HTTP methods
Use appropriate HTTP methods
- Use
GETfor reading data - Use
POSTfor mutations
Validate all inputs
Validate all inputs
Always validate input data to prevent security issues:
Handle errors appropriately
Handle errors appropriately
Don’t expose sensitive information in errors:
Use middleware for cross-cutting concerns
Use middleware for cross-cutting concerns
Keep handlers clean by extracting common logic:
Related Concepts
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