Skip to content
Souloss
Go back

Configuring Environment Variables in Cloudflare Pages

A step-by-step guide to configuring environment variables in Cloudflare Pages Dashboard for astro-minimax features including AI chat, notifications, and build settings.

This guide explains how to configure environment variables in Cloudflare Pages Dashboard for astro-minimax. Proper environment variable configuration is essential for AI chat, notification systems, and build processes to function correctly.

Overview

Why Environment Variables Matter

Environment variables in astro-minimax serve three primary purposes:

PurposeDescriptionExamples
Build ConfigurationControl build-time behaviorNODE_VERSION
Runtime SecretsSecure credential storageAPI keys, tokens
Feature ConfigurationEnable optional featuresAI providers, notifications

Build-time vs Runtime Variables

Understanding when variables are accessed is crucial:

Build-time Variables are used during the build process. Changes require a new deployment to take effect.

VariableUsed AtEffect of Change
NODE_VERSIONBuildRequires redeploy
AI_BINDING_NAMEBuildRequires redeploy

Runtime Variables are accessed when the application runs. Changes take effect on the next request without redeployment.

VariableUsed AtEffect of Change
AI_API_KEYRuntimeImmediate
NOTIFY_TELEGRAM_BOT_TOKENRuntimeImmediate
NOTIFY_RESEND_API_KEYRuntimeImmediate

Accessing Environment Variables

Navigate to the Cloudflare Pages environment variables settings:

Cloudflare Dashboard → Workers & Pages → [your-project] → Settings → Environment variables
plaintext

Environment Scopes

Cloudflare Pages provides two environment scopes:

ScopeDescriptionUse Case
ProductionVariables for production deploymentsAPI keys, live tokens
PreviewVariables for preview deploymentsTest tokens, dev keys

Tip

Use different API keys for production and preview environments to isolate testing from production data.

Required Variables

These variables are essential for astro-minimax to function properly.

Build Configuration

VariableValueRequiredDescription
NODE_VERSION22YesNode.js version for build. Use Node.js 22 LTS for best compatibility.

AI Configuration

VariableDefaultRequiredDescription
AI_BINDING_NAMEminimaxAIFor Workers AIThe binding name defined in wrangler.toml. Must match the [ai].binding value.

Note

When deploying to Cloudflare Pages, AI_BINDING_NAME should match the binding defined in your wrangler.toml file. See the AI Binding Configuration section below.

Optional Variables

Configure these based on your feature requirements.

Notification Variables

Telegram Notifications

VariableExampleRequiredDescription
NOTIFY_TELEGRAM_BOT_TOKEN123456789:ABC...NoTelegram Bot API token for sending notifications
NOTIFY_TELEGRAM_CHAT_ID123456789NoTarget chat ID for Telegram notifications

See the Notification Guide for setup instructions.

Email Notifications (Resend)

VariableExampleRequiredDescription
NOTIFY_RESEND_API_KEYre_xxxNoResend API key for email notifications
NOTIFY_RESEND_FROMnoreply@yourdomain.comNoSender email address
NOTIFY_RESEND_TOyou@example.comNoRecipient email address

AI Provider Variables

For custom AI providers (not Cloudflare Workers AI):

VariableExampleRequiredDescription
AI_API_KEYsk-xxxFor OpenAIAPI key for AI provider
AI_BASE_URLhttps://api.openai.com/v1For OpenAIBase URL for AI API endpoint
AI_MODELgpt-4o-miniNoModel name to use (default: gpt-4o-mini)

See the AI Guide for detailed configuration.

Site Configuration

VariableExampleRequiredDescription
SITE_URLhttps://your-blog.comNoYour site URL for AI context
SITE_AUTHORYourNameNoAuthor name for AI responses

Step-by-Step: Adding Variables

Follow these steps to add environment variables in Cloudflare Pages Dashboard.

Step 1: Open Your Project

  1. Log in to Cloudflare Dashboard
  2. Click Workers & Pages in the left sidebar
  3. Select your astro-minimax project from the list

Step 2: Navigate to Settings

  1. Click the Settings tab in the project navigation
  2. Scroll down to find Environment variables
  3. Click Environment variables to open the configuration panel

Step 3: Add Variables

For each variable you need to configure:

  1. Click Add variable button
  2. Enter the variable name (e.g., NODE_VERSION)
  3. Enter the value (e.g., 22)
  4. Select the environment scope:
    • Production for live deployments
    • Preview for branch previews
    • Or select both if the value is the same
  5. Click Save

For sensitive values like API keys:

  1. After adding the variable, locate it in the list
  2. Click the Encrypt option if available
  3. This prevents the value from being visible in the dashboard

Warning

Once a variable is encrypted, you cannot view its original value. Store your secrets securely elsewhere.

Step 5: Deploy Changes

