#Environment Variables in Astro
3 messages · Page 1 of 1 (latest)
Hi @mortal wing. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Hey there! I'd be happy to help you with defining environment variables in Astro. Here's how you can do it:
In Astro, you can define environment variables using .env files in your project directory. Here's a quick rundown:
- Create a
.envfile in the root of your project. - Add your variables to the file. For example:
DB_PASSWORD="foobar"
PUBLIC_POKEAPI="<https://pokeapi.co/api/v2">
Note that variables prefixed with PUBLIC_ will be available in both server-side and client-side code, while others (like DB_PASSWORD) will only be available server-side for security reasons.
You can also create environment-specific files like .env.development or .env.production for different settings in various environments.
To access these variables in your Astro code, you can use import.meta.env, like this:
const apiUrl = import.meta.env.PUBLIC_POKEAPI;
Remember, for security, it's crucial to add your .env file to .gitignore to prevent sensitive information from being committed to your repository.