/Knowledge/sources/honojs__hono/llms-full/context-var--0ad99bb299e1.md
Context: var - hono-docs
Context: var Source evidence: /Sources/honojs hono/provenance.md Canonical citation: https://hono.dev/llms full.txt var Summary You can also access the value...
Context: var
Source evidence: /Sources/honojs__hono/provenance.md Canonical citation: https://hono.dev/llms-full.txt#var
Summary
You can also access the value of a variable with c.var. ts twoslash import type { Context } from 'hono' declare const c: Context // ---cut--- const result = c.var.client.oneMethod() If you want to create the middleware which pro...
Content
You can also access the value of a variable with c.var.
import type { Context } from 'hono'
declare const c: Context
// ---cut---
const result = c.var.client.oneMethod()
If you want to create the middleware which provides a custom method, write like the following:
import { Hono } from 'hono'
import { createMiddleware } from 'hono/factory'
// ---cut---
type Env = {
Variables: {
echo: (str: string) => string
}
}
const app = new Hono()
const echoMiddleware = createMiddleware<Env>(async (c, next) => {
c.set('echo', (str) => str)
await next()
})
app.get('/echo', echoMiddleware, (c) => {
return c.text(c.var.echo('Hello!'))
})
If you want to use the middleware in multiple handlers, you can use app.use().
Then, you have to pass the Env as Generics to the constructor of Hono to make it type-safe.
Metadata
{
"chunk_index": 0,
"citation": "https://hono.dev/llms-full.txt#var",
"coverage_role": "overview",
"qualified_title": "Hono / llms-full / Context > var (1)",
"retrieved_at": "2026-07-02T05:51:01Z",
"section_index": 562,
"source_id": "/honojs/hono",
"source_type": "llms_full",
"target_label": "llms-full",
"title": "Context: var",
"upstream_url": "https://hono.dev/llms-full.txt",
"version": "2026"
}