Getting Started
Core Concepts
Built-In Middleware
Advanced Features
Troubleshooting
The Type-Safe RPC Client allows you to consume your API with full TypeScript support. By importing your server's application type, the client automatically provides autocomplete for your endpoints, request methods, and expected data structures.
This removes the need to manually define types for your API responses or remember URL paths, ensuring that if your server-side API changes, your client code will catch errors at compile time.
hc function: Use hc to initialize your client.Hono application type as a generic to hc.import { hc } from 'hono/client'
import type { AppType } from '../server'
const client = hc<AppType>('http://localhost:3000')
// The client provides autocomplete for paths and request methods
const res = await client.api.users.$get({
query: { id: '123' }
})
if (res.ok) {
const data = await res.json()
}Hono type definition to infer the path structure and expected input/output shapes..api.users) to the actual URL path (/api/users) dynamically.$ prefix on the final path segment (e.g., $get, $post, $put, $delete, $patch).fetch implementations, or other request configurations.To send a request with a JSON body, include a json property in the arguments object:
const res = await client.api.users.$post({
json: { name: 'John Doe' }
})If your route defines path parameters (e.g., /users/:id), pass a param object:
const res = await client.api.users[':id'].$get({
param: { id: '123' }
})You can retrieve the URL or path string for a specific endpoint by calling the .$url() or .$path() methods on the client chain:
const url = client.api.users.$url()Tip
Use the .$url() method on your client chain to retrieve the full URL string for debugging or external calls.
Warning
Always ensure your server's application type is correctly exported and imported. If the types do not match the actual implementation, the RPC client will not accurately reflect your API.
Note
The RPC client includes support for WebSockets via the .$ws() method, which establishes a connection following your defined API path.