0
0
Astroframework~20 mins

Handling environment variables in Astro - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Astro Env Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does Astro handle environment variables in components?

Consider an Astro component that uses an environment variable API_KEY. Which option correctly accesses this variable in the component?

Astro
const apiKey = import.meta.env.API_KEY;

export default function Component() {
  return <p>{apiKey}</p>;
}
ADeclare <code>const API_KEY = process.env.API_KEY</code> at the top level and use it in JSX.
BUse <code>process.env.API_KEY</code> directly in the component JSX.
CAccess the variable using <code>import.meta.env.API_KEY</code> inside the component script.
DUse <code>Astro.env.API_KEY</code> inside the component script.
Attempts:
2 left
💡 Hint

Astro uses import.meta.env to access environment variables at build time.

📝 Syntax
intermediate
2:00remaining
Which syntax correctly loads a private environment variable in Astro?

Astro supports public and private environment variables. Which syntax correctly loads a private variable SECRET_KEY that should not be exposed to client code?

AUse <code>import.meta.env.SECRET_KEY</code> only in server-side code or Astro server routes.
BUse <code>process.env.SECRET_KEY</code> in client components.
CUse <code>import.meta.env.PUBLIC_SECRET_KEY</code> in client components.
DUse <code>Astro.env.SECRET_KEY</code> in client components.
Attempts:
2 left
💡 Hint

Private environment variables are only accessible on the server side in Astro.

🔧 Debug
advanced
2:00remaining
Why does this Astro component fail to access the environment variable?

Given this Astro component code:

---
const apiKey = process.env.API_KEY;
---

{apiKey}

Why does it fail to display the API key?

Astro
const apiKey = process.env.API_KEY;

export default function Component() {
  return <p>{apiKey}</p>;
}
ABecause <code>process.env</code> is not available in Astro components; should use <code>import.meta.env</code> instead.
BBecause environment variables must be imported from a special Astro module.
CBecause the component is missing an async keyword to access environment variables.
DBecause the environment variable <code>API_KEY</code> is not defined in the .env file.
Attempts:
2 left
💡 Hint

Astro uses a different syntax than Node.js for environment variables.

state_output
advanced
2:00remaining
What is the output of this Astro server route accessing env variables?

Consider this Astro server route code:

export async function get() {
  return new Response(JSON.stringify({ key: import.meta.env.SECRET_KEY }), {
    headers: { 'Content-Type': 'application/json' }
  });
}

If SECRET_KEY is set to supersecret in the environment, what is the JSON response?

Astro
export async function get() {
  return new Response(JSON.stringify({ key: import.meta.env.SECRET_KEY }), {
    headers: { 'Content-Type': 'application/json' }
  });
}
A{"key":null}
B{"key":"supersecret"}
C{"key":"undefined"}
DThrows a ReferenceError
Attempts:
2 left
💡 Hint

Server routes can access private environment variables directly.

🧠 Conceptual
expert
2:00remaining
Why should public environment variables in Astro start with PUBLIC_ prefix?

Astro requires public environment variables to start with PUBLIC_. Why is this naming convention important?

AIt enables Astro to load these variables only during development, not in production.
BIt is a legacy requirement from older frameworks and has no security impact.
CIt allows Astro to automatically encrypt these variables before sending to the client.
DIt ensures only variables with <code>PUBLIC_</code> prefix are exposed to client-side code, protecting private data.
Attempts:
2 left
💡 Hint

Think about what data should be safe from client exposure.