What is the purpose of environment variables in Astro projects?
Environment variables store configuration values outside the code. They help keep secrets safe and allow different settings for development and production.
Click to reveal answer
beginner
How do you access environment variables in Astro components?
Use the special import from 'astro:env' like <code>import { PUBLIC_API_URL } from 'astro:env';</code> to safely access variables starting with <code>PUBLIC_</code>.
Click to reveal answer
beginner
Why should secret environment variables NOT start with PUBLIC_ in Astro?
Variables starting with PUBLIC_ are exposed to client-side code. Secrets must not be exposed to keep them safe on the server only.
Click to reveal answer
beginner
Where do you define environment variables for an Astro project?
Create a .env file in the project root. Write variables like API_KEY=your_key_here. Use .env.production for production settings.
Click to reveal answer
intermediate
How does Astro handle environment variables during build and runtime?
Astro replaces environment variables at build time. Public variables are embedded in client code, while private ones stay on the server during build.
Click to reveal answer
Which prefix must environment variables have to be accessible in Astro client-side code?
APRIVATE_
BCLIENT_
CASTRO_
DPUBLIC_
✗ Incorrect
Only variables starting with PUBLIC_ are exposed to client-side code in Astro.
Where should you store secret keys in an Astro project?
ADirectly in client-side JavaScript
BIn variables starting with PUBLIC_
CIn .env files without PUBLIC_ prefix
DIn HTML meta tags
✗ Incorrect
Secrets should be stored in .env files without the PUBLIC_ prefix to keep them server-only.
How do you import environment variables in Astro components?
Aimport env from 'env';
Bimport { VAR_NAME } from 'astro:env';
Cconst VAR_NAME = process.env.VAR_NAME;
Dimport { VAR_NAME } from 'process';
✗ Incorrect
Astro uses import { VAR_NAME } from 'astro:env' to access environment variables.
What file name is used for production environment variables in Astro?
A.env.production
B.env.prod
C.env.production.local
D.env.prod.local
✗ Incorrect
Astro uses .env.production for production environment variables.
What happens to environment variables during Astro's build process?
AThey are replaced with their values at build time
BThey remain as placeholders in the code
CThey are encrypted and sent to the client
DThey are ignored
✗ Incorrect
Astro replaces environment variables with their actual values during build time.
Explain how to safely use environment variables in an Astro project for both server and client code.
Think about how to separate public and private data.
You got /4 concepts.
Describe the role of environment variables during Astro's build process and why it matters.
Consider what happens before the site is served.
You got /4 concepts.
Practice
(1/5)
1. In Astro, how do you access environment variables inside your code?
Variables without the PUBLIC_ prefix are only available on the server side and not exposed to client code.
Step 2: Analyze client-side access attempt
Trying to access import.meta.env.SECRET_KEY in client code results in undefined, so console.log(secret) logs "undefined".
Final Answer:
It will log "undefined" because SECRET_KEY is undefined in client code. -> Option D
Quick Check:
Non-PUBLIC_ vars are undefined client-side [OK]
Hint: Secrets need no PUBLIC_ prefix and stay server-only [OK]
Common Mistakes:
Expecting secret to be available in client code
Assuming Astro logs variable names instead of values
Thinking Astro replaces missing vars with null
5. You want to use an API key in your Astro project. The key should be kept secret and never exposed to the browser. How should you handle this environment variable?
hard
A. Prefix the variable with PUBLIC_ and use it directly in client code.
B. Store the variable without prefix and access it in client code via import.meta.env.
C. Store the variable without PUBLIC_ prefix and access it only in server-side code.
D. Prefix the variable with SERVER_ and use it in client code.
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 the PUBLIC_ 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 C
Quick Check:
Secrets stay server-only without PUBLIC_ prefix [OK]
Hint: Keep secrets unprefixed and server-only for safety [OK]
Common Mistakes:
Adding PUBLIC_ prefix and exposing secrets to client
Trying to access secrets in client code
Assuming SERVER_ prefix controls access
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.