Overview
Astro Portfolio v3 uses environment variables to securely manage API keys and configuration. The project leverages Astro’s built-in environment variable system with type safety and validation defined inastro.config.mjs:29-47.
Environment Schema
The project defines a strict environment schema for type safety:Required Variables
Your Beehiiv publication ID for newsletter integration.Context: Server-side only
Access: Secret (not exposed to client)
Used in: Newsletter subscription API endpoint (
Access: Secret (not exposed to client)
Used in: Newsletter subscription API endpoint (
/api/newsletter)Get your publication ID from your Beehiiv dashboard. Navigate to Settings > API to find your publication details.
Your Beehiiv API key for authenticating newsletter subscription requests.Context: Server-side only
Access: Secret (not exposed to client)
Used in: Newsletter API endpoint for creating subscriptionsHow to get your API key:
Access: Secret (not exposed to client)
Used in: Newsletter API endpoint for creating subscriptionsHow to get your API key:
- Log in to Beehiiv
- Go to Settings > API
- Click Create API Key
- Copy the generated key
Your Google Tag Manager container ID for analytics and tracking.Context: Client-side
Access: Public (exposed to browser)
Format:
Used in: Analytics tracking across the siteHow to get your GTM ID:
Access: Public (exposed to browser)
Format:
GTM-XXXXXXXUsed in: Analytics tracking across the site
This variable is public and will be visible in the browser. Only the container ID is exposed, not sensitive tracking data.
- Log in to Google Tag Manager
- Select or create a container
- Find your container ID (format:
GTM-XXXXXXX) in the top bar
Local Development
Setup
Cloudflare Pages Configuration
Setting Environment Variables
Access Project Settings
- Log in to the Cloudflare Dashboard
- Navigate to Pages > Your project
- Click Settings > Environment Variables
Add Production Variables
For the Production environment, add:
| Variable Name | Value | Type |
|---|---|---|
BEEHIIV_PUBLICATION_ID | Your publication ID | Secret |
BEEHIIV_API_KEY | Your API key | Secret |
GTM_ID | Your GTM container ID | Plain text |
Mark
BEEHIIV_PUBLICATION_ID and BEEHIIV_API_KEY as Secret to encrypt them in Cloudflare’s system.Add Preview Variables (Optional)
For preview deployments, you can:
- Use the same production values
- Use separate test/staging credentials
- Leave GTM_ID empty to disable analytics in previews
Environment-Specific Configuration
You can set different values for different environments:- Production: Used for the
mainbranch - Preview: Used for pull request previews
- Per-branch: Override variables for specific branches
Preview environments are useful for testing with sandbox APIs or disabling analytics during development.
Security Best Practices
Do’s ✅
- Use Secret Variables: Mark sensitive values as “Secret” in Cloudflare Pages
- Rotate Keys Regularly: Change API keys periodically
- Use Server Context: Keep sensitive variables server-side only (
context: 'server') - Validate Input: Always validate data before using with API keys
- Monitor Usage: Track API usage in Beehiiv dashboard for suspicious activity
Don’ts ❌
- Never Commit Secrets: Don’t commit
.envfiles or hardcode API keys - Don’t Expose Server Variables: Never pass server-side secrets to client components
- Avoid Logging Secrets: Don’t log API keys in error messages or console
- Don’t Share Keys: Each developer should use their own test credentials
- Don’t Use Production Keys Locally: Use separate development/test keys for local work
Accessing Variables in Code
Server-Side (API Routes)
Client-Side (Components)
Astro’s environment system provides type safety and prevents accidental exposure of server-side secrets to the client.
Validation and Defaults
The environment schema includes default values for graceful degradation:- BEEHIIV_PUBLICATION_ID: Defaults to
'abc'(placeholder) - BEEHIIV_API_KEY: Defaults to empty string (disables newsletter)
- GTM_ID: Defaults to empty string (disables analytics)
With default values, the site will build successfully even without environment variables, but features like newsletter subscription won’t work until properly configured.
Troubleshooting
Newsletter Not Working
Verify Variables
Check that
BEEHIIV_PUBLICATION_ID and BEEHIIV_API_KEY are set correctly in Cloudflare Pages.Test API Key
Use the Beehiiv API documentation to test your credentials with a curl request.
Analytics Not Tracking
- Verify
GTM_IDformat is correct (GTM-XXXXXXX) - Check browser console for GTM initialization errors
- Ensure the variable is set in the Production environment
Build Failures
- Missing required variables may cause build errors
- Check the build logs for specific environment variable errors
- Ensure all variables are spelled correctly (case-sensitive)
Next Steps
Cloudflare Pages
Deploy your portfolio with environment variables
Custom Domain
Configure a custom domain for your site