Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docs on environment variables #1575

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const SIDEBAR = {
{ text: 'RSS', link: 'guides/rss' },
{ text: 'Supported Imports', link: 'guides/imports' },
{ text: 'Aliases', link: 'guides/aliases' },
{ text: 'Environment Variables', link: 'guides/environment-variables' },
{ text: 'Deploy to the web', link: 'guides/deploy' },
{ text: 'Publish to npm', link: 'guides/publish-to-npm' },

Expand Down
61 changes: 61 additions & 0 deletions docs/src/pages/guides/environment-variables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
layout: ~/layouts/MainLayout.astro
title: Using environment variables
description: Learn how to use environment variables in an Astro project.
---

Astro uses Snowpack for environment variables, and allows you to use any of it's methods to get and set environment variables. Note that all environment variables must be prefixed with `SNOWPACK_PUBLIC` to be accessible by client side code.

## Setting environment variables

The two primary methods for setting environment variables are through the `env` property in `snowpack.config.mjs` or through a `.env` file using [@snowpack/plugin-dotenv](https://www.npmjs.com/package/@snowpack/plugin-dotenv).

### Setting environment variables through snowpack.config.mjs

Environment variables can be set through the `env` object in `snowpack.config.mjs`. You do not need to prefix variables if you are passing them through the `env` config, but in this tutorial we still will. For example, you could do this:

```js
// snowpack.config.mjs
export default {
env: {
SNOWPACK_PUBLIC_POKEAPI: "https://pokeapi.co/api/v2"
}
}
```

### Setting environment variables through dotenv files

Environment variables can also be set through `.env` files, although it takes more setup. First, you need to install [@snowpack/plugin-dotenv](https://www.npmjs.com/package/@snowpack/plugin-dotenv).

```bash
# npm
npm install @snowpack/plugin-dotenv
# yarn
yarn add @snowpack/plugin-dotenv
#pnpm
pnpm add @snowpack/plugin-dotenv
```

Then add the plugin to your `snowpack.config.mjs`.

```js
// snowpack.config.mjs
export default {
plugins: ['@snowpack/plugin-dotenv'],
};
```

Now, create a `.env` file in the project directory and add some variables to it

```bash
# .env
SNOWPACK_PUBLIC_POKEAPI="https://pokeapi.co/api/v2"
```

## Getting environment variables

Instead of using `process.env`, with Snowpack you use `__SNOWPACK_ENV__`. For example, to get the `SNOWPACK_PUBLIC_POKEAPI` environment variable, you could use `__SNOWPACK_ENV__.SNOWPACK_PUBLIC_POKEAPI`.

```js
fetch(`${__SNOWPACK_ENV__.SNOWPACK_PUBLIC_POKEAPI}/pokemon/squirtle`
```