
- Astro JS Tutorial
- Astro JS - Home
- Astro JS - Overview
- Astro JS vs Next JS
- Astro JS vs React JS
- Astro JS Setup
- Astro JS - Installation
- Astro JS - Project Structure
- Astro JS - Pages
- Astro JS Architecture
- Astro JS - Islands
- Astro JS - Islands Architecture
- Astro JS Routing
- Astro JS - Routing
- Astro JS - Dynamic Routing
- Astro JS - Redirecting Routes
- Astro JS - i18n Routing
- Astro JS Configuration
- Astro JS - Configuration
- Astro JS - Editor Setup
- Astro JS - TypeScript Configuration
- Astro JS - Environment Variables
- Astro JS Build UI
- Astro JS - Components
- Astro JS - Slots
- Astro JS - Layouts
- Astro JS - Fonts
- Astro JS - Scripts
- Astro JS Create Website
- Astro JS - Markdown Contents
- Astro JS - Add Images
- Astro JS - Manage Content
- Astro JS - Content Collections
- Astro JS - Data Fetching
- Astro JS Styling and CSS
- Astro JS - Styling
- Astro JS - CSS Integration
- Astro JS - CSS Cascading Order
- Astro JS Integrations
- Astro JS - React Integrations
- Astro JS - Svelte Integrations
- Astro JS - Solid Integrations
- Astro JS - Vue Integrations
- Astro JS Adapters
- Astro JS - Netlify Adapter
- Astro JS - Cloudflare Adapter
- Astro JS Testing and Deployment
- Astro JS - Testing
- Astro JS - Deployment
- Astro JS Advanced Topics
- Astro JS - State Management
- Astro JS - Prefetching
- Astro JS - Middleware
- Astro JS - Endpoints
- Astro JS - Authentication
- Astro JS - Bun Environment
- Astro JS - Docker
- Astro JS - View Transition
- Astro JS - Transition Directives
- Astro JS - Astro DB
- Astro JS - Bundling
- Astro JS Useful Resources
- Astro JS - Interview Questions
- Astro JS - Cheatsheet
Astro JS - Environment Variables
Environment Variables
The environment variables are key value pairs that store configuration data of application. It is generally used to store sensitive information like API keys, database connection strings, and other secrets. Astro have built-in support for loading environment variables from the .env file to process.env object.
Environment Variables in Astro
Astro uses Viteâs built-in support for environment variables, which are statically replaced at build time, and lets you use any of its methods to work with the. Note that while all environment variables are available in server-side code, only environment variables prefixed with PUBLIC_ are available in client-side code for security purposes.
Example
Consider the following environment variable declaration.
// File: ./.env PASSWORD=password123 PUBLIC_ANALYTICS_ID=123
In this example, PUBLIC_ANALYTICS_ID (accessible via import.meta.env.PUBLIC _ANALYTICS_ID) will be available in server or client code, while PASSWORD (accessible via import.meta.env.PASSWORD) will be server-side only.
Setting Environment Variables
The environment variables are kept in a file named '.env' in the root directory of the project. The '.env' file is a plain text file that contains key-value pairs of environment variables. Each line in the file is a key-value pair, separated by an equal sign.
# This will only be available when run on the server! DB_PASSWORD="foobar" # This will be available everywhere! PUBLIC_POKEAPI="https://pokeapi.co/api/v2"
You can also add .production, .development or a custom mode name to the filename itself (e.g .env.testing, .env.staging). This allows you to use different sets of environment variables at different times.
Using Environment Variables
Environment variables in Astro are accessed with import.meta.env, using the import.meta feature added in ES2020, instead of process.env. For example, use import.meta.env.PUBLIC_POKEAPI to get the PUBLIC_POKEAPI environment variable.
// When import.meta.env.SSR === true const data = await db(import.meta.env.DB_PASSWORD); // When import.meta.env.SSR === false const data = fetch(`${import.meta.env.PUBLIC_POKEAPI}/pokemon/squirtle`);
When using SSR, environment variables can be accessed at runtime based on the SSR adapter being used. With most adapters you can access environment variables with process.env, but some adapters work differently. For the Deno adapter, you will use Deno.env.get(). See how to access the Cloudflare runtime to handle environment variables when using the Cloudflare adapter. Astro will first check the server environment for variables, and if they donât exist, Astro will look for them in .env files.
Types of Environment Variables
There are three types of environment variable, based on combination of context ("client" or "server") and access ("secret" or "public") settings defined in your schema:
- Public client variables: These are available to the client and are not secret. They are prefixed with PUBLIC_ in your .env file.
- Secret server variables: These are available to the server and are secret. They are not prefixed with PUBLIC_ in your .env file.
- Public server variables: These are available to the server and are not secret. They are prefixed with PUBLIC_ in your .env file.