Skip to main content

Context

The Context 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.
See the HonoRequest API reference for available methods.

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.
string
required
Text content to send
StatusCode
HTTP status code (default: 200)
HeaderRecord
Additional headers to set
Response
Response with Content-Type: text/plain

json

Respond with JSON.
JSONValue | {}
required
Object to serialize as JSON
StatusCode
HTTP status code (default: 200)
HeaderRecord
Additional headers to set
Response
Response with Content-Type: application/json

html

Respond with HTML.
string | Promise<string>
required
HTML content to send
StatusCode
HTTP status code (default: 200)
HeaderRecord
Additional headers to set
Response | Promise<Response>
Response with Content-Type: text/html

body

Respond with a raw body.
Data | null
required
Body data (string, ArrayBuffer, ReadableStream, or Uint8Array)
StatusCode
HTTP status code
HeaderRecord
Headers to set
Response
Response with the provided body

redirect

Redirect to a different URL.
string | URL
required
URL to redirect to
RedirectStatusCode
HTTP redirect status code (default: 302)
Response
Redirect response with Location header

notFound

Return a 404 Not Found response.
Response
404 Not Found response

newResponse

Create a new Response with merged headers.
Data | null
required
Response body
StatusCode
HTTP status code
HeaderRecord
Headers to set
Response
New Response with headers merged from c.header() calls

Header Methods

Set a response header.
string
required
Header name
string
Header value. If undefined, the header is deleted.
SetHeadersOptions
Options object with append boolean

status

Set the response status code.
StatusCode
required
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.
string
required
Variable name
any
required
Value to store

get

Get a context variable.
string
required
Variable name
any
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.
Response | Promise<Response>
Rendered response

setRenderer

Set a custom renderer for the application.
Renderer
required
Function that renders content to a Response

setLayout

Set a layout component.
Layout
required
Layout component function
Layout
The layout function

getLayout

Get the current layout.
Layout | undefined
The current layout function, or undefined

Type Parameters

The Context class accepts generic type parameters for type safety:
Env
default:"any"
Environment type defining bindings and variables
string
default:"any"
Path parameter type for type-safe param access
Input
default:"{}"
Input type for validated data