Leptos Server APIs
5. Leptos SSR, REST, and gRPC Integration
Leptos server functions (#[server]) bridge the browser UI with backend
commands. The counter app also exposes explicit JSON REST endpoints and, on
Spin, a gRPC service through the same WASI HTTP trigger. All three command
surfaces call the same application service so persistence, projection catch-up,
and Redis realtime publishing stay consistent.
5.1 Server Function Definitions (src/app.rs)
Here is how the server functions are constructed in
examples/counter-app/src/app.rs. Notice how they isolate SSR execution from
client-side WASM hydration compilation:
5.2 Unified Server-Side Command Execution (src/application.rs)
Behind the scenes on the server, the application layer initializes the event
store, executes the command within aggregate consistency boundaries through the
repository, publishes realtime notifications, and advances the projection
runner. The Leptos server functions, REST routes, and gRPC service all call this
same function:
{"error":{"code":"...","message":"..."}}, gRPC maps the same error to a
tonic::Code, and server functions convert it to ServerFnError only after
logging through tracing. See Error Handling and Transport Mapping for the complete production guide.
5.3 Curlable REST and Spin gRPC APIs
The UI server functions are framework-owned endpoints. For integration checks, the app exposes these explicit JSON REST routes:proto/counter.proto and is served through the Spin HTTP
trigger. Run with transport=both to keep the browser UI, REST APIs, SSE, and
gRPC active together:
transport=grpc serves only gRPC endpoints. transport=both serves HTTP UI,
REST, SSE, and gRPC. Wasmtime currently fails fast for transport=grpc and
transport=both.
5.4 WASI HTTP Routing (src/server.rs)
The WASI HTTP router handles transport-specific routes before handing normal UI
and server-function traffic to leptos_wasi::Handler. Keep this order:
- Spin gRPC route detection.
transport=grpcHTTP guard.- Explicit JSON REST counter routes.
/api/counter/streamSSE realtime route.- Leptos static-file, UI, and server-function handler.