Gateway Environment Variables
- Applies to
- Gatewayruntime for API, AI, and MCP gateways
- Hosting
- Managed EdgeManaged DedicatedDiffersSelf-HostedDiffers
2 differences by hosting option
ZUPLO_ENVIRONMENT_TYPE values depend on the hosting environment, so check for the values your deployment reports instead of assuming edge.
ZUPLO_ENVIRONMENT_TYPE values depend on the hosting environment, so check for the values your deployment reports instead of assuming edge.
Your API gateway can read every environment variable set for its environment, including secrets. This page covers how to read variables in code, configuration files, and URL handlers, and which variables Zuplo sets for you.
Configuration Guide
To set and manage environment variables, and to learn how gateway variables differ from Developer Portal variables, see Configuring Environment Variables.
Where to Use Variables
Each location in your project has its own syntax:
| Location | Syntax | Resolved |
|---|---|---|
| Custom code (handlers, policies, hooks) | environment.VAR_NAME | Runtime |
Configuration files (policies.json, OpenAPI routes) | $env(VAR_NAME) | Build time |
| URL Rewrite, URL Forward, and WebSocket handlers | ${env.VAR_NAME} | Runtime (per request) |
In Code
The environment object from @zuplo/runtime returns a record of environment
variable names to their values.
Interface
Code
Usage
Access environment variables using the environment object:
Code
Type Safety
Since environment variables might not be defined, they return
string | undefined:
Code
In Configuration Files
Inside policy options, route handler options, and CORS policy options,
environment variables can be referenced with the $env(VAR_NAME) pattern.
Substitutions happen at build time — the build replaces each $env() expression
with a reference to the runtime environment object before the project is
deployed.
Where $env() is allowed
config/policies.json— any property underpolicies[].handler.options, including nested objects and array elementsconfig/policies.json— any direct property of acorsPolicies[]entry, such asallowedOrigins,allowedHeaders,allowedMethods,exposeHeaders, andmaxAgeconfig/routes.oas.json(and other OpenAPI route files) — any property under a route'sx-zuplo-route.handler.options, including nested objects and array elements
$env() is not allowed in other locations such as policy name,
policyType, module, route paths, or top-level OpenAPI fields. Using it
elsewhere produces a build error:
Code
Standalone substitution
When the value is only an $env() expression, the variable's value is
inserted directly. The runtime type matches the type of the environment variable
(always a string when set, undefined when not).
Code
String interpolation
You can mix $env() with literal text to compose a value. The build wraps the
result in a JavaScript template literal that's evaluated at runtime, so each
request gets the current value of the variable.
Code
If the variable isn't set, the interpolated portion resolves to an empty string.
For example, with API_HOST unset, "https://$env(API_HOST)/v1" becomes
"https:///v1". Use a standalone $env() (no surrounding text) if you need to
detect an unset variable in your handler or policy code.
In arrays
$env() works inside string array elements, including mixed arrays where some
elements are static and others are interpolated:
Code
In nested objects
$env() works at any depth within a handler or policy options object:
Code
Variable name rules
The name inside $env(...) is matched literally up to the first closing
parenthesis. Use only letters, digits, and underscores in the name — matching
what the
Zuplo Portal
accepts when you create the variable.
Rewrite, Forwarding, and WebSocket Handlers
The URL Rewrite handler's rewritePattern, the URL Forward handler's baseUrl,
and the WebSocket handler's rewritePattern use a different syntax. These
options are evaluated as JavaScript template literals at request time, so you
reference variables with ${env.VAR_NAME}:
Code
Because rewritePattern and baseUrl run as code, they also have access to the
request, URL parts, route params, and query string. See the
URL Rewrite Handler docs for the full list of
available variables.
Using $env(VAR_NAME) inside rewritePattern or baseUrl is the most common
mistake. The build will warn you when it detects this, but the value will be
passed through to the handler as a literal string and the rewrite will fail at
runtime. Always use ${env.VAR_NAME} in these two options.
System Environment Variables
Zuplo sets the following variables for your gateway. You can't create variables with these names.
| Variable | Value |
|---|---|
ZUPLO_ENVIRONMENT_STAGE | The environment stage: production, preview, working-copy, or local. Use this variable to check which environment the gateway runs in. |
ZUPLO_ENVIRONMENT_TYPE | The kind of host that runs the gateway, such as edge or local. |
ZUPLO_ENVIRONMENT_NAME | The globally unique name of the environment, as used in its default URL. For example, my-project-main-1235 in my-project-main-1235.zuplo.app. Custom domains don't change this value. |
ZUPLO_ACCOUNT_NAME | The name of the Zuplo account that owns the environment. |
ZUPLO_PROJECT_NAME | The name of the project that owns the environment. |
ZUPLO_BUILD_ID | A unique ID (UUID) for the build of the environment. |
ZUPLO_COMPATIBILITY_DATE | The compatibility date of the runtime environment. |
Your Developer Portal gets a different set of system variables, and its
ZUPLO_ENVIRONMENT_TYPE has different values. See
Dev Portal environment variables.
Environment Detection
Zuplo doesn't set NODE_ENV, because the gateway runs on a V8 runtime, not
Node.js. Use ZUPLO_ENVIRONMENT_STAGE instead:
Code
Common Patterns
Required Environment Variables
Create a helper to validate required variables:
Code
Configuration Object
Create a configuration object from environment variables:
Code
Environment-Specific Configuration
Use ZUPLO_ENVIRONMENT_STAGE to change behavior between production, preview,
and development environments:
Code
To use different values per environment, such as a backend URL, set the same variable with a different value for each environment instead of branching in code.
Secret Management
Code
Best Practices
1. Use Descriptive Names
Code
2. Provide Defaults Where Appropriate
Code
3. Validate Early
Code
4. Don't Log Secrets
Code
Setting Environment Variables
Set environment variables in the Zuplo Portal under Settings >
Environment Variables.
For local development, use a .env file. See
Configuring Environment Variables Locally.
See Also
- Configuring Environment Variables - Setting up environment variables
- ConfigurationError - Error handling for configuration issues