Skip to main content
Environment variables allow you to configure your Astro application without hardcoding values. Use them for API keys, database URLs, feature flags, and other configuration that changes between environments.

Using Environment Variables

Access environment variables through import.meta.env:
src/pages/index.astro
Only variables prefixed with PUBLIC_ are available in client-side code. Server-only variables are accessible only during SSR.

.env Files

Store environment variables in .env files in your project root:
.env
Add .env files to .gitignore to avoid committing secrets to version control.

.env File Priority

Astro loads environment variables from multiple files in this order (highest priority first):
1

.env.production.local

Production environment, local overrides (gitignored)
2

.env.production

Production environment
3

.env.local

All environments, local overrides (gitignored)
4

.env

All environments

Public vs Private Variables

Variables prefixed with PUBLIC_ are available everywhere:
.env
Accessible in any file:
src/components/Analytics.astro
Public variables are embedded in client bundles. Never use PUBLIC_ for secrets!

Type Safety

Type your environment variables for better IntelliSense and type checking:
src/env.d.ts
Now TypeScript will:
  • Autocomplete environment variable names
  • Show type errors for missing variables
  • Catch typos at compile time
Make required variables non-optional in the type definition to catch missing values early.

Astro Environment Schema

For advanced type safety and validation, use Astro’s environment schema:
astro.config.mjs

Field Types

Access Levels

'client' | 'server'
required
Where the variable can be accessed
'public' | 'secret'
required
Whether the variable is public or contains secrets
Secret client variables are not allowed for security reasons. Secrets must be server-only.

Runtime Access

Access validated environment variables at runtime:

Default Values

Provide fallback values for optional variables:
src/components/Config.astro
Or use the schema’s default values:
astro.config.mjs

Common Patterns

.env
src/lib/api.ts

Loading .env in Scripts

Load environment variables in Node.js scripts:
scripts/seed-db.ts

Platform-Specific Variables

Many hosting platforms provide their own environment variables:
Access them like any other environment variable:

Built-in Variables

Astro provides several built-in variables:
'development' | 'production'
Current mode (astro dev vs astro build)
boolean
Whether running in production
boolean
Whether running in development
string
The site URL from your config
string
The base path from your config

Security Best Practices

1

Never commit secrets

Add .env and .env.local to .gitignore:
.gitignore
2

Use PUBLIC_ carefully

Only use PUBLIC_ for values safe to expose:
  • ✅ API endpoints
  • ✅ Public IDs
  • ❌ API keys
  • ❌ Passwords
  • ❌ Secrets
3

Provide example file

Create .env.example with dummy values:
.env.example
4

Validate on startup

Check required variables exist:
src/lib/env.ts

Configuration

Astro configuration options

TypeScript

TypeScript setup and types

Deployment

Deploy your Astro site

SSR

Server-side rendering