---
title: "Type-Safe RPC Client"
description: "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,..."
last_updated: "2026-07-02T09:15:05.27849+00:00"
canonical_url: "https://www.doc0.app/docs/552ca36e-f67e-41c3-a07a-def9bd9551b0/guide/advanced-features/type-safe-rpc-client"
---

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.

## Getting Started

1. **Import the `hc` function**: Use `hc` to initialize your client.
2. **Define your client**: Pass your `Hono` application type as a generic to `hc`.
3. **Call endpoints**: Use the proxied object to chain your API paths and invoke a method.

```typescript
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()
}
```

## Key Concepts

*   **Type Inference**: The client uses your server’s `Hono` type definition to infer the path structure and expected input/output shapes.
*   **Proxy-based API**: The client uses JavaScript Proxies to map your code calls (e.g., `.api.users`) to the actual URL path (`/api/users`) dynamically.
*   **Request Methods**: Methods are accessed using a `$` prefix on the final path segment (e.g., `$get`, `$post`, `$put`, `$delete`, `$patch`).
*   **ClientRequestOptions**: The second argument in a request call, used to pass headers, custom `fetch` implementations, or other request configurations.

## Common Usage Patterns

### Sending JSON Data
To send a request with a JSON body, include a `json` property in the arguments object:

```typescript
const res = await client.api.users.$post({
  json: { name: 'John Doe' }
})
```

### Handling URL Parameters
If your route defines path parameters (e.g., `/users/:id`), pass a `param` object:

```typescript
const res = await client.api.users[':id'].$get({
  param: { id: '123' }
})
```

### Accessing URL/Path strings
You can retrieve the URL or path string for a specific endpoint by calling the `.$url()` or `.$path()` methods on the client chain:

```typescript
const url = client.api.users.$url()
```

## Tips and Warnings

> [!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.

## Configuration Options

| Option | Type | Description |
| :--- | :--- | :--- |
| `headers` | `Record<string, string>` | Custom HTTP headers to include with the request. |
| `fetch` | `Function` | A custom fetch implementation for testing or environments. |
| `buildSearchParams` | `Function` | Custom logic for serializing query parameters. |

## Related

- [Basic Routing](https://www.doc0.app/docs/552ca36e-f67e-41c3-a07a-def9bd9551b0/guide/getting-started/basic-routing)


## Sitemap

See the full [sitemap](https://www.doc0.app/docs/552ca36e-f67e-41c3-a07a-def9bd9551b0/llms.txt) for all pages in this wiki.
