Read environment variables in API routes with getEnv and requireEnv.
In API routes, environment variables are read with getEnv(c, key) and requireEnv(c, key). Both read from the Hono request context, so the same code works on every runtime: Node.js, Bun, Deno, Vercel Edge, Netlify Edge, and Cloudflare Workers.
getEnv and requireEnv accept the Hono context type natively. Every read is request-scoped, so there is no process.env fallback to worry about.Use requireEnv for variables your app cannot run without. Use getEnv with ?? for optional configuration.
| Function | Use for | Behavior |
|---|---|---|
| requireEnv(c, key) | Required config - app cannot run without | Throws if missing or empty |
| getEnv(c, key) ?? default | Optional config - fallback to default | Returns undefined if missing |
A full API endpoint that uses environment variables for configuration:
Always handle errors from requireEnv gracefully:
On failure, the terminal shows:
.env files are only loaded during development. In production, set variables in your hosting platform's dashboard.getEnv and requireEnv work on Node.js, Bun, Deno, Vercel Edge, Netlify Edge, and Cloudflare Workers.BINI_ prefix for client-side public config. No prefix for server-only secrets.Overview of getEnv and requireEnv.
What gets bundled and what stays server-side.
Files under app/api/ map to /api/*.
Routing, middleware, and validation with Hono.
Read environment variables in API routes with getEnv and requireEnv.
In API routes, environment variables are read with getEnv(c, key) and requireEnv(c, key). Both read from the Hono request context, so the same code works on every runtime: Node.js, Bun, Deno, Vercel Edge, Netlify Edge, and Cloudflare Workers.
getEnv and requireEnv accept the Hono context type natively. Every read is request-scoped, so there is no process.env fallback to worry about.Use requireEnv for variables your app cannot run without. Use getEnv with ?? for optional configuration.
| Function | Use for | Behavior |
|---|---|---|
| requireEnv(c, key) | Required config - app cannot run without | Throws if missing or empty |
| getEnv(c, key) ?? default | Optional config - fallback to default | Returns undefined if missing |
A full API endpoint that uses environment variables for configuration:
Always handle errors from requireEnv gracefully:
On failure, the terminal shows:
.env files are only loaded during development. In production, set variables in your hosting platform's dashboard.getEnv and requireEnv work on Node.js, Bun, Deno, Vercel Edge, Netlify Edge, and Cloudflare Workers.BINI_ prefix for client-side public config. No prefix for server-only secrets.Overview of getEnv and requireEnv.
What gets bundled and what stays server-side.
Files under app/api/ map to /api/*.
Routing, middleware, and validation with Hono.