Next.js · Middle · Question 39 of 75
Where should you store a secret API key used in a server component?
- ✓Correct answer: In an environment variable without the
NEXT_PUBLIC_prefix - 2In
NEXT_PUBLIC_API_KEYso it's available everywhere - 3Right in the component's code - it's a server component anyway
Explanation
Variables without the prefix are available only on the server. NEXT_PUBLIC_* variables are inlined into the client bundle and visible to everyone. Hardcoding ends up in the repository. .env*.local files aren't committed, and in production the values are set on the platform.