Environment Variables in Templates
The get_env() template function reads environment variables at render time, letting you inject analytics IDs, API endpoints, feature flags, and other environment-specific values without maintaining separate config files per environment.
Quick Start
{# Read an environment variable #}
{% set analytics_id = get_env(name="GA_ID") %}
{% if analytics_id %}
<script async src="https://www.googletagmanager.com/gtag/js?id={{ analytics_id }}"></script>
{% endif %}
Set the variable when building:
GA_ID=G-XXXXXXX accent build --output ./public
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | (required) | Environment variable name |
default | string | "" | Fallback value if the variable is unset |
required | bool | false | Fail the render if the variable is unset and no default is provided |
Examples
Default values
{# Falls back to the default when the variable is not set #}
{% set api_url = get_env(name="API_URL", default="https://api.example.com") %}
<script>window.API_URL = "{{ api_url }}";</script>
Required variables
{# Stops the build with a clear error if DEPLOY_TOKEN is missing #}
{% set token = get_env(name="DEPLOY_TOKEN", required=true) %}
Error message when missing: Required environment variable 'DEPLOY_TOKEN' is not set
Conditional rendering
{% if get_env(name="PREVIEW") %}
<div class="preview-banner">Preview Deploy - {{ get_env(name="COMMIT_SHA", default="unknown") }}</div>
{% endif %}
Build vs serve
# Static build: values baked into HTML at build time GA_ID=G-123 PREVIEW=true accent build --output ./public # Dynamic serve: values read from server process environment on each render GA_ID=G-123 accent serve --production
In serve mode, values are read per-request but frozen into the page cache for the full TTL. Changing an env var requires a cache flush (SIGHUP, admin reload, or server restart).
Security: Allowlist
By default, all environment variables are accessible (matching Zola and Hugo behavior). To restrict access, set env_allowlist in config.yaml:
env_allowlist: - "GA_*" # Google Analytics IDs - "API_*" # API endpoints - "PREVIEW" # Preview flag - "COMMIT_SHA" # Git commit hash - "SITE_*" # Site-specific overrides
When the allowlist is set:
- Variables matching at least one pattern are returned normally
- Variables not matching any pattern return an empty string
required=trueon a non-matching variable produces an error
The allowlist applies to both accent build and accent serve. In production serve mode, always set an allowlist to prevent accidental leakage of secrets like DATABASE_URL or API_KEY.
Edition Availability
The get_env() function is available with no license key.