Skip to main content

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

string
required
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

  1. Build Time: Server functions are extracted and assigned unique IDs
  2. Client Import: Instead of bundling server code, a client RPC stub is created
  3. Runtime: The RPC stub fetches and executes the server function by ID

Generated Code Example

When you write:
The build plugin transforms client imports to:

Function Signature

The created RPC function accepts any arguments and forwards them to the server:

Properties

The returned function has the following properties:
string
The URL endpoint for the server function, constructed from the base URL and function ID.
ClientFnMeta
Metadata object containing the function ID:
true
Internal marker indicating this is a server function stub.

Network Communication

Client RPC functions use the serverFnFetcher internally, which:
  1. Serializes arguments using Seroval
  2. Sends HTTP request to the server function endpoint
  3. Deserializes the response
  4. 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

Client RPC functions are not type-safe by default since they’re created with just an ID string. For type safety, use the standard createServerFn pattern which preserves types across the client-server boundary.

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