Skip to content

Using env variables

The env() function helps to retrieve environment variables across all supported runtimes.

ts
import { env, type Get, type UniversalMiddleware } from "@universal-middleware/core";

const handler = (() => (request, context, runtime) => {
  // process.env.DATABASE_URL on supported environment
  // Done.env.get("DATABASE_URL") for Deno
  // runtime.env.DATABASE_URL for Cloudflare
  const { DATABASE_URL } = env<{ DATABASE_URL: string }>(runtime);
  
  // ...
}) satisfies Get<[], UniversalMiddleware>;

INFO

env() supports the following:

RuntimeSupports
Cloudflarewrangler.toml
DenoDeno.env, process.env
BunBun.env, process.env, import.meta.env
Node.jsprocess.env