Cloudflare Workers use a specific environment variable system.
Local development:
Use a .dev.vars file for local development (e.g., with wrangler dev):
DATABASE_URL=your-database-url
Accessing variables in the Frontend:
In client and server components, you can use standard syntax:
const DATABASE_URL = process.env.DATABASE_URL
Accessing variables in the Backend (API):
In the backend, you must use the env adapter from hono/adapter to access variables from the context c:
import { env } from "hono/adapter"
import { j } from "jstack"
export const postRouter = j.router({
recent: j.procedure.get(({ c }) => {
const { DATABASE_URL } = env(c)
}),
})
// Backend (API)
import { env } from "hono/adapter"
import { j } from "jstack"
export const postRouter = j.router({
recent: j.procedure.get(({ c }) => {
const { DATABASE_URL } = env(c)
}),
})