Skip to content

Environment Variables

Environment variables let you configure your app without hardcoding values. On device, they're stored in NVS (non-volatile storage) and persist across reboots.

Reading environment variables

Use import.meta.env to access environment variables in your code:

ts
const {WIFI_SSID, WIFI_PASSPHRASE, API_KEY} = import.meta.env

Accessing an undefined variable throws a TypeError, so typos and missing config are caught early:

ts
// throws TypeError: Environment variable "WFII_SSID" is not configured
const ssid = import.meta.env.WFII_SSID

You can also read environment variables programmatically with getEnv() from mikrojs/sys, which returns undefined instead of throwing:

ts
import {getEnv} from 'mikrojs/sys'

const apiKey = getEnv('API_KEY') // string | undefined

Setting environment variables

With mikro env

The mikro env command reads and writes environment variables directly on the connected device.

Set a variable:

sh
npx mikro env set API_URL https://api.example.com

Set a secret (prompts for the value, never displayed):

sh
npx mikro env set API_KEY --secret

List all variables:

sh
npx mikro env list

Secret values are masked in the output:

API_URL   https://api.example.com
API_KEY   ********

Delete a variable:

sh
npx mikro env delete API_URL

TIP

Variable names can be up to 15 characters (NVS key limit). Use short, descriptive names like WIFI_SSID, API_KEY, MQTT_HOST.

With .env files

The mikro dev and mikro deploy commands can load environment variables from files:

sh
npx mikro dev --env .env --secrets .secrets.env

Both files use standard dotenv format:

sh
# .env
WIFI_SSID=MyNetwork
API_URL=https://api.example.com
sh
# .secrets.env  (add this to .gitignore!)
WIFI_PASSPHRASE=hunter2
API_KEY=sk-abc123

Variables from --env are stored as regular env vars. Variables from --secrets are stored as secrets.

Secrets vs. regular variables

The --secret flag controls how a variable is displayed, not where it's stored. All environment variables (secret or not) are written to the same NVS storage and are equally available at runtime via import.meta.env.

The difference:

RegularSecret
Stored on deviceYesYes
Available in import.meta.envYesYes
Shown in mikro env listValue visibleMasked (********)
Set via CLImikro env set KEY valuemikro env set KEY --secret

A variable name must be unique. Setting a variable with --secret overwrites any existing non-secret variable with the same name, and vice versa.

Simulator

When running with mikro sim, environment variables are loaded from a .env file in the project directory if one exists.