Environment variables help keep secret or changing information outside your code. This makes your app safer and easier to update.
Handling environment variables in Astro
import.meta.env.MY_VARIABLEUse import.meta.env to access environment variables in Astro.
Variables must start with PUBLIC_ to be exposed to client-side code.
const apiKey = import.meta.env.PUBLIC_API_KEY;const secret = import.meta.env.MY_SECRET;console.log(import.meta.env.MODE);This Astro component reads a public environment variable PUBLIC_API_URL and shows it on the page.
Make sure to define PUBLIC_API_URL in your .env file before running.
--- const apiUrl = import.meta.env.PUBLIC_API_URL; --- <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Env Variable Example</title> </head> <body> <h1>API URL:</h1> <p>{apiUrl}</p> </body> </html>
Environment variables without PUBLIC_ prefix are only available on the server side.
Restart your Astro dev server after changing environment variables.
Never commit secret keys to public repositories.
Use import.meta.env to access environment variables in Astro.
Prefix variables with PUBLIC_ to use them in client code.
Keep secrets safe by only using them on the server side.
Practice
Solution
Step 1: Understand Astro environment variable access
Astro uses theimport.meta.envobject to expose environment variables.Step 2: Compare with other options
process.envis common in Node.js but not in Astro client code.window.envandAstro.envare not valid in Astro.Final Answer:
Using import.meta.env -> Option AQuick Check:
Astro environment variables = import.meta.env [OK]
- Using process.env which is Node.js specific
- Trying to access env variables via window object
- Assuming Astro has a global Astro.env object
Solution
Step 1: Identify the client-side environment variable prefix
Astro requires environment variables to be prefixed withPUBLIC_to expose them to client-side code.Step 2: Eliminate incorrect prefixes
SERVER_is not used for client exposure,CLIENT_andASTRO_are not recognized prefixes in Astro.Final Answer:
PUBLIC_ -> Option AQuick Check:
Client env prefix = PUBLIC_ [OK]
- Using SERVER_ prefix which is for server only
- Assuming CLIENT_ or ASTRO_ prefixes work
- Not prefixing variables and expecting client access
const apiKey = import.meta.env.PUBLIC_API_KEY; console.log(apiKey);
If the environment variable
PUBLIC_API_KEY is set to 12345, what will be logged in the browser console?Solution
Step 1: Understand variable access in Astro client code
Since the variable is prefixed withPUBLIC_, it is exposed to client code and accessible viaimport.meta.env.PUBLIC_API_KEY.Step 2: Determine logged value
The value ofPUBLIC_API_KEYis set to12345, soconsole.log(apiKey)outputs the string "12345".Final Answer:
"12345" -> Option BQuick Check:
PUBLIC_API_KEY value = "12345" [OK]
- Thinking the PUBLIC_ prefix doesn't make it client accessible, expecting undefined
- Confusing variable name with value
- Assuming error if variable exists
const secret = import.meta.env.SECRET_KEY; console.log(secret);
Assuming
SECRET_KEY is set in the environment but not prefixed with PUBLIC_.Solution
Step 1: Check environment variable accessibility rules
Variables without thePUBLIC_prefix are only available on the server side and not exposed to client code.Step 2: Analyze client-side access attempt
Trying to accessimport.meta.env.SECRET_KEYin client code results inundefined, soconsole.log(secret)logs "undefined".Final Answer:
It will log "undefined" because SECRET_KEY is undefined in client code. -> Option DQuick Check:
Non-PUBLIC_ vars are undefined client-side [OK]
- Expecting secret to be available in client code
- Assuming Astro logs variable names instead of values
- Thinking Astro replaces missing vars with null
Solution
Step 1: Understand security best practices for secrets
Secrets like API keys should never be exposed to client code to avoid leaks.Step 2: Use environment variable naming and access rules
Variables without thePUBLIC_prefix are only available on the server side in Astro, so store the API key without the prefix and access it only in server-side code.Final Answer:
Store the variable without PUBLIC_ prefix and access it only in server-side code. -> Option CQuick Check:
Secrets stay server-only without PUBLIC_ prefix [OK]
- Adding PUBLIC_ prefix and exposing secrets to client
- Trying to access secrets in client code
- Assuming SERVER_ prefix controls access
Ready to prove it?
Pass the test and this pattern moves unknown
