createClientRpc
Creates a client-side RPC function that calls a server-side function by ID. This is useful for code splitting and lazy loading server functions.Client RPC functions are typically generated automatically by the build plugin. You usually don’t need to call
createClientRpc manually.Basic Usage
API Reference
The unique identifier of the server function to call. This ID is generated during build time and matches the corresponding server function.
How It Works
- Build Time: Server functions are extracted and assigned unique IDs
- Client Import: Instead of bundling server code, a client RPC stub is created
- Runtime: The RPC stub fetches and executes the server function by ID
Generated Code Example
When you write:Function Signature
The created RPC function accepts any arguments and forwards them to the server:Properties
The returned function has the following properties:The URL endpoint for the server function, constructed from the base URL and function ID.
Metadata object containing the function ID:
Internal marker indicating this is a server function stub.
Network Communication
Client RPC functions use theserverFnFetcher internally, which:
- Serializes arguments using Seroval
- Sends HTTP request to the server function endpoint
- Deserializes the response
- Handles errors, redirects, and streaming responses
Serialization
Arguments and return values are automatically serialized using Seroval, supporting:- Primitives (string, number, boolean, null, undefined)
- Objects and arrays
- Date objects
- RegExp
- Maps and Sets
- Circular references
- Promises (streaming)
- Custom serializable types
Error Handling
Custom Fetch
You can configure a custom fetch implementation globally:Advanced Example
Type Safety
Use Cases
Code Splitting
RPC functions enable automatic code splitting of server-only dependencies:Lazy Loading
Dynamically import server functions when needed:Third-Party Integration
Call server functions from libraries that don’t know about TanStack Start:Comparison with createServerFn
| Feature | createServerFn | createClientRpc |
|---|---|---|
| Type Safety | Full type inference | No types (just ID) |
| Build-time | Transforms to RPC | Manual RPC creation |
| Use Case | Primary API | Advanced/manual use |
| Middleware | Supported | Inherited from server |
| Validation | Supported | Inherited from server |
Related
- Server Functions - Primary way to create server functions
- Server RPC - Server-side RPC creation
- Middleware - Add middleware to server functions