Environment Variables
It is a good practice to store runtime configuration data, such as API keys, in environment variables. When working locally Enhance supports loading environment variables with a .env
file.
Local Dev Environment Variable Tutorial
In this walkthrough we will set up environment variables, and display them on an example debug page. Please note, a .env
file should not be checked-in to revision control because it will often contain sensitive information. Environment variables normally should never be leaked, or displayed to the end web consumer: this is an example to help demonstrate the lifecycle.
1. Create a .env
file
The .env
file will contain key/value pairs representing the environment variables.
API_KEY=xxx
DOMAIN_NAME=https://enhance.dev
2. Create an API Route to read env vars
API Routes are a Node.js process; process.env
lists all environment variables.
export async function get () {
const env = process.env
return {
json: { env }
}
}
3. Create a Page to display the env vars
Display the values passed from the API Route.
export default function debug ({ html, state }) {
const env = state.store?.env
return html`<pre>${ JSON.stringify(env) }</pre>`
}