Documentation Index Fetch the complete documentation index at: https://mintlify.com/honojs/hono/llms.txt
Use this file to discover all available pages before exploring further.
Hono
The Hono class is the main entry point for creating Hono web applications. It extends HonoBase and provides routing, middleware, and request handling capabilities.
Constructor
Creates a new Hono application instance.
const app = new Hono < E , S , BasePath >( options ? )
Configuration options for the Hono instance Specifies whether to distinguish whether the last path is a directory or not. When true, /about and /about/ are treated as different routes.
Custom router implementation. By default, Hono uses SmartRouter with RegExpRouter and TrieRouter.
Custom function to extract the path from a request. Useful for handling host header routing.
Type Parameters
Environment type that defines bindings and variables available in your app
S
Schema
default: "BlankSchema"
Schema type that defines your API routes for type-safe client generation
Base path prefix for all routes in this app instance
Example
import { Hono } from 'hono'
import { RegExpRouter } from 'hono/router/reg-exp-router'
// Basic usage
const app = new Hono ()
// With custom router
const app2 = new Hono ({ router: new RegExpRouter () })
// With strict mode disabled
const app3 = new Hono ({ strict: false })
// With type definitions
type Env = {
Bindings : {
DB : D1Database
}
Variables : {
user : User
}
}
const app4 = new Hono < Env >()
Route Methods
Define HTTP route handlers for specific methods.
get
Register a GET route handler.
app . get ( path , ... handlers )
app . get ( ... handlers )
The route path. If omitted, handlers are applied to the current path.
One or more handler functions or middleware
Returns the Hono instance for method chaining
app . get ( '/' , ( c ) => c . text ( 'Hello, World!' ))
app . get ( '/users/:id' , ( c ) => {
const id = c . req . param ( 'id' )
return c . json ({ id })
})
post
Register a POST route handler.
app . post ( path , ... handlers )
app . post ( '/users' , async ( c ) => {
const body = await c . req . json ()
return c . json ({ created: true , data: body })
})
put
Register a PUT route handler.
app . put ( path , ... handlers )
delete
Register a DELETE route handler.
app . delete ( path , ... handlers )
patch
Register a PATCH route handler.
app . patch ( path , ... handlers )
options
Register an OPTIONS route handler.
app . options ( path , ... handlers )
all
Register a route handler that matches all HTTP methods.
app . all ( path , ... handlers )
app . all ( '/status' , ( c ) => c . text ( 'OK' ))
Middleware Methods
use
Register middleware that runs for matching routes.
app . use ( path ? , ... middleware )
Path pattern to match. Defaults to '*' (all routes).
middleware
MiddlewareHandler[]
required
One or more middleware functions
Returns the Hono instance for method chaining
// Apply to all routes
app . use ( logger ())
// Apply to specific path
app . use ( '/api/*' , async ( c , next ) => {
c . set ( 'requestTime' , Date . now ())
await next ()
})
// Multiple middleware
app . use ( '/admin/*' , auth (), checkPermissions ())
Register a route handler for custom HTTP methods or multiple methods.
app . on ( method , path , ... handlers )
method
string | string[]
required
HTTP method(s) to match
path
string | string[]
required
Path(s) to match
Returns the Hono instance for method chaining
// Single method and path
app . on ( 'PURGE' , '/cache' , ( c ) => c . text ( 'Cache purged' ))
// Multiple methods
app . on ([ 'PUT' , 'PATCH' ], '/data' , handler )
// Multiple paths
app . on ( 'GET' , [ '/v1/users' , '/v2/users' ], handler )
Application Methods
route
Mount another Hono instance as a sub-application.
Base path for the sub-application
The Hono instance to mount
Returns the Hono instance with merged routes
const api = new Hono ()
api . get ( '/users' , ( c ) => c . json ({ users: [] }))
api . get ( '/posts' , ( c ) => c . json ({ posts: [] }))
const app = new Hono ()
app . route ( '/api' , api )
// Routes: GET /api/users, GET /api/posts
basePath
Create a new instance with a base path prefix.
Returns a new Hono instance with the base path applied
const api = new Hono (). basePath ( '/api/v1' )
api . get ( '/users' , handler ) // Matches /api/v1/users
mount
Mount applications built with other frameworks.
app . mount ( path , applicationHandler , options ? )
Base path for the mounted application
applicationHandler
(request: Request, ...args: any) => Response | Promise<Response>
required
Request handler from another framework
Configuration for how the request should be handled
Returns the Hono instance for method chaining
import { Router as IttyRouter } from 'itty-router'
const ittyRouter = IttyRouter ()
ittyRouter . get ( '/hello' , () => new Response ( 'Hello from itty-router' ))
const app = new Hono ()
app . mount ( '/itty-router' , ittyRouter . handle )
Error Handling
onError
Register a custom error handler.
Function that receives an error and context, returns a Response
Returns the Hono instance for method chaining
app . onError (( err , c ) => {
console . error ( ` ${ err } ` )
return c . text ( 'Custom Error Message' , 500 )
})
notFound
Register a custom 404 handler.
handler
NotFoundHandler<E>
required
Function that receives a context and returns a Response
Returns the Hono instance for method chaining
app . notFound (( c ) => {
return c . text ( 'Custom 404 Message' , 404 )
})
Request Handling
fetch
The main entry point for handling requests.
app . fetch ( request , env ? , executionCtx ? )
The incoming Request object
Environment bindings (e.g., Cloudflare Workers bindings)
Execution context for async operations
return
Response | Promise<Response>
The response to send back to the client
// Cloudflare Workers
export default app
// Or manually
export default {
fetch: app . fetch
}
request
Convenience method for testing that creates a Request and calls fetch.
app . request ( input , requestInit ? , env ? , executionCtx ? )
input
Request | string | URL
required
Request object, URL string, or pathname
Request initialization options
return
Response | Promise<Response>
The response from the application
// For testing
test ( 'GET /hello is ok' , async () => {
const res = await app . request ( '/hello' )
expect ( res . status ). toBe ( 200 )
})
// With full URL
const res = await app . request ( 'http://localhost:8787/api/users' )
// With request options
const res = await app . request ( '/api/users' , {
method: 'POST' ,
body: JSON . stringify ({ name: 'John' })
})
Properties
routes
Array of all registered routes.
app . routes : RouterRoute []
console . log ( app . routes )
// [{ path: '/', method: 'GET', handler: [Function] }, ...]