Build-time variables require a new deployment:

  1. Go to the Deployments tab
  2. Click Retry deployment on the latest deployment
  3. Or push a new commit to trigger automatic deployment

Runtime variables take effect immediately without redeployment.

AI Binding Configuration

The AI chat feature uses Cloudflare Workers AI, which requires proper binding configuration.

wrangler.toml Configuration

Your project should have a wrangler.toml file in apps/blog/:

name = "astro-minimax"
pages_build_output_dir = "dist"
compatibility_date = "2026-03-12"
compatibility_flags = ["nodejs_compat"]

[ai]
binding = "minimaxAI"

[[kv_namespaces]]
binding = "CACHE_KV"
id = "your-kv-namespace-id"
toml

Matching Environment Variable

The AI_BINDING_NAME environment variable must match the [ai].binding value:

wrangler.tomlEnvironment Variable
binding = "minimaxAI"AI_BINDING_NAME=minimaxAI
binding = "AI"AI_BINDING_NAME=AI

How It Works

  1. Cloudflare Pages reads wrangler.toml during deployment
  2. The [ai].binding creates a Workers AI binding
  3. Your code accesses AI via the binding name
  4. The environment variable tells astro-minimax which binding to use

KV Namespace Configuration

KV Namespace is optional and used for caching AI responses.

When to Configure KV

ScenarioKV Needed
Basic AI chatNo
Response cachingYes
Rate limitingYes
Session persistenceYes

Adding KV Namespace

  1. In Cloudflare Dashboard, go to Workers & PagesKV
  2. Create a new namespace or use an existing one
  3. Copy the namespace ID
  4. Update your wrangler.toml:
[[kv_namespaces]]
binding = "CACHE_KV"
id = "your-kv-namespace-id"
toml
  1. Redeploy your project

Security Best Practices

Secrets Management

PracticeDescription
Use encrypted variablesEncrypt sensitive values in the dashboard
Separate environmentsUse different credentials for production and preview
Limit accessOnly grant team members access who need it
Rotate keysPeriodically rotate API keys and tokens
Never commit secretsDo not add .env files to version control

Variable Scope Recommendations

Variable TypeProductionPreviewEncrypted
NODE_VERSIONYesYesNo
AI_BINDING_NAMEYesYesNo
AI_API_KEYYesYesYes
NOTIFY_TELEGRAM_BOT_TOKENYesOptionalYes
NOTIFY_RESEND_API_KEYYesOptionalYes
SITE_URLYesYesNo
SITE_AUTHORYesYesNo

Access Control

In Cloudflare Dashboard:

Your Project → Settings → Access policies
plaintext

Configure who can:

Troubleshooting

Build Fails: Node Version Issues

Symptom: Build fails with Node.js compatibility errors.

Solution:

  1. Verify NODE_VERSION=22 is set in environment variables
  2. Check that the variable is set for Production environment
  3. Redeploy after adding or changing the variable

AI Chat Not Working

Symptom: AI chat returns errors or no response.

Solutions:

CheckAction
Workers AI bindingVerify wrangler.toml has [ai] section
Binding nameEnsure AI_BINDING_NAME matches wrangler.toml binding
Deployment typeMust deploy to Cloudflare Pages (not static hosting)
AI quotaCheck Cloudflare Dashboard for Workers AI usage limits

Notifications Not Sending

Symptom: No notifications received for comments or AI chat.

Solutions:

CheckAction
Environment variablesVerify all NOTIFY_* variables are set correctly
Variable scopeEnsure variables are set for Production
Chat ID (Telegram)Verify the Chat ID is correct (must be numeric)
Bot permissionsFor groups, bot must be an administrator
API key validityTest the API key separately

Variables Not Taking Effect

Symptom: Changes to environment variables don’t change behavior.

Solutions:

  1. Build-time variables: Trigger a new deployment
  2. Runtime variables: Wait a few seconds for propagation
  3. Cache: Clear Cloudflare cache if applicable
  4. Variable name: Check for typos in variable names

Preview vs Production Differences

Symptom: Features work in preview but not production (or vice versa).

Solution:

  1. Open environment variables settings
  2. Check which scope each variable is assigned to
  3. Ensure critical variables are set for both Production and Preview

Next Steps

After configuring environment variables:

  1. Test AI Chat: Visit your deployed site and test the AI chat feature. See AI Guide for detailed testing.

  2. Test Notifications: Trigger a test comment or AI conversation to verify notifications. See Notification Guide for troubleshooting.

  3. Monitor Deployment: Check deployment logs in Cloudflare Dashboard for any build errors.

  4. Configure Custom Domain: Add your custom domain in Cloudflare Pages settings. See Deployment Guide for instructions.

  5. Set Up Analytics: Configure Umami analytics for visitor tracking.



Previous Post
How to update dependencies of astro-minimax
Next Post
Complete Setup Guide: From Zero to Production

评论区

文明评论,共建和谐社区