The Adapter Helper provides utilities for detecting the runtime environment and accessing environment variables.
Import
import { env, getRuntimeKey } from 'hono/adapter'
Functions
env()
Get environment variables and bindings in a type-safe way.
function env<T extends Record<string, any> = Record<string, any>, C extends Context = Context>(
c: C
): T & C['env']['Bindings']
Environment variables and bindings
Example
import { env } from 'hono/adapter'
app.get('/', (c) => {
const { DATABASE_URL, API_KEY } = env(c)
return c.text(`Using database: ${DATABASE_URL}`)
})
getRuntimeKey()
Detect the current JavaScript runtime.
function getRuntimeKey():
| 'node'
| 'deno'
| 'bun'
| 'workerd'
| 'fastly'
| 'edge-light'
| 'other'
Example
import { getRuntimeKey } from 'hono/adapter'
const runtime = getRuntimeKey()
console.log(`Running on ${runtime}`)
if (runtime === 'deno') {
// Deno-specific code
} else if (runtime === 'bun') {
// Bun-specific code
}
Type-Safe Environment
import { env } from 'hono/adapter'
type Env = {
Bindings: {
DATABASE_URL: string
API_KEY: string
}
}
app.get('/', (c) => {
const { DATABASE_URL, API_KEY } = env<Env['Bindings']>(c)
// TypeScript knows these are strings
})