Context Variables in Hono
From the Hono cheat sheet · Context & Request · verified Jul 2026
Context Variables
Store and retrieve values in request context
typescript
// Set context variable in middleware
app.use('*', async (c, next) => {
c.set('userId', '12345')
c.set('startTime', Date.now())
await next()
})
// Get context variable in handler
app.get('/profile', (c) => {
const userId = c.get('userId')
return c.json({ userId })
})
// Type-safe context with generics
type Variables = {
userId: string
user: { name: string; email: string }
}
const app = new Hono<{ Variables: Variables }>()💡 c.set(key, value) stores, c.get(key) retrieves
⚡ Use generics for type-safe context variables
📌 Bindings are for environment variables (Cloudflare)
🎯 Variables persist throughout the request lifecycle