Skip to main content
The Adapter helper provides utilities for detecting the current JavaScript runtime and accessing environment variables in a runtime-agnostic way. This enables writing code that works across different platforms like Node.js, Deno, Bun, Cloudflare Workers, and more.

Import

Runtime Detection

getRuntimeKey

Detect the current JavaScript runtime:
Returns: Runtime - A string identifying the current runtime

Supported Runtimes

  • 'node' - Node.js
  • 'deno' - Deno
  • 'bun' - Bun
  • 'workerd' - Cloudflare Workers
  • 'fastly' - Fastly Compute
  • 'edge-light' - Vercel Edge Runtime and similar
  • 'other' - Unknown or unsupported runtime

Environment Variables

env

Access environment variables in a runtime-agnostic way:
Parameters:
Context
required
The Hono context object
Runtime
Optional runtime override. If not specified, automatically detected using getRuntimeKey()
Returns: T & Context['env'] - Object containing environment variables

Type-safe Environment Variables

With Generic Type Parameter

Define environment variable types inline:

With Hono Generics

Define environment variables at the application level:

Combined Approach

Combine both generic types:

Runtime-specific Behavior

The env function adapts to different runtimes automatically:

Node.js and Bun

Accesses process.env:

Deno

Accesses Deno.env.toObject():

Cloudflare Workers

Accesses environment bindings from context:

Fastly Compute

Returns empty object (use ConfigStore instead):

Edge Runtime

Accesses process.env:

Use Cases

Runtime-specific Configuration

Adjust behavior based on runtime:

Database Connection

Connect to database using environment variables:

API Keys and Secrets

Access API keys securely:

Feature Flags

Implement feature flags with environment variables:

Manual Runtime Override

Override automatic runtime detection when needed:

Known User Agents

The helper uses user agents for runtime detection:

Detection Priority

Runtime detection follows this priority:
  1. Check navigator.userAgent against known user agents
  2. Check for EdgeRuntime global (Edge Runtime)
  3. Check for fastly global (Fastly Compute)
  4. Check process.release.name === 'node' (Node.js)
  5. Return 'other' if none match

Type Definitions

Best Practices

Type Your Environment

Always provide TypeScript types for environment variables to catch errors early

Use Hono Bindings

Define environment types in Hono’s generic Bindings for better type inference

Validate Variables

Validate that required environment variables exist before using them

Avoid Runtime Checks

Rely on automatic detection rather than manual runtime checking when possible
For Cloudflare Workers, environment variables are accessed via bindings (c.env) rather than traditional environment variables. The env helper handles this automatically.
On Fastly Compute, standard environment variables are not available. Use the Fastly ConfigStore API for configuration data instead.

Error Handling

Handle missing environment variables gracefully: