#dotenv
6 messages · Page 1 of 1 (latest)
Astro doesn't have a specific startup file like some other frameworks, but it does use environment variables which are typically stored in a .env file at the root of your project. These variables are statically replaced at build time using Vite’s built-in support for environment variables.
You can create a .env file at the root of your Astro project and add your variables there. For example:
PUBLIC_COSMIC_BUCKET_SLUG=YOUR_BUCKET_SLUG
PUBLIC_COSMIC_READ_KEY=YOUR_READ_KEY
Astro will then use these values during the build process. If you want to use runtime variables, they must be absent from the .env file during the build, and you must provide Astro with every environment variable to expect at run-time.
For example:
VARIABLE_1=placeholder astro build
This signals to Astro that the actual value will be available when the built application is run. The placeholder value will be ignored by the build process, and Astro will use the value provided at run-time.
In the case of multiple run-time variables, store them in a separate file (e.g. .env.runtime) from .env. Start the build with the following command:
export $(cat .env.runtime) && astro build
Remember, 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.
You can access these environment variables in your Astro files using import.meta.env, for example import.meta.env.PUBLIC_POKEAPI.
Sources:
- Configuring API Keys (https://docs.astro.build/en/guides/cms/cosmic/#configuring-api-keys)
- Runtime environment variables (https://docs.astro.build/en/guides/integrations-guide/node/#runtime-environment-variables)
- Using environment variables (https://docs.astro.build/en/guides/environment-variables/#_top)
- Getting environment variables (https://docs.astro.build/en/guides/environment-variables/#getting-environment-variables)
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@bold cave kapa.ai is still learning and improving, please let us know how it did by reacting below
Let me check the knowledge base for you. One moment! :books:
Astro doesn't directly support the dotenv package, but it does use Vite's built-in support for environment variables. These variables are statically replaced at build time. You can create a .env file at the root of your Astro project and add your variables there.
For example:
SECRET_PASSWORD=password123
PUBLIC_ANYBODY=there
In this example, PUBLIC_ANYBODY (accessible via import.meta.env.PUBLIC_ANYBODY) will be available in server or client code, while SECRET_PASSWORD (accessible via import.meta.env.SECRET_PASSWORD) will be server-side only.
Astro will then use these values during the build process. If you want to use runtime variables, they must be absent from the .env file during the build, and you must provide Astro with every environment variable to expect at run-time.
For example:
VARIABLE_1=placeholder astro build
This signals to Astro that the actual value will be available when the built application is run. The placeholder value will be ignored by the build process, and Astro will use the value provided at run-time.
In the case of multiple run-time variables, store them in a separate file (e.g. .env.runtime) from .env. Start the build with the following command:
export $(cat .env.runtime) && astro build
Remember, 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.
You can access these environment variables in your Astro files using import.meta.env, for example import.meta.env.PUBLIC_POKEAPI.