Overview
The Adosa Real Estate website requires environment variables to authenticate with the eGO Real Estate API. Understanding the difference between build-time and runtime variables in Astro is critical for proper configuration.Required Environment Variables
PUBLIC_EGO_API_TOKEN
The API authentication token for accessing the eGO Real Estate API.Authentication token for the eGO Real Estate API. Must be prefixed with
PUBLIC_ to be accessible in client-side code.src/services/api/api.ts:3
.env File Structure
Create a.env file in the project root:
Example .env File
Astro Environment Variables
Astro has specific rules for environment variables that differ from other frameworks.Build-Time vs Runtime
All Astro environment variables are resolved at BUILD TIME, not runtime. This means:- Variables are read when you run
npm run build - They are embedded into the compiled JavaScript bundles
- Changing
.envafter build requires a rebuild - No environment variables are loaded at runtime in production
PUBLIC_ Prefix Requirement
Astro requires thePUBLIC_ prefix for client-side environment variables:
| Prefix | Accessible In | Use Case |
|---|---|---|
PUBLIC_ | Client + Server | API tokens, public configuration |
| No prefix | Server only | Database credentials, secrets |
ApiCore is imported by components that run in the browser). Without PUBLIC_, the variable would be undefined.
Configuration in Different Environments
Local Development
- Create
.envfile in project root:
- Start dev server:
.env in development mode.
Production Build
- Set environment variable before building:
- Or create
.env.production:
CI/CD (GitHub Actions, GitLab CI)
Set environment variables in your CI/CD platform’s secrets management: GitHub Actions:Vercel / Netlify
- Go to project settings → Environment Variables
- Add variable:
- Key:
PUBLIC_EGO_API_TOKEN - Value:
your_token_here
- Key:
- Redeploy site (environment variables trigger rebuild)
Security Considerations
Is PUBLIC_ Safe?
ThePUBLIC_ prefix means the token is embedded in client-side JavaScript bundles. Anyone can inspect the source code and find it.
For the eGO API, this is acceptable because:
- The API is designed for public website integration
- Rate limiting is implemented server-side by eGO
- The token is scoped to read-only property data
- Lead submissions go through a PHP proxy that can add server-side validation
Additional Security Measures
-
PHP Proxy for Sensitive Operations
The lead submission system uses
public/api/proxy.phpwhich keeps the actual API token server-side:Consider moving this to an environment variable on the server. -
Rate Limiting
The
ApiCoreclass includes automatic retry and rate limit handling to prevent abuse. -
API Token Rotation
Periodically rotate the API token and rebuild the site:
- Domain Restrictions If the eGO API supports domain restrictions, configure it to only accept requests from your production domain.
TypeScript Support
Add type definitions for environment variables insrc/env.d.ts:
Validation
Validate that required environment variables are present:Troubleshooting
Variable is undefined
Problem:import.meta.env.PUBLIC_EGO_API_TOKEN returns undefined
Solutions:
- Ensure variable name starts with
PUBLIC_ - Restart dev server after adding/changing
.env - Check
.envis in project root (same directory aspackage.json) - Verify no typos in variable name
Changes not taking effect
Problem: Updated.env but site still uses old value
Solution: Environment variables are embedded at build time. Restart dev server or rebuild:
Token exposed in JavaScript bundle
Problem: API token visible in compiled JavaScript Explanation: This is expected behavior withPUBLIC_ variables. They are meant to be publicly accessible. If this is a concern:
- Move sensitive operations to server-side (PHP proxy)
- Use API tokens with limited scope/permissions
- Implement server-side proxies for all API calls
Environment-Specific Configuration
Manage multiple environments with separate files:npm run dev→.env.developmentnpm run build→.env.production
Best Practices
- Always use .env files - Don’t hardcode API tokens in source code
- Add .env to .gitignore - Prevent committing secrets
- Document required variables - Create
.env.examplewith placeholders - Validate on startup - Throw errors if required variables are missing
- Use different tokens per environment - Separate dev/staging/production
- Rotate tokens periodically - Update and rebuild every 3-6 months
- Monitor API usage - Track requests to detect unauthorized access
.env.example Template
Create this file in the repository for documentation:.env and add their actual values.
Related Documentation
- eGO Real Estate API - API integration details
- Leads Management - Lead submission system
- Astro Environment Variables - Official Astro docs