@mercurjs/client provides a type-safe way to interact with your Mercur backend. Every endpoint, request input, and response is fully typed — inferred directly from your backend routes.
Installation
Create a client
The
Routes type is generated from your backend using @mercurjs/cli@rc codegen. This gives the client full type safety with zero manual type
definitions.Calling APIs
The client exposes three methods:$-prefixed keys for path parameters:
Inferring types
Extract input and output types from any client method usingInferClientInput and InferClientOutput:
React Query
The client returns plain Promises, so it works with any data-fetching library. Here’s a typical pattern with TanStack React Query:Error handling
Failed requests throw aClientError:
FAQ
Why is a route missing from the client after I added it?
Why is a route missing from the client after I added it?
The client’s types come from the generated route map — run
bunx @mercurjs/cli@rc codegen after adding or changing route files. During development, codegen --watch keeps the map current automatically.How are URLs derived from property access?
How are URLs derived from property access?
Each property becomes a kebab-cased path segment (
client.vendor.salesSummary → /vendor/sales-summary), $-prefixed keys fill path parameters, and the terminal method picks the HTTP verb: query → GET, mutate → POST, delete → DELETE.Can I upload files through the client?
Can I upload files through the client?
Yes — pass
Blob/File values in the payload and the client switches to multipart form data automatically.Next steps
Add a custom API route
Build a route and call it through the client, end to end.
CLI
The codegen command that generates the client’s route map.