summaryrefslogtreecommitdiff
path: root/docs/src/pages/en/guides/environment-variables.md
diff options
context:
space:
mode:
authorGravatar Caleb Jasik <calebjasik@jasik.xyz> 2022-01-03 13:59:34 -0600
committerGravatar GitHub <noreply@github.com> 2022-01-03 11:59:34 -0800
commitf26eb7b74558a06ed96e7c94fd9844eaf2508fb4 (patch)
tree750ea3ba771df1669aa50782b7882ed6bee0132b /docs/src/pages/en/guides/environment-variables.md
parentf9b813aa86a199c8c00a4e1fe306ff6db24f1b31 (diff)
downloadastro-f26eb7b74558a06ed96e7c94fd9844eaf2508fb4.tar.gz
astro-f26eb7b74558a06ed96e7c94fd9844eaf2508fb4.tar.zst
astro-f26eb7b74558a06ed96e7c94fd9844eaf2508fb4.zip
Docs/move-english-docs-to-"en"-folder (#2268)
* Move english pages under `/en` and fix broken links hopefully * Add meta refresh tags for `/` to `/en/` url moves + make `/index.astro` work without js * update languageselect for new en format Co-authored-by: Fred K. Schott <fkschott@gmail.com>
Diffstat (limited to 'docs/src/pages/en/guides/environment-variables.md')
-rw-r--r--docs/src/pages/en/guides/environment-variables.md31
1 files changed, 31 insertions, 0 deletions
diff --git a/docs/src/pages/en/guides/environment-variables.md b/docs/src/pages/en/guides/environment-variables.md
new file mode 100644
index 000000000..30ab36398
--- /dev/null
+++ b/docs/src/pages/en/guides/environment-variables.md
@@ -0,0 +1,31 @@
+---
+layout: ~/layouts/MainLayout.astro
+title: Using environment variables
+description: Learn how to use environment variables in an Astro project.
+---
+
+Astro uses Vite for environment variables, and allows you to use any of its methods to get and set environment variables. Note that all environment variables must be prefixed with `PUBLIC_` to be accessible by client side code.
+
+The ability to access private variables on the server side is [still being discussed](https://github.com/withastro/astro/issues/1765).
+
+## Setting environment variables
+
+Vite includes `dotenv` by default, allowing you to easily set environment variables without any extra configuration in Astro projects. You can also attach a mode (either `production` or `development`) to the filename, like `.env.production` or `.env.development`, which makes the environment variables only take effect in that mode.
+
+Just create a `.env` file in the project directory and add some variables to it.
+
+```bash
+# .env
+PUBLIC_POKEAPI="https://pokeapi.co/api/v2"
+```
+
+## Getting environment variables
+
+Instead of using `process.env`, with Vite you use `import.meta.env`, which uses the `import.meta` feature added in ES2020 (don't worry about browser support though, Vite replaces all `import.meta.env` mentions with static values). For example, to get the `PUBLIC_POKEAPI` environment variable, you could use `import.meta.env.PUBLIC_POKEAPI`.
+
+```js
+fetch(`${import.meta.env.PUBLIC_POKEAPI}/pokemon/squirtle`);
+```
+
+> ⚠️WARNING⚠️:
+> Because Vite statically replaces `import.meta.env`, you cannot access it with dynamic keys like `import.meta.env[key]`.