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.
Context
TheContext object is passed to every route handler and middleware function. It provides access to the request, response helpers, environment bindings, and context variables.
Constructor
You typically don’t create Context instances directly. Hono creates them for each request.Request Properties
req
The HonoRequest instance for accessing request data.env
Environment bindings (e.g., Cloudflare Workers KV, D1, etc.).event
The FetchEvent associated with the current request (Cloudflare Workers).Throws an error if the context does not have a FetchEvent.
executionCtx
The ExecutionContext for the current request.Throws an error if the context does not have an ExecutionContext.
error
Error object if a handler threw an error (available in error handlers).finalized
Boolean indicating whether the response has been finalized.res
The Response object for the current request.Response Methods
text
Respond with plain text.Text content to send
HTTP status code (default: 200)
Additional headers to set
Response with Content-Type: text/plain
json
Respond with JSON.Object to serialize as JSON
HTTP status code (default: 200)
Additional headers to set
Response with Content-Type: application/json
html
Respond with HTML.HTML content to send
HTTP status code (default: 200)
Additional headers to set
Response with Content-Type: text/html
body
Respond with a raw body.Body data (string, ArrayBuffer, ReadableStream, or Uint8Array)
HTTP status code
Headers to set
Response with the provided body
redirect
Redirect to a different URL.URL to redirect to
HTTP redirect status code (default: 302)
Redirect response with Location header
notFound
Return a 404 Not Found response.404 Not Found response
newResponse
Create a new Response with merged headers.Response body
HTTP status code
Headers to set
New Response with headers merged from c.header() calls
Header Methods
header
Set a response header.Header name
Header value. If undefined, the header is deleted.
Options object with
append booleanstatus
Set the response status code.HTTP status code to set
It’s usually cleaner to pass the status directly to response methods like
c.json(data, 201) instead of calling c.status() separately.Context Variables
Context variables allow you to pass data between middleware and handlers.set
Set a context variable.Variable name
Value to store
get
Get a context variable.Variable name
The stored value, or undefined if not set
var
Read-only object containing all context variables.c.var provides type-safe access to variables when using TypeScript with proper type definitions.Rendering Methods
render
Render content using the configured renderer.Rendered response
setRenderer
Set a custom renderer for the application.Function that renders content to a Response
setLayout
Set a layout component.Layout component function
The layout function
getLayout
Get the current layout.The current layout function, or undefined
Type Parameters
The Context class accepts generic type parameters for type safety:Environment type defining bindings and variables
Path parameter type for type-safe param access
Input type for validated data