📖 Scenario: You are building a simple Astro website that needs to use a secret API key safely. Instead of hardcoding the key in your code, you will use environment variables to keep it secure and flexible.
🎯 Goal: Learn how to set up and use environment variables in an Astro project to access a secret API key inside a component.
📋 What You'll Learn
Create an environment variable file with a secret API key
Configure Astro to load environment variables
Access the environment variable inside an Astro component
Display the API key value safely in the component
💡 Why This Matters
🌍 Real World
Environment variables are used in real projects to keep secrets like API keys safe and to configure apps without changing code.
💼 Career
Knowing how to handle environment variables is essential for web developers to build secure and flexible applications.
Progress0 / 4 steps
1
Create the environment variable file
Create a file named .env in the root of your Astro project. Inside it, add the line PUBLIC_API_KEY=12345abcde exactly as shown.
Astro
Hint
The .env file should be in the root folder of your Astro project. Use the exact variable name PUBLIC_API_KEY and value 12345abcde.
2
Configure Astro to load environment variables
In your astro.config.mjs file, import dotenv/config at the top to enable loading environment variables from the .env file.
Astro
Hint
Adding import 'dotenv/config' at the top of astro.config.mjs loads your environment variables automatically.
3
Access the environment variable inside an Astro component
Create an Astro component file named ApiKeyDisplay.astro. Inside the frontmatter, create a constant apiKey that reads Astro.env.PUBLIC_API_KEY.
Astro
Hint
Use the frontmatter section --- to declare const apiKey = Astro.env.PUBLIC_API_KEY. Then display it inside a paragraph.
4
Complete the component to display the API key
Ensure your ApiKeyDisplay.astro component returns a paragraph element that shows the text Your API key is: followed by the apiKey variable inside curly braces.
Astro
Hint
The paragraph should exactly read <p>Your API key is: {apiKey}</p> to display the key.
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.