Skip to main content

Overview

The hc() function creates a type-safe HTTP client that automatically infers types from your Hono server routes. It uses Proxy objects to provide a chainable API that mirrors your server’s route structure.

Import

Function Signature

string
required
The base URL of your API server (e.g., 'http://localhost:3000' or 'https://api.example.com')
ClientRequestOptions
Configuration options for the client
Client<T, Prefix>
A proxy-based client that mirrors your server’s route structure with type-safe methods

Usage

Basic Usage

With Headers

Custom Fetch Implementation

Request Methods

The client provides methods for all HTTP verbs, prefixed with $:
  • $get(args?, options?) - GET request
  • $post(args?, options?) - POST request
  • $put(args?, options?) - PUT request
  • $patch(args?, options?) - PATCH request
  • $delete(args?, options?) - DELETE request
  • $options(args?, options?) - OPTIONS request
  • $head(args?, options?) - HEAD request

Request Arguments

Each method accepts an optional args object with the following properties:
Record<string, string | string[]>
Query parameters for the request
Record<string, string>
Path parameters (e.g., for routes like /posts/:id)
object
JSON body data (automatically sets Content-Type: application/json)
Record<string, string | Blob | File | (string | Blob | File)[]>
Form data (automatically creates FormData and handles arrays)
Record<string, string>
Additional headers for this specific request
Cookies to send with the request

Example Requests

Utility Methods

$url()

Generates a fully-qualified URL object for the route.
Type Signature:

$path()

Generates the path portion of the URL (without the base URL).
Type Signature:

$ws()

Creates a WebSocket connection for routes that support WebSocket upgrades.
Type Signature:

Response Handling

All HTTP methods return a Promise<ClientResponse> which extends the standard Response interface with type-safe methods:

Per-Request Options

You can override client-level options for individual requests:

Type Safety

The client automatically infers request and response types from your server definition:
See api/client/types for helper types like InferRequestType and InferResponseType.

Notes

  • The client automatically handles URL parameter replacement for path parameters like :id
  • Index routes (ending in /index) are automatically normalized
  • GET and HEAD requests never include a body, even if you accidentally provide one
  • FormData is automatically created when using the form option
  • Array values in forms are automatically expanded (multiple form fields with the same key)
  • The client is built on Proxy objects, so it works with any route structure without requiring code generation