Skip to main content
@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@latest codegen. This gives the client full type safety with zero manual type definitions.
You can pass default fetch options for authentication or other headers:

Calling APIs

The client exposes three methods:
Use $-prefixed keys for path parameters:
Override fetch options per request when needed:

Inferring types

Extract input and output types from any client method using InferClientInput 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 a ClientError:

FAQ

The client’s types come from the generated route map — run bunx @mercurjs/cli@latest codegen after adding or changing route files. During development, codegen --watch keeps the map current automatically.
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.
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.