MenuSettings and looks / Environment variables

Docs / Settings and looks

Environment variables

Set an API key once in Settings, and every Node Run reads it as process.env. The key stays out of your code.

On this page

Environment variables are names and values you set in Settings → Running → Environment variables. JSpark passes them to every Node Run as process.env, and to a page’s server. Your Snippet reads the name, so the value itself is never in your code, the Library or an export.

Add a variable

  1. Open Settings with ⌘, or Ctrl+, and click Running.
  2. Under Environment variables, type a Name, such as API_KEY.
  3. Type its Value.
  4. Click Add, or press ↵ or Enter in the Value field.

The name joins the list above the fields, with its value shown as dots. The next Node Run has it.

Two variables saved: Settings shows their names, never their values.
  • A name is letters, digits and _, and doesn’t start with a digit: API_KEY and token_2 work, 2FA and MY-KEY don’t. Settings says so when a name can’t be used.
  • A value can be up to 32 KB. A longer one is refused with That value is too long.

Change or remove one

  • To change a value, type the name again and the new value. The button says Replace. A saved value is never shown again, so type it in full.
  • To remove a variable, click the trash button beside its name.

Use it in a Snippet

Read the variable by its name:

const key = process.env.API_KEY
const res = await fetch('https://api.example.com/items', {
  headers: { Authorization: `Bearer ${key}` },
})
  • Every Node Run gets the variables: Run, Run as you type and the Timeline. A change applies from the next Run.
  • A page’s server gets them too. Its port, process.env.PORT, is always the one JSpark chose, even if you set a variable named PORT. See A page with a server.
  • Reading process.env doesn’t pause Run as you type. Writing to it does, and so does what you do with the value: the fetch above waits for you to press Run. See Outside Access.

Where the values are kept

  • On this computer, in JSpark’s data folder, in a file of their own, apart from your settings.
  • Never in your Snippets, the Library or an export, and never in Copy Diagnostics or a problem report.
  • Once saved, a value is not shown again. Settings only ever gets the names.
  • Restore defaults… leaves them as they are. See Reset or remove JSpark.
  • They don’t move with an export. On a new computer, add them again.

More than 3 variables JSpark+

JSpark keeps up to 3 variables. JSpark+ keeps as many as you need. With 3 saved in JSpark, Add wears the JSpark+ mark, and clicking it says that more than 3 environment variables needs JSpark+.

Three saved in JSpark: adding a fourth is JSpark+, and the last line says JSpark+ loads .env.

Changing or removing one of the 3 works in JSpark. If a Yearly key lapses, every variable you have stays, is still passed to Runs and can still be changed. Only adding another asks for JSpark+.

Load a .env file JSpark+

In JSpark+, a .env file in the Working Folder is passed to every Node Run too. When a name is in both, the .env value wins over the one in Settings.

# One variable per line
API_KEY=sk-123
export GREETING="Hello\nworld"
  • One NAME=value per line. Blank lines and lines starting with # are skipped, and export in front is allowed. A value can be in quotes; in double quotes, \n is a new line.
  • JSpark reads the file at every Run, so an edit applies to the next one.
  • It is always the Working Folder’s .env, also for a Snippet you set to a folder of your own.
  • When JSpark+ finds the file, Settings says The Working Folder’s .env is passed to Runs too, and wins over these.
  • Empty… in Settings deletes the .env with every other file in the Working Folder.

In JSpark, the .env is not read, and Settings says JSpark+ also passes the Working Folder’s .env to Runs. Variables set in Settings work in JSpark too.

The pictures show JSpark on a Mac. On Windows, the window looks a little different and uses Ctrl for ⌘, but everything works the same way.

Esc

Type to search every docs page.