Skip to Content
docsmiddlewarebuiltinContext Storage

Last Updated: 3/9/2026


Context Storage Middleware

The Context Storage Middleware stores the Hono Context in the AsyncLocalStorage, to make it globally accessible.

::: info Note This middleware uses AsyncLocalStorage. The runtime should support it.

Cloudflare Workers: To enable AsyncLocalStorage, add the nodejs_compat or nodejs_als flag to your wrangler.toml file. :::

Import

import { Hono } from 'hono' import { contextStorage, getContext, tryGetContext, } from 'hono/context-storage'

Usage

The getContext() will return the current Context object if the contextStorage() is applied as a middleware.

type Env = { Variables: { message: string } } const app = new Hono<Env>() app.use(contextStorage()) app.use(async (c, next) => { c.set('message', 'Hello!') await next() }) // You can access the variable outside the handler. const getMessage = () => { return getContext<Env>().var.message } app.get('/', (c) => { return c.text(getMessage()) })

On Cloudflare Workers, you can access the bindings outside the handler.

type Env = { Bindings: { KV: KVNamespace } } const app = new Hono<Env>() app.use(contextStorage()) const setKV = (value: string) => { return getContext<Env>().env.KV.put('key', value) }

tryGetContext

tryGetContext() works like getContext(), but returns undefined instead of throwing an error when the context is not available:

const context = tryGetContext<Env>() if (context) { // Context is available console.log(context.var.message) }