Skip to content

Environment Configuration & AppEnv ​

BeechCMS is built to run natively on Cloudflare Workers, leveraging serverless primitives like Cloudflare D1, Cloudflare R2, and Workers AI.

Configuration in BeechCMS is governed by the AppEnv contract, which cleanly separates runtime Cloudflare infrastructure bindings (Env) from per-request dependency injection services (Variables).


The AppEnv Contract ​

Every Hono router and API handler in BeechCMS is typed with AppEnv:

typescript
import type { Context } from 'hono'
import type { AppEnv } from '../types'

export async function customHandler(c: Context<AppEnv>) {
  // Access Cloudflare Bindings & Env vars:
  const db = c.env.DB
  const jwtSecret = c.env.JWT_SECRET

  // Access Middleware-injected services:
  const logger = c.get('activityLogger')
  const scheduler = c.get('scheduler')
  const privacy = c.get('privacyService')
}

AppEnv combines two distinct layers:

  1. c.env (Bindings & Secrets): Infrastructure components provided directly by Cloudflare Workers runtime (D1 databases, R2 buckets, environment secrets).
  2. c.get(...) (Request Variables): Domain repositories, cryptography services, schedulers, and authenticated user contexts initialized per-request by middleware.

Cloudflare Infrastructure Bindings ​

Declared in wrangler.toml and bound at runtime:

BindingTypeDescription
DBD1DatabasePrimary SQLite database for content schemas, seeds, and metadata.
MEDIA_BUCKETR2BucketObject storage bucket for image and file uploads.
SEARCH_R2R2BucketStorage bucket for compiled vector binaries (vectors.bin) and search manifests.
AIWorkersAICloudflare Workers AI binding for embedding generation.
LOGIN_RATE_LIMITERRateLimitCloudflare edge rate limiter for the admin login route.
REFRESH_RATE_LIMITERRateLimitEdge rate limiter for token refresh rotation.
PUBLIC_READ_RATE_LIMITERRateLimitRate limiting for public read queries.
PUBLIC_WRITE_RATE_LIMITERRateLimitRate limiting for public form submissions.

Environment Variables & Secrets ​

Core Authentication & Security ​

VariableRequiredDescription
JWT_SECRETYes256-bit secret key used to sign and verify HMAC-SHA256 JWT tokens.
PRIVACY_MASTER_KEYRequired for protected fields256-bit key (64 hex chars) used for field encryption and blind indexes. Without it, reads and writes on seeds with protected fields fail. See Field Policies.
JWT_ISSUEROptionalValue validated against the iss claim (default: beechcms).
JWT_AUDIENCEOptionalValue validated against the aud claim (default: beechcms-api).
CORS_ORIGINSOptionalComma-separated list of allowed origins (e.g. https://my-site.com,https://admin.my-site.com).

Public API & Forms ​

VariableRequiredDescription
BEECH_PUBLISHABLE_KEYRecommendedRead-only key for /api/v1/public/*. Lives in wrangler.jsonc vars, local value beech_pk_dev, safe in browsers.
BEECH_SECRET_KEYRequired for public writesReads and writes. A secret (.dev.vars / wrangler secret put), local value beech_sk_dev, refused outside ENV=development.
PUBLIC_TIME_TRAP_SECRETRequired for anonymous formsHMAC key (use 32+ random bytes) that signs the anti-bot Time-Trap tokens. When unset, GET /api/v1/public/timetrap/token and anonymous POST /api/v1/public/:seed/add answer 503 time-trap-not-configured instead of signing with a guessable key. Requests authenticated with the secret key are not affected. The value lives in .dev.vars locally and in the secret store in production.
PUBLIC_PUBLISHED_ONLYOptionalWhen set to true, restricts public reads strictly to published items.

Automations & Delivery ​

VariableRequiredDescription
RESEND_API_KEYOptionalAPI key for transactional emails via Resend.
EMAIL_FROMOptionalDefault sender address (e.g. BeechCMS <noreply@yourdomain.com>).
WEBHOOK_SECRETOptionalSecret key used to sign outgoing automation webhooks with HMAC-SHA256 (X-BeechCMS-Signature).
QSTASH_URLOptionalUpstash QStash REST publish URL for serverless queues.
QSTASH_TOKENOptionalBearer token for Upstash QStash.
QSTASH_CURRENT_SIGNING_KEYOptionalPrimary signing key for inbound QStash webhook verification.
QSTASH_NEXT_SIGNING_KEYOptionalSecondary key for seamless key rotation in QStash.

Local Development Configuration ​

During local development, Wrangler loads environment secrets from a .dev.vars file in the project root:

ini
# .dev.vars (Never commit to version control)
JWT_SECRET=super-secret-local-jwt-token-key-change-me
PRIVACY_MASTER_KEY=4a2f8c9b... # 64 hex characters, see Field Policies
RESEND_API_KEY=re_dev_123456789
EMAIL_FROM="BeechCMS Dev <noreply@localhost>"
BEECH_SECRET_KEY=beech_sk_dev
PUBLIC_TIME_TRAP_SECRET=local-time-trap-secret-change-me
WEBHOOK_SECRET=whsec_dev_local_secret
CORS_ORIGINS=http://localhost:5173,http://localhost:3000

The publishable key is not a secret and lives in wrangler.jsonc vars ("BEECH_PUBLISHABLE_KEY": "beech_pk_dev").

To run local development with D1 and R2 emulated:

bash
pnpm run dev

Production Secrets Management ​

In production on Cloudflare Workers, sensitive keys must never be stored in plain text or in wrangler.toml. Use the Wrangler CLI to securely upload secrets directly to Cloudflare's encrypted key vault:

bash
# Set JWT signing secret
npx wrangler secret put JWT_SECRET --env production

# Set Resend email key
npx wrangler secret put RESEND_API_KEY --env production

# Set Webhook HMAC secret
npx wrangler secret put WEBHOOK_SECRET --env production

# Set QStash tokens
npx wrangler secret put QSTASH_TOKEN --env production
npx wrangler secret put QSTASH_CURRENT_SIGNING_KEY --env production

# Set the public API secret key
npx wrangler secret put BEECH_SECRET_KEY --env production

# Set the Time-Trap signing secret
npx wrangler secret put PUBLIC_TIME_TRAP_SECRET --env production

# Set the field-policy master key (once, never replaced)
npx wrangler secret put PRIVACY_MASTER_KEY --env production

Projects created with npx @beechcms/cms@latest have no env block, so they drop --env production. The secret key value is beech_sk_live_ + 32 hex characters (the same generator beech keys:migrate uses). PRIVACY_MASTER_KEY is set once and never replaced: see master key provisioning.

Wrangler prompts you interactively to enter each secret, ensuring they remain encrypted at rest.


Injected Request Services (Variables) ​

The BeechCMS middleware pipeline instantiates and registers singleton services on each request via Hono's c.set():

typescript
// Available on any c.get(...) in handlers:
const scheduler = c.get('scheduler')              // IScheduler (waitUntil async task executor)
const activityLogger = c.get('activityLogger')    // IActivityLogger (audit logging)
const notificationService = c.get('notificationService') // INotificationService
const privacyService = c.get('privacyService')    // IPrivacyService (ALE encryption/blind indexing)
const jwtPayload = c.get('jwtPayload')            // Authenticated user claims (sub, email, name, surname)

This dependency-injection architecture ensures handlers remain strictly testable with mock providers and fixed clocks.