Making Requests
The Hono RPC client provides a fluent API for making HTTP requests with full type safety.GET Requests
Simple GET requests with no parameters:GET with Query Parameters
Pass query parameters using thequery property:
Array query parameters are automatically serialized as multiple parameters with the same key (e.g.,
tag=framework&tag=typescript).POST Requests with JSON
Send JSON data using thejson property:
POST with Form Data
Send form data using theform property:
Multiple Form Values
Form fields can have multiple values:Path Parameters
Access routes with path parameters using bracket notation:Multiple Path Parameters
Headers and Cookies
Request Headers
Add custom headers to individual requests:Global Headers
Set headers for all requests when creating the client:Dynamic Headers
Use a function to compute headers dynamically:Async Headers
Headers can be computed asynchronously:Cookies
Send cookies with your requests:Handling Responses
Response Object
The client returns a standardResponse object with typed methods:
JSON Responses
Text Responses
Blob and Binary Data
Error Handling
Status Code Checking
Handle different status codes with type narrowing:Using res.ok
Check if the response is successful (status 200-299):
When using
res.ok, TypeScript narrows the type to only success status codes.Parsing Response Errors
Use theparseResponse utility for automatic error handling:
Middleware Error Responses
Handle errors returned from middleware:Advanced Features
Custom Fetch Implementation
Provide your own fetch implementation:Using with app.request
Test your API without making network calls:
This is perfect for testing - no need to start a server!
Custom RequestInit
Pass customRequestInit options:
Custom Query Serialization
Customize how query parameters are serialized:WebSocket Support
Connect to WebSocket endpoints:URL Utilities
Get Full URL
Generate the full URL for a route:Get Path Only
Generate just the path (without domain):These utilities are useful for generating links in your UI or for debugging.
Best Practices
Handle errors consistently
Handle errors consistently
Create a wrapper for consistent error handling:
Use environment variables for URLs
Use environment variables for URLs
Don’t hardcode API URLs:
Leverage TypeScript's type narrowing
Leverage TypeScript's type narrowing
Use status code checks to narrow response types:
What’s Next?
Validators
Add runtime validation to your API
Middleware
Learn about Hono’s middleware system