Skip to content

Reading variables

Your variables are attached to your environment's Worker when a deployment is published. Code running at the edge reads them from its env, the object Cloudflare Workers pass to every request.

Hono

On the context, as c.env:

src/index.ts
import { Hono } from 'hono';
 
type Bindings = { API_TOKEN: string };
 
const app = new Hono<{ Bindings: Bindings }>();
 
app.get('/', (c) => c.text(c.env.API_TOKEN ? 'configured' : 'missing'));
 
export default app;

Futon

On ctx.runtime.env, when the app is exported with toWinterCgHandler:

src/index.ts
import { Futon, toWinterCgHandler } from '@rasenganjs/futon';
 
type Bindings = { API_TOKEN: string };
 
const app = new Futon<Bindings>();
 
app.get('/', async (ctx) => new Response(ctx.runtime.env.API_TOKEN ? 'configured' : 'missing'));
 
export default { fetch: toWinterCgHandler(app) };

A plain Worker

As the second argument of fetch:

src/index.ts
export default {
  async fetch(request: Request, env: { API_TOKEN: string }) {
    return new Response(env.API_TOKEN ? 'configured' : 'missing');
  },
};

Frontends

A static frontend (Vite, a client-rendered Rasengan.js app, a static site) has no code at the edge: its files are served as they are, so it can't read variables at runtime. Values it needs must be in the bundle, which means set at build time; see Vite's environment variables. Only put values there that anyone may see.

Values are strings

Every variable is a string. Parse numbers and booleans yourself:

const maxItems = Number(env.MAX_ITEMS ?? '20');
const debug = env.DEBUG === 'true';