Environment Variables
Dev Portal is built on top of Vite and uses their approach for managing environment variables.
Dev Portal exposes environment variables under the import.meta.env object as strings automatically.
To prevent accidentally leaking environment variables to the client, only variables prefixed with
ZUDOKU_PUBLIC_ are exposed to your Zudoku-processed code.
Security Notice
Environment variables prefixed with ZUDOKU_PUBLIC_ will be exposed to the client-side code and
visible in the browser. Never use this prefix for sensitive information like API keys, passwords, or
other secrets.
Local Env Files
When developing locally, you can create a .env file in the root of your project and add
environment-specific variables. See the
Vite documentation for more information on
supported files.
Here is an example of a .env.local file:
Code
You can access this variable in your application like this:
Code
Configuration Files
Environment variables can also be used in your configuration files. When referencing environment
variables in your configuration files, you can use process.env directly.
Code
React Components
If you need to access environment variables inside a custom react component, you can access them via
import.meta.env. Public environment variables are inlined during the build process.
Code
IntelliSense for TypeScript
By default, Dev Portal provides type definitions for import.meta.env in zudoku/client.d.ts. While
you can define more custom env variables in .env.[mode] files, you may want to get TypeScript
IntelliSense for user-defined env variables that are prefixed with ZUDOKU_PUBLIC_.
To achieve this, you can create a zudoku-env.d.ts in the src directory, then augment
ImportMetaEnv like this:
Code
Imports will break type augmentation
If the ImportMetaEnv augmentation does not work, make sure you do not have any import statements
in vite-env.d.ts. A helpful explanation can be found on
this StackOverflow reply.
Environment Variables in Zuplo
Set Dev Portal variables in the Zuplo Portal under Settings > Environment Variables, the same place you set gateway variables. For the Dev Portal:
- Only variables whose names start with
ZUPLO_PUBLIC_orZUDOKU_PUBLIC_are available. Both prefixes work the same way. - Only configuration variables are available. Zuplo never sends secret variables to the Dev Portal build, even if their names have a public prefix.
- Values are read when the portal is built and are embedded in the published site. Redeploy the environment after you change a value.
For how Dev Portal variables differ from gateway variables, see Configuring Environment Variables.
Zuplo Build Variables
When Zuplo builds your Dev Portal, it sets the following variables. You don't need to create them.
| Variable | Value |
|---|---|
ZUPLO_ENVIRONMENT_TYPE | The environment being built: PRODUCTION, PREVIEW, or WORKING_COPY. |
ZUPLO_SERVER_URL | The default URL of the API gateway deployed with this Dev Portal, for example https://my-project-main-1234abc.zuplo.app. Custom domains don't change this value. |
ZUPLO_BUILD_ID | A unique ID (UUID) for the build. |
Read these variables with import.meta.env in your custom components and
plugins:
Code
ZUPLO_ENVIRONMENT_TYPE in the Dev Portal build uses different values than the
ZUPLO_ENVIRONMENT_TYPE and ZUPLO_ENVIRONMENT_STAGE variables available to
your gateway. See
System Environment Variables
for the gateway values.
Check the Environment in zudoku.config.tsx
The configuration file runs twice: once in Node.js when the build starts, and
again as part of the built site, which renders your pages and <head> tags. In
the built site, process.env only resolves variables that start with
ZUPLO_PUBLIC_ or ZUDOKU_PUBLIC_, so process.env.ZUPLO_ENVIRONMENT_TYPE is
undefined there. Read import.meta.env first and fall back to process.env
to get the same value in both places:
zudoku.config.tsx
The build logs a warning that process.env.ZUPLO_ENVIRONMENT_TYPE isn't
prefixed with ZUPLO_PUBLIC_ or ZUDOKU_PUBLIC_. You can ignore it.
To avoid the warning, create a ZUPLO_PUBLIC_ variable with a different value
for each environment instead. Both approaches include the value in the
client-side bundle, so use them only for values that aren't secret.
Local Development
Locally, put ZUPLO_PUBLIC_ and ZUDOKU_PUBLIC_ variables in a .env file in
your Dev Portal folder, as described in Local Env Files.
Variables you set in the Zuplo Portal aren't available locally.
zuplo dev sets ZUPLO_SERVER_URL to your local gateway, for example
http://localhost:9000. ZUPLO_ENVIRONMENT_TYPE is never PRODUCTION locally,
so isProduction in the previous example is false